summaryrefslogtreecommitdiffstats
path: root/nsprpub/pr/src/md/os2/os2thred.c
blob: aa929a1a234f9aaebfbacab766796ea662c2e7e7 (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "primpl.h"
#include <process.h>  /* for _beginthread() */
#include <signal.h>
#include <float.h>

/* --- globals ------------------------------------------------ */
_NSPR_TLS*        pThreadLocalStorage = 0;
_PRInterruptTable             _pr_interruptTable[] = { { 0 } };
APIRET (* APIENTRY QueryThreadContext)(TID, ULONG, PCONTEXTRECORD);

void
_PR_MD_ENSURE_TLS(void)
{
    if(!pThreadLocalStorage)
    {
        /* Allocate thread local storage (TLS).  Note, that only 32 bytes can
         * be allocated at a time.
         */
        int rc = DosAllocThreadLocalMemory(sizeof(_NSPR_TLS) / 4, (PULONG*)&pThreadLocalStorage);
        PR_ASSERT(rc == NO_ERROR);
        memset(pThreadLocalStorage, 0, sizeof(_NSPR_TLS));
    }
}

void
_PR_MD_EARLY_INIT()
{
    HMODULE hmod;

    if (DosLoadModule(NULL, 0, "DOSCALL1", &hmod) == 0)
        DosQueryProcAddr(hmod, 877, "DOSQUERYTHREADCONTEXT",
                         (PFN *)&QueryThreadContext);
}

static void
_pr_SetThreadMDHandle(PRThread *thread)
{
    PTIB ptib;
    PPIB ppib;
    PRUword rc;

    rc = DosGetInfoBlocks(&ptib, &ppib);

    thread->md.handle = ptib->tib_ptib2->tib2_ultid;
}

/* On OS/2, some system function calls seem to change the FPU control word,
 * such that we crash with a floating underflow exception.  The FIX_FPU() call
 * in jsnum.c does not always work, as sometimes FIX_FPU() is called BEFORE the
 * OS/2 system call that horks the FPU control word.  So, we set an exception
 * handler that covers any floating point exceptions and resets the FPU CW to
 * the required value.
 */
static ULONG
_System OS2_FloatExcpHandler(PEXCEPTIONREPORTRECORD p1,
                             PEXCEPTIONREGISTRATIONRECORD p2,
                             PCONTEXTRECORD p3,
                             PVOID pv)
{
#ifdef DEBUG_pedemonte
    printf("Entering exception handler; ExceptionNum = %x\n", p1->ExceptionNum);
    switch(p1->ExceptionNum) {
        case XCPT_FLOAT_DENORMAL_OPERAND:
            printf("got XCPT_FLOAT_DENORMAL_OPERAND\n");
            break;
        case XCPT_FLOAT_DIVIDE_BY_ZERO:
            printf("got XCPT_FLOAT_DIVIDE_BY_ZERO\n");
            break;
        case XCPT_FLOAT_INEXACT_RESULT:
            printf("got XCPT_FLOAT_INEXACT_RESULT\n");
            break;
        case XCPT_FLOAT_INVALID_OPERATION:
            printf("got XCPT_FLOAT_INVALID_OPERATION\n");
            break;
        case XCPT_FLOAT_OVERFLOW:
            printf("got XCPT_FLOAT_OVERFLOW\n");
            break;
        case XCPT_FLOAT_STACK_CHECK:
            printf("got XCPT_FLOAT_STACK_CHECK\n");
            break;
        case XCPT_FLOAT_UNDERFLOW:
            printf("got XCPT_FLOAT_UNDERFLOW\n");
            break;
    }
#endif

    switch(p1->ExceptionNum) {
        case XCPT_FLOAT_DENORMAL_OPERAND:
        case XCPT_FLOAT_DIVIDE_BY_ZERO:
        case XCPT_FLOAT_INEXACT_RESULT:
        case XCPT_FLOAT_INVALID_OPERATION:
        case XCPT_FLOAT_OVERFLOW:
        case XCPT_FLOAT_STACK_CHECK:
        case XCPT_FLOAT_UNDERFLOW:
        {
            unsigned cw = p3->ctx_env[0];
            if ((cw & MCW_EM) != MCW_EM) {
                /* Mask out all floating point exceptions */
                p3->ctx_env[0] |= MCW_EM;
                /* Following two lines set precision to 53 bit mantissa.  See jsnum.c */
                p3->ctx_env[0] &= ~MCW_PC;
                p3->ctx_env[0] |= PC_53;
                return XCPT_CONTINUE_EXECUTION;
            }
        }
    }
    return XCPT_CONTINUE_SEARCH;
}

PR_IMPLEMENT(void)
PR_OS2_SetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* excpreg)
{
    /* setup the exception handler for the thread */
    APIRET rv;
    excpreg->ExceptionHandler = OS2_FloatExcpHandler;
    excpreg->prev_structure = NULL;
    rv = DosSetExceptionHandler(excpreg);
    PR_ASSERT(rv == NO_ERROR);
}

PR_IMPLEMENT(void)
PR_OS2_UnsetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* excpreg)
{
    /* unset exception handler */
    APIRET rv = DosUnsetExceptionHandler(excpreg);
    PR_ASSERT(rv == NO_ERROR);
}

PRStatus
_PR_MD_INIT_THREAD(PRThread *thread)
{
    APIRET rv;

    if (thread->flags & (_PR_PRIMORDIAL | _PR_ATTACHED)) {
        _pr_SetThreadMDHandle(thread);
    }

    /* Create the blocking IO semaphore */
    rv = DosCreateEventSem(NULL, &(thread->md.blocked_sema), 0, 0);
    return (rv == NO_ERROR) ? PR_SUCCESS : PR_FAILURE;
}

