Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Bug_2980_Regression_Test.cpp
blob29a6261884fe00f897a1a6fde3b5485a6d0ae40a
1 //FUZZ: disable check_for_lack_ACE_OS
3 #include "ace/config-lite.h"
5 #include <iostream>
6 #include <assert.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <string.h>
12 // This is a non-ACE driver program which loads an ACE-based DLL. The
13 // usual ACE-related defines will not apply and we must use
14 // platform-specific ones.
16 // This test has not been made to work on Windows and vxWorks, yet ...
18 #if defined (ACE_HAS_THREADS)
19 # define CAN_USE_THREADS
20 #else
21 # undef CAN_USE_THREADS
22 #endif
24 #if !(defined (WIN32) || defined (ACE_VXWORKS) || defined (ACE_HAS_LYNXOS_178))\
25 && !defined ACE_FACE_SAFETY_EXTENDED
26 # define CAN_RUN_TEST
28 # include <dlfcn.h>
30 namespace {
31 # if defined (ACE_DLL_SUFFIX)
32 const char *DllTestName = "./libBug_2980_Regression" ACE_DLL_SUFFIX;
33 #else
34 const char *DllTestName = "./libBug_2980_Regression.so";
35 #endif /* ACE_DLL_SUFFIX */
38 # if defined CAN_USE_THREADS
39 # include <pthread.h>
40 # endif
42 #else
43 # undef CAN_RUN_TEST
44 #endif
46 #if defined (__BORLANDC__)
47 # define PRINTF std::printf
48 #else
49 # define PRINTF printf
50 #endif
53 typedef int (* voidfunction)(void);
56 #if defined (CAN_RUN_TEST)
57 static void * dllHandle;
58 static voidfunction capi_init = 0;
59 static voidfunction capi_fini = 0;
60 static voidfunction capi_dosomething = 0;
61 #endif /* defined (CAN_RUN_TEST) */
64 extern "C"
65 void* loadDll(void*)
67 #if defined (CAN_RUN_TEST)
69 PRINTF ("loadDll - entered\n");
70 const char *subdir_env = getenv ("ACE_EXE_SUB_DIR");
71 if (subdir_env)
73 char *dllFile =
74 (char *) malloc (2 + strlen (subdir_env) + strlen (DllTestName));
75 strcpy (dllFile, subdir_env);
76 strcat (dllFile, "/");
77 strcat (dllFile, DllTestName);
78 dllHandle = dlopen (dllFile, RTLD_NOW);
79 free (dllFile);
81 else
83 dllHandle = dlopen (DllTestName, RTLD_NOW);
86 if (dllHandle == 0)
88 PRINTF ("unable to load library: %s\n", dlerror());
89 assert(dllHandle != 0);
92 void* temp = dlsym (dllHandle, "capi_init");
93 memcpy (&capi_init, &temp, sizeof (temp));
94 if (capi_init == 0)
96 PRINTF ("unable to resolve symbol capi_init: %s\n", dlerror());
97 assert(capi_init != 0);
100 temp = dlsym (dllHandle, "capi_fini");
101 memcpy (&capi_fini, &temp, sizeof (temp));
102 if (capi_fini == 0)
104 PRINTF ("unable to resolve symbol capi_fini: %s\n", dlerror());
105 assert(capi_fini != 0);
108 temp = dlsym (dllHandle, "capi_dosomething");
109 memcpy (&capi_dosomething, &temp, sizeof (temp));
110 if (capi_dosomething == 0)
112 PRINTF ("unable to resolve symbol capi_dosomething: %s\n", dlerror());
113 assert(capi_dosomething != 0);
115 capi_init();
116 PRINTF ("loadDll - leaving\n");
117 #endif /* defined (CAN_RUN_TEST) */
118 return 0;
121 extern "C"
122 void* unloadDll(void*)
124 #if defined (CAN_RUN_TEST)
125 PRINTF ("unloadDll - entered\n");
126 capi_fini();
127 dlclose(dllHandle);
128 PRINTF ("unloadDll - leaving\n");
129 #endif /* defined (CAN_RUN_TEST) */
130 return 0;
133 void * loadunloadDll(void *pp)
135 loadDll(pp);
137 #if defined (CAN_RUN_TEST)
138 assert(capi_dosomething != 0);
139 capi_dosomething();
140 #endif /* defined (CAN_RUN_TEST) */
142 unloadDll(pp);
144 return 0;
146 // FUZZ: disable check_for_improper_main_declaration
147 int main (int, char *[])
149 #if !defined (CAN_RUN_TEST)
150 # ifndef ACE_FACE_SAFETY_EXTENDED
151 PRINTF ("Terminating because this test has not been designed "
152 "to run on WIN32 or VXWORKS.\n");
153 # endif
154 #else
155 PRINTF ("main called\n");
156 PRINTF ("main - calling loadDll\n");
158 # if defined (CAN_USE_THREADS)
159 int result = 0;
160 pthread_t tid1;
161 result = pthread_create(&tid1, 0, &loadDll, 0);
162 if (result != 0)
164 PRINTF ("pthread_create() failed: %d\n", result);
165 return result;
168 pthread_join(tid1, 0);
169 PRINTF ("loadDll thread finished and re-joined\n");
171 # else
173 loadDll(0);
174 PRINTF ("loadDll finished\n");
176 # endif /* defined (CAN_USE_THREADS) */
178 PRINTF ("main - calling unloadDll\n");
180 # if defined (CAN_USE_THREADS)
181 pthread_t tid2;
182 result = pthread_create(&tid2, 0, &unloadDll, 0);
183 if (result != 0)
185 PRINTF ("pthread_create() failed: %d\n", result);
186 return 1;
188 pthread_join(tid2, 0);
189 PRINTF ("unloadDll thread finished and re-joined\n");
191 # else
193 unloadDll(0);
194 PRINTF ("unloadDll finished\n");
196 # endif /* defined (CAN_USE_THREADS) */
198 PRINTF ("main finished\n");
199 #endif /* defined (CAN_RUN_TEST) */
201 return 0;
204 //FUZZ: enable check_for_lack_ACE_OS