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