typedef struct param_store
{
    void (*start)(void *);
    PRThread* thread;
} PARAMSTORE;

/* This is a small intermediate function that sets/unsets the exception
   handler before calling the initial thread function */
static void
ExcpStartFunc(void* arg)
{
    EXCEPTIONREGISTRATIONRECORD excpreg;
    PARAMSTORE params, *pParams = arg;

    PR_OS2_SetFloatExcpHandler(&excpreg);

    params = *pParams;
    PR_Free(pParams);
    params.start(params.thread);

    PR_OS2_UnsetFloatExcpHandler(&excpreg);
}

PRStatus
_PR_MD_CREATE_THREAD(PRThread *thread,
                     void (*start)(void *),
                     PRThreadPriority priority,
                     PRThreadScope scope,
                     PRThreadState state,
                     PRUint32 stackSize)
{
    PARAMSTORE* params = PR_Malloc(sizeof(PARAMSTORE));
    params->start = start;
    params->thread = thread;
    thread->md.handle = thread->id = (TID) _beginthread(ExcpStartFunc,
                                     NULL,
                                     thread->stack->stackSize,
                                     params);
    if(thread->md.handle == -1) {
        return PR_FAILURE;
    }

    /*
     * On OS/2, a thread is created with a thread priority of
     * THREAD_PRIORITY_NORMAL
     */

    if (priority != PR_PRIORITY_NORMAL) {
        _PR_MD_SET_PRIORITY(&(thread->md), priority);
    }

    return PR_SUCCESS;
}

void
_PR_MD_YIELD(void)
{
    /* Isn't there some problem with DosSleep(0) on OS/2? */
    DosSleep(0);
}

void
_PR_MD_SET_PRIORITY(_MDThread *thread, PRThreadPriority newPri)
{
    int nativePri = PRTYC_NOCHANGE;
    BOOL rv;

    if (newPri < PR_PRIORITY_FIRST) {
        newPri = PR_PRIORITY_FIRST;
    } else if (newPri > PR_PRIORITY_LAST) {
        newPri = PR_PRIORITY_LAST;
    }
    switch (newPri) {
        case PR_PRIORITY_LOW:
        case PR_PRIORITY_NORMAL:
            nativePri = PRTYC_REGULAR;
            break;
        case PR_PRIORITY_HIGH:
            nativePri = PRTYC_FOREGROUNDSERVER;
            break;
        case PR_PRIORITY_URGENT:
            nativePri = PRTYC_TIMECRITICAL;
    }
    rv = DosSetPriority(PRTYS_THREAD, nativePri, 0, thread->handle);
    PR_ASSERT(rv == NO_ERROR);
    if (rv != NO_ERROR) {
        PR_LOG(_pr_thread_lm, PR_LOG_MIN,
               ("PR_SetThreadPriority: can't set thread priority\n"));
    }
    return;
}

void
_PR_MD_CLEAN_THREAD(PRThread *thread)
{
    APIRET rv;

    if (thread->md.blocked_sema) {
        rv = DosCloseEventSem(thread->md.blocked_sema);
        PR_ASSERT(rv == NO_ERROR);
        thread->md.blocked_sema = 0;
    }

    if (thread->md.handle) {
        thread->md.handle = 0;
    }
}

void
_PR_MD_EXIT_THREAD(PRThread *thread)
{
    _PR_MD_CLEAN_THREAD(thread);
    _PR_MD_SET_CURRENT_THREAD(NULL);
}


void
_PR_MD_EXIT(PRIntn status)
{
    _exit(status);
}

#ifdef HAVE_THREAD_AFFINITY
PR_EXTERN(PRInt32)
_PR_MD_SETTHREADAFFINITYMASK(PRThread *thread, PRUint32 mask )
{
    /* Can we do this on OS/2?  Only on SMP versions? */
    PR_NOT_REACHED("Not implemented");
    return 0;

    /* This is what windows does:
       int rv;

       rv = SetThreadAffinityMask(thread->md.handle, mask);

       return rv?0:-1;
     */
}

PR_EXTERN(PRInt32)
_PR_MD_GETTHREADAFFINITYMASK(PRThread *thread, PRUint32 *mask)
{
    /* Can we do this on OS/2?  Only on SMP versions? */
    PR_NOT_REACHED("Not implemented");
    return 0;

    /* This is what windows does:
       PRInt32 rv, system_mask;

       rv = GetProcessAffinityMask(GetCurrentProcess(), mask, &system_mask);

       return rv?0:-1;
     */
}
#endif /* HAVE_THREAD_AFFINITY */

void
_PR_MD_SUSPEND_CPU(_PRCPU *cpu)
{
    _PR_MD_SUSPEND_THREAD(cpu->thread);
}

void
_PR_MD_RESUME_CPU(_PRCPU *cpu)
{
    _PR_MD_RESUME_THREAD(cpu->thread);
}

void
_PR_MD_SUSPEND_THREAD(PRThread *thread)
{
    if (_PR_IS_NATIVE_THREAD(thread)) {
        APIRET rc;

        /* XXXMB - DosSuspendThread() is not a blocking call; how do we
         * know when the thread is *REALLY* suspended?
         */
        rc = DosSuspendThread(thread->md.handle);
        PR_ASSERT(rc == NO_ERROR);
    }
}

void
_PR_MD_RESUME_THREAD(PRThread *thread)
{
    if (_PR_IS_NATIVE_THREAD(thread)) {
        DosResumeThread(thread->md.handle);
    }
}


PRThread*
_MD_CURRENT_THREAD(void)
{
    PRThread *thread;

    thread = _MD_GET_ATTACHED_THREAD();

    if (NULL == thread) {
        thread = _PRI_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL, 0);
    }

    PR_ASSERT(thread != NULL);
    return thread;
}