Debian Codegraph @

mt_win32.c

raw
1 /* This is the file to support WIN32 threads.
2 * We initialize the global data structure and the global access variable.
3 */
4
5 #include "regina_c.h"
6 #include "rexxsaa.h"
7 #define DONT_TYPEDEF_PFN
8 #include "rexx.h"
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <assert.h>
13
14 #define WIN32_LEAN_AND_MEAN
15 #ifdef _MSC_VER
16 # if _MSC_VER >= 1100
17 /* Stupid MSC can't compile own headers without warning at least in VC 5.0 */
18 # pragma warning(disable: 4115 4201 4214 4514)
19 # endif
20 #endif
21 #include <windows.h>
22 #ifdef _MSC_VER
23 # if _MSC_VER >= 1100
24 # pragma warning(default: 4115 4201 4214)
25 # endif
26 #endif
27
28 typedef struct { /* mt_tsd: static variables of this module (thread-safe) */
29 HANDLE Heap;
30 } mt_tsd_t; /* thread-specific but only needed by this module. see
31 * ReginaInitializeThread
32 */
33
34 /* every time a Regina thread is initialised by ReginaInitializeThread, the TSD
35 * for that thread is added to an empty slot in this array. When a thread
36 * terminates and calls Deinitialize, the used slot is freed up
37 */
38 tsd_t *tsds[MAX_CONCURRENT_REGINA_THREADS] = {0,};
39
40 static DWORD ThreadIndex = 0xFFFFFFFF; /* index of the TSD, not yet got */
41
42 /* We use only one critical section for all purposes. That's enough since
43 * we use it very rarely.
44 */
45 static CRITICAL_SECTION cs = {0,};
46
47 #if defined(DYNAMIC) || (defined(__MINGW32__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)))
48 /* Deinitialize is called when the thread terminates.
49 * This is a wonderful position to place code which frees all allocated stuff!
50 */
51 static void Deinitialize (tsd_t *TSD)
52 {
53 mt_tsd_t *mt = TSD->mt_tsd;
54 int i;
55
56 if (mt == NULL)
57 return;
58 if (mt->Heap != (HANDLE) 0)
59 HeapDestroy(mt->Heap);
60 free(mt);
61 /*
62 * Free up our slot in the list of process TSDs.
63 * Probably does not need to be protected (only allow one thread at a time)
64 */
65 for ( i = 0; i < MAX_CONCURRENT_REGINA_THREADS; i++ )
66 {
67 if ( tsds[i] == TSD )
68 {
69 tsds[i] = NULL;
70 break;
71 }
72 }
73 free(TSD);
74 }
75
76 int IfcReginaCleanup( VOID )
77 {
78 tsd_t *TSD = __regina_get_tsd();
79
80 if (TSD == NULL)
81 return 0;
82
83 deinit_rexxsaa(TSD);
84 Deinitialize (TSD);
85 TlsSetValue(ThreadIndex,NULL);
86
87 return 1;
88 }
89 #endif
90
91 #ifdef DYNAMIC
92 #define AcquireCriticalSection(cs) EnterCriticalSection(cs)
93 #define AcquireThreadIndex() ThreadIndex
94
95 /* We provide a DLL entry function. Look at the standard documentation */
96 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID reserved)
97 {
98 tsd_t *TSD;
99
100 switch (Reason) {
101 case DLL_PROCESS_ATTACH:
102 /* Two things to do */
103 InitializeCriticalSection(&cs);
104 ThreadIndex = TlsAlloc();
105 if (ThreadIndex == 0xFFFFFFFF)
106 return FALSE; /* FIXME, FGC: Do we have to call SetLastError()? */
107 break;
108 case DLL_PROCESS_DETACH:
109 break;
110 case DLL_THREAD_ATTACH:
111 break;
112 case DLL_THREAD_DETACH:
113 TSD = __regina_get_tsd();
114 if (TSD != NULL)
115 {
116 deinit_rexxsaa(TSD);
117 Deinitialize (TSD);
118 }
119 break;
120 }
121 return(TRUE);
122 }
123 #else
124 /* The DLL will do this work in DllMain above. This is for a MT library: */
125
126 /* AcquireCriticalSection locks the given critical section and
127 * initializes it on the first use.
128 */
129 static void AcquireCriticalSection(CRITICAL_SECTION *cs)
130 {
131 /* double initializing the critical section won't produce an error.
132 * We must do this on thread attachment if an error occurs in later
133 * implementations of WIN32.
134 */
135 InitializeCriticalSection(cs);
136
137 EnterCriticalSection(cs);
138 }
139
140 /* AcquireThreadIndex returns a valid ThreadIndex. */
141 static DWORD AcquireThreadIndex(void)
142 {
143 if (ThreadIndex == 0xFFFFFFFF)
144 { /* get a unique access variable for the whole process */
145 AcquireCriticalSection(&cs);
146 if (ThreadIndex == 0xFFFFFFFF) /* may've changed just before Acquire */
147 ThreadIndex = TlsAlloc();
148 LeaveCriticalSection(&cs);
149 /* give back a possible error value. nothing will help at this point */
150 }
151 return(ThreadIndex);
152 }
153 #endif
154
155 /* This should prevent some error messages and is used as a #define */
156 static unsigned sizeof_ptr(void)
157 {
158 return(sizeof(void *));
159 }
160
161 /* Lowest level memory allocation function for normal circumstances. */
162 static void *MTMalloc( const tsd_t *TSD, size_t size )
163 {
164 mt_tsd_t *mt = TSD->mt_tsd;
165
166 if (mt == NULL)
167 return(NULL); /* Let it die */
168
169 return(HeapAlloc(mt->Heap,HEAP_NO_SERIALIZE,size));
170 }
171
172 /* Lowest level memory deallocation function for normal circumstances. */
173 static void MTFree( const tsd_t *TSD, void *chunk )
174 {
175 mt_tsd_t *mt = TSD->mt_tsd;
176
177 /*
178 * Just in case...
179 */
180 if ( chunk == NULL)
181 return;
182
183 if (mt == NULL)
184 return; /* ??? */
185
186 HeapFree(mt->Heap,HEAP_NO_SERIALIZE,chunk);
187 }
188
189 /* Lowest level exit handler. */
190 static void MTExit(int code)
191 {
192 ExitThread(code);
193 }
194
195 /* ReginaInitializeThread creates a new thread structure and returns a ptr
196 * to the initialized value.
197 * The function may be called more than once.
198 */
199 tsd_t *ReginaInitializeThread(void)
200 {
201 int OK;
202 int i, found = 0;
203 DWORD idx;
204 tsd_t *retval;
205 mt_tsd_t *mt;
206
207 /* If you run into trouble here, you must change the code in
208 * ReginsSetMutex/ReginaUnsetMutex. The argument there assumes the
209 * following rule. This is an ugly hack.
210 */
211 assert(sizeof_ptr() >= sizeof(HANDLE));
212 if (sizeof_ptr() < sizeof(HANDLE))
213 return(NULL); /* Be absolutely sure that we HAVE a problem */
214
215 idx = AcquireThreadIndex();
216
217 /* fetch the value of the access variable */
218 retval = TlsGetValue(idx);
219
220 if (retval != NULL) /* already initialized? */
221 return(retval);
222
223 /* First call in this thread... */
224 retval = malloc(sizeof(tsd_t)); /* no Malloc, etc! */
225
226 if (retval == NULL) /* THIS is really a problem. I don't know what we */
227 return(NULL); /* should do now. Let the caller run into a crash... */
228
229 TlsSetValue(idx,retval);
230
231 memset(retval,0,sizeof(tsd_t));
232 retval->MTMalloc = MTMalloc;
233 retval->MTFree = MTFree;
234 retval->MTExit = MTExit;
235
236 /* Since the local data structure contains a Heap object for the memory
237 * management we initialize it first.
238 */
239 if ((mt = malloc(sizeof(mt_tsd_t))) == NULL)
240 return(NULL); /* This is a catastrophy */
241 retval->mt_tsd = mt;
242 memset(mt,0,sizeof(mt_tsd_t));
243 if ((mt->Heap = HeapCreate(HEAP_NO_SERIALIZE,0x10000,0)) == NULL)
244 return(NULL); /* This is a catastrophy */
245
246 OK = init_memory(retval); /* Initialize the memory module FIRST*/
247
248 /* Without the initial memory we don't have ANY chance! */
249 if (!OK)
250 return(NULL);
251
252 {
253 extern OS_Dep_funcs __regina_OS_Win;
254 retval->OS = &__regina_OS_Win;
255 }
256 retval->OS->init();
257 OK &= init_vars(retval); /* Initialize the variable module */
258 OK &= init_stacks(retval); /* Initialize the stack module */
259 OK &= init_filetable(retval); /* Initialize the files module */
260 OK &= init_math(retval); /* Initialize the math module */
261 OK &= init_spec_vars(retval); /* Initialize the interprt module */
262 OK &= init_tracing(retval); /* Initialize the tracing module */
263 OK &= init_builtin(retval); /* Initialize the builtin module */
264 OK &= init_client(retval); /* Initialize the client module */
265 OK &= init_library(retval); /* Initialize the library module */
266 OK &= init_rexxsaa(retval); /* Initialize the rexxsaa module */
267 OK &= init_shell(retval); /* Initialize the shell module */
268 OK &= init_envir(retval); /* Initialize the envir module */
269 OK &= init_expr(retval); /* Initialize the expr module */
270 OK &= init_error(retval); /* Initialize the error module */
271 #ifdef VMS
272 OK &= init_vms(retval); /* Initialize the vmscmd module */
273 OK &= init_vmf(retval); /* Initialize the vmsfuncs module */
274 #endif
275 OK &= init_arexxf(retval); /* Initialize the arxfuncs modules */
276 retval->loopcnt = 1; /* stupid r2perl-module */
277 retval->traceparse = -1;
278 retval->thread_id = (unsigned long)GetCurrentThreadId();
279
280 if (!OK)
281 exiterror( ERR_STORAGE_EXHAUSTED, 0 ) ;
282 /*
283 * Update the global list of threadid/TSD.
284 * Should this be protected (only allow one thread at a time)???
285 */
286 for ( i = 0; i < MAX_CONCURRENT_REGINA_THREADS; i++ )
287 {
288 if ( tsds[i] == 0 )
289 {
290 tsds[i] = retval;
291 found = 1;
292 break;
293 }
294 }
295 if ( found == 0 )
296 exiterror( ERR_STORAGE_EXHAUSTED, 1, "MAX_CONCURRENT_REGINA_THREADS exceeded." ) ;
297
298 return(retval);
299 }
300
301 int __regina_get_number_concurrent_regina_threads(void)
302 {
303 return MAX_CONCURRENT_REGINA_THREADS;
304 }
305
306 tsd_t *__regina_get_tsd_for_threadid( unsigned long threadid )
307 {
308 int i;
309 for ( i = 0; i < MAX_CONCURRENT_REGINA_THREADS; i++ )
310 {
311 if ( tsds[i]->thread_id == threadid )
312 return tsds[i];
313 }
314 return NULL;
315 }
316
317 tsd_t *__regina_get_next_tsd( int idx )
318 {
319 return tsds[idx];
320 }
321
322 /* __regina_get_tsd returns a pointer to the thread specific data. Be sure to
323 * calls this after a ReginaInitializeThread only.
324 */
325 tsd_t *__regina_get_tsd(void)
326 {
327 /* See above for comments */
328 return(TlsGetValue(ThreadIndex));
329 }
330
331 /* ReginaSetMutex is the opposite of ReginaUnsetMutex and sets a mutex
332 * variable. The "true" mutex is "*arg" since we have hidden the type
333 * HANDLE which is the correct type. Thus, we have used "HANDLE" and
334 * "void *" in the same manner. If we include windows.h for the
335 * definition of HANDLE we cant include windows later and may run
336 * into trouble. The initialization code will check of errors of
337 * this assumption.
338 * The argument (*mutex) may be NULL. We initialize the mutex in this
339 * case. This prevents the calling functions to initialize the mutex.
340 * The is a little speed penalty but the mutexes are not used very
341 * often. YOU should change it if it hurts you.
342 */
343 void ReginaSetMutex(void **mutex)
344 {
345 int OK = 1;
346 volatile HANDLE *w32_mutex = (volatile HANDLE *) mutex;
347
348 if (*w32_mutex == (HANDLE) 0)
349 {
350 AcquireCriticalSection(&cs);
351 if (*w32_mutex == (HANDLE)0) /* may have changed due MT */
352 {
353 *w32_mutex = CreateMutex(NULL,FALSE,NULL);
354 if (*w32_mutex == NULL)
355 OK = 0;
356 }
357 LeaveCriticalSection(&cs);
358 if (!OK)
359 { /* We must die now! There is no other chance. */
360 *((int *) NULL) = 1;
361 }
362 }
363
364 WaitForSingleObject(*w32_mutex,INFINITE);
365 /* ignore errors, we continue especially if WAIT_ABANDONED occurs */
366 }
367
368 /* see ReginaSetMutex */
369 void ReginaUnsetMutex(void **mutex)
370 {
371 volatile HANDLE *w32_mutex = (volatile HANDLE *) mutex;
372
373 ReleaseMutex(*w32_mutex);
374 }