Debian Codegraph @

mt_os2.c

raw
1 /* This is the file to support OS/2 threads.
2 * This file is under construction. We partially support EMX. The EMX
3 * environment must currently be "OS2". Thus, usage is provided for
4 * makefile.os2.emx. Others may run.
5 * We initialize the global data structure and the global access variable.
6 */
7
8 #define INCL_NOCOMMON
9 #define INCL_DOSPROCESS
10 #define INCL_DOSSEMAPHORES
11 #define INCL_DOSERRORS
12 #define INCL_DOSMEMMGR
13
14 #ifdef __EMX__
15 # define DONT_TYPEDEF_PFN
16 # include <io.h>
17 # include <os2emx.h>
18 #elif defined (__WATCOMC__)
19 # define DONT_TYPEDEF_PFN
20 # include <os2.h>
21 #endif
22
23 #include "rexx.h"
24 #include <stdlib.h>
25 #include <stddef.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <assert.h>
29
30 static ULONG *TSD_ptrs = NULL;
31
32 typedef struct _MT_mem {
33 struct _MT_mem *prev;
34 struct _MT_mem *next;
35 /* real data follows here */
36 } MT_mem;
37
38 typedef struct { /* mt_tsd: static variables of this module (thread-safe) */
39 MT_mem *mem_base; /* From DosAllocMem, we use this memory scheme since it is
40 * native OS/2. emx has its own heap allocation functions.
41 */
42 } mt_tsd_t; /* thread-specific but only needed by this module. see
43 * ReginaInitializeThread
44 */
45
46 /* every time a Regina thread is initialised by ReginaInitializeThread, the TSD
47 * for that thread is added to an empty slot in this array. When a thread
48 * terminates and calls Deinitialize, the used slot is freed up
49 */
50 tsd_t *tsds[MAX_CONCURRENT_REGINA_THREADS] = {0,};
51
52 /* Returns a pointer to the TSD pointer */
53 static tsd_t **FindThreadDataIdx(void)
54 {
55 tsd_t **retval = NULL;
56 ULONG rc;
57
58 DosEnterCritSec();
59 if (TSD_ptrs != NULL)
60 {
61 retval = (tsd_t **) TSD_ptrs;
62 goto Leave;
63 }
64 if ((rc = DosAllocThreadLocalMemory(1,&TSD_ptrs)) != NO_ERROR)
65 {
66 fprintf(stderr,"rc = %lu from DosAllocThreadLocalMemory, await SIGSEGV!\n",
67 (unsigned long) rc);
68 TSD_ptrs = NULL;
69 }
70 if (TSD_ptrs != NULL)
71 retval = (tsd_t **) TSD_ptrs;
72 Leave:
73 DosExitCritSec();
74 return(retval);
75 }
76
77 /* Deinitialize is called when the thread terminates.
78 * This is a wonderful position to place code which frees all allocated stuff!
79 */
80 static void Deinitialize(void *buf)
81 {
82 tsd_t *TSD = buf;
83 mt_tsd_t *mt;
84 MT_mem *chunk;
85
86 if (TSD == NULL) /* Should never happen but be sure */
87 return;
88
89 deinit_rexxsaa(TSD);
90
91 mt = TSD->mt_tsd;
92 if (mt)
93 while ((chunk = mt->mem_base) != NULL)
94 {
95 TSD->MTFree(TSD,chunk + 1);
96 if (mt->mem_base == chunk)
97 break; /* something goes wrong. Don't run into an endless loop */
98 }
99 if (mt)
100 free(mt);
101 free(TSD);
102 }
103
104 int IfcReginaCleanup( VOID )
105 {
106 tsd_t **ptsd = FindThreadDataIdx();
107
108 if (*ptsd == NULL)
109 return 0;
110 Deinitialize(*ptsd);
111 *ptsd = NULL;
112
113 return 1;
114 }
115
116 #ifdef DYNAMIC
117 # ifdef __EMX__
118 /* We provide a DLL entry function. Look at the standard documentation for
119 * EMX. See emxdev.doc and testdll6.c. We may use the macros CRT_INIT1 and
120 * CRT_TERM1 from sys/startup.h instead but that's undocumented stuff.
121 */
122 int _CRT_init (void);
123 void _CRT_term (void);
124 void __ctordtorInit (void);
125 void __ctordtorTerm (void);
126
127 unsigned long _DLL_InitTerm (unsigned long mod_handle, unsigned long flag)
128 {
129 switch (flag)
130 {
131 case 0:
132 if (_CRT_init() != 0)
133 return 0;
134 __ctordtorInit();
135 return 1;
136 case 1:
137 /*
138 * This will run ONLY if called on dynamic unload of the complete
139 * library. This means, the last thread will receive an unload
140 * command. Stupid OS/2.
141 */
142 IfcReginaCleanup();
143 __ctordtorTerm();
144 _CRT_term();
145 return 1;
146 default:
147 break;
148 }
149 return 0;
150 }
151 # endif /* EMX */
152 #endif /* DYNAMIC */
153
154 /* This should prevent some error messages and is used as a #define */
155 static unsigned sizeof_ptr(void)
156 {
157 return(sizeof(void *));
158 }
159
160 /* Lowest level memory allocation function for normal circumstances. */
161 static void *MTMalloc( const tsd_t *TSD, size_t size )
162 {
163 mt_tsd_t *mt;
164 MT_mem *new;
165
166 if (DosAllocMem((PPVOID) &new,
167 size + sizeof(MT_mem),
168 PAG_READ|PAG_WRITE|PAG_COMMIT) != NO_ERROR)
169 return(NULL); /* may happen. errors are detected in the above layers */
170
171 mt = TSD->mt_tsd;
172 new->prev = NULL;
173 new->next = mt->mem_base;
174 if (mt->mem_base)
175 mt->mem_base->prev = new;
176 mt->mem_base = new;
177 return(new + 1); /* jump over the head */
178 }
179
180 /* Lowest level memory deallocation function for normal circumstances. */
181 static void MTFree( const tsd_t *TSD, void *chunk )
182 {
183 mt_tsd_t *mt = TSD->mt_tsd;
184 MT_mem *this;
185
186 /*
187 * Just in case...
188 */
189 if (chunk == NULL)
190 return;
191
192 this = chunk;
193 this--; /* Go to the header of the chunk */
194
195 if (this->prev)
196 if (this->prev->next != this)
197 return;
198 if (this->next)
199 if (this->next->prev != this)
200 return;
201
202 /* This is a chunk allocated by MTMalloc */
203 if (this->prev)
204 this->prev->next = this->next;
205 if (this->next)
206 this->next->prev = this->prev;
207 if (this == mt->mem_base)
208 mt->mem_base = this->next;
209
210 /* Last not least we set the pointers to NULL. This prevents a double-free*/
211 this->next = NULL;
212 this->prev = NULL;
213 DosFreeMem(this);
214 }
215
216 /* Lowest level exit handler. */
217 static void MTExit(int code)
218 {
219 DosExit(EXIT_THREAD,code);
220 }
221
222 /* ReginaInitializeThread creates a new thread structure and returns a ptr
223 * to the initialized value.
224 * The function may be called more than once.
225 */
226 tsd_t *ReginaInitializeThread(void)
227 {
228 int OK;
229 tsd_t *retval,**ptsd;
230 mt_tsd_t *mt;
231
232 /* If you run into trouble here, you must change the code in
233 * ReginsSetMutex/ReginaUnsetMutex. The argument there assumes the
234 * following rule. This is an ugly hack.
235 */
236 assert(sizeof_ptr() >= sizeof(HMTX));
237 if (sizeof_ptr() < sizeof(HMTX))
238 return(NULL); /* Be absolutely sure that we HAVE a problem */
239
240 ptsd = FindThreadDataIdx();
241 if (ptsd == NULL) /* can't initialize? */
242 return(NULL);
243
244 if (*ptsd != NULL) /* already initialized? */
245 return(*ptsd);
246
247 /* First call in this thread, a atexit() per thread will be great, sigh...*/
248 retval = malloc(sizeof(tsd_t)); /* no Malloc, etc! */
249
250 if (retval == NULL) /* THIS is really a problem. I don't know what we */
251 return(NULL); /* should do now. Let the caller run into a crash... */
252
253 *ptsd = retval;
254
255 memset(retval,0,sizeof(tsd_t));
256 retval->MTMalloc = MTMalloc;
257 retval->MTFree = MTFree;
258 retval->MTExit = MTExit;
259
260 /* Since the local data structure contains a Heap object for the memory
261 * management we initialize it first.
262 */
263 if ((mt = malloc(sizeof(mt_tsd_t))) == NULL)
264 return(NULL); /* This is a catastrophy */
265 retval->mt_tsd = mt;
266 mt->mem_base = NULL;
267
268 OK = init_memory(retval); /* Initialize the memory module FIRST*/
269
270 /* Without the initial memory we don't have ANY chance! */
271 if (!OK)
272 return(NULL);
273
274 {
275 extern OS_Dep_funcs __regina_OS_Os2;
276 retval->OS = &__regina_OS_Os2;
277 }
278
279 OK &= init_vars(retval); /* Initialize the variable module */
280 OK &= init_stacks(retval); /* Initialize the stack module */
281 OK &= init_filetable(retval); /* Initialize the files module */
282 OK &= init_math(retval); /* Initialize the math module */
283 OK &= init_spec_vars(retval); /* Initialize the interprt module */
284 OK &= init_tracing(retval); /* Initialize the tracing module */
285 OK &= init_builtin(retval); /* Initialize the builtin module */
286 OK &= init_client(retval); /* Initialize the client module */
287 OK &= init_library(retval); /* Initialize the library module */
288 OK &= init_rexxsaa(retval); /* Initialize the rexxsaa module */
289 OK &= init_shell(retval); /* Initialize the shell module */
290 OK &= init_envir(retval); /* Initialize the envir module */
291 OK &= init_expr(retval); /* Initialize the expr module */
292 OK &= init_error(retval); /* Initialize the error module */
293 #ifdef VMS
294 OK &= init_vms(retval); /* Initialize the vmscmd module */
295 OK &= init_vmf(retval); /* Initialize the vmsfuncs module */
296 #endif
297 OK &= init_arexxf(retval); /* Initialize the arxfuncs modules */
298 retval->loopcnt = 1; /* stupid r2perl-module */
299 retval->traceparse = -1;
300 #ifdef __EMX__
301 retval->thread_id = _gettid();
302 #elif defined(__WATCOMC__)
303 retval->thread_id = *_threadid;
304 #else
305 retval->thread_id = _gettid();
306 #endif
307
308 if (!OK)
309 exiterror( ERR_STORAGE_EXHAUSTED, 0 ) ;
310 return(retval);
311 }
312
313 int __regina_get_number_concurrent_regina_threads(void)
314 {
315 return MAX_CONCURRENT_REGINA_THREADS;
316 }
317
318 tsd_t *__regina_get_tsd_for_threadid( unsigned long threadid )
319 {
320 int i;
321 for ( i = 0; i < MAX_CONCURRENT_REGINA_THREADS; i++ )
322 {
323 if ( tsds[i]->thread_id == threadid )
324 return tsds[i];
325 }
326 return NULL;
327 }
328
329 tsd_t *__regina_get_next_tsd( int idx )
330 {
331 return tsds[idx];
332 }
333
334 /* __regina_get_tsd returns a pointer to the thread specific data. Be sure to
335 * calls this after a ReginaInitializeThread only.
336 */
337 tsd_t *__regina_get_tsd(void)
338 {
339 /* See above for comments */
340 return(*FindThreadDataIdx());
341 }
342
343 /* ReginaSetMutex is the opposite of ReginaUnsetMutex and sets a mutex
344 * variable. The "true" mutex is "*arg" since we have hidden the type
345 * HMTX which is the correct type. Thus, we have used "HMTX" and
346 * "void *" in the same manner. If we include windows.h for the
347 * definition of HANDLE we cant include windows later and may run
348 * into trouble. The initialization code will check of errors of
349 * this assumption.
350 * The argument (*mutex) may be NULL. We initialize the mutex in this
351 * case. This prevents the calling functions to initialize the mutex.
352 * The is a little speed penalty but the mutexes are not used very
353 * often. YOU should change it if it hurts you.
354 */
355 void ReginaSetMutex(void **mutex)
356 {
357 int OK = 1;
358 HMTX *os2_mutex = (HMTX *) mutex;
359
360 if (*os2_mutex == (HMTX) 0)
361 {
362 DosEnterCritSec();
363 if (*os2_mutex == (HMTX) 0) /* may have changed due MT */
364 {
365 if (DosCreateMutexSem(NULL,os2_mutex,0,FALSE) != NO_ERROR)
366 OK = 0;
367 }
368 DosExitCritSec();
369 if (!OK)
370 { /* We must die now! There is no other chance. */
371 *((int *) NULL) = 1;
372 }
373 }
374
375 DosRequestMutexSem(*os2_mutex,SEM_INDEFINITE_WAIT);
376 /* ignore errors, we continue especially if ERROR_INTERRUPTED occurs.
377 * FIXME, ignoring ERROR_INTERRUPTED OK?
378 */
379 }
380
381 /* see ReginaSetMutex */
382 void ReginaUnsetMutex(void **mutex)
383 {
384 HMTX *os2_mutex = (HMTX *) mutex;
385
386 DosReleaseMutexSem(*os2_mutex);
387 }