~ubuntu-branches/ubuntu/gutsy/virtualbox-ose/gutsy

« back to all changes in this revision

Viewing changes to src/libs/xpcom18a4/nsprpub/pr/src/md/windows/w95thred.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-09-08 16:44:58 UTC
  • Revision ID: james.westby@ubuntu.com-20070908164458-wao29470vqtr8ksy
Tags: upstream-1.5.0-dfsg2
ImportĀ upstreamĀ versionĀ 1.5.0-dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
16
 *
 
17
 * The Initial Developer of the Original Code is
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1998-2000
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
#include "primpl.h"
 
39
#include <process.h>  /* for _beginthreadex() */
 
40
 
 
41
extern void _PR_Win32InitTimeZone(void);  /* defined in ntmisc.c */
 
42
 
 
43
/* --- globals ------------------------------------------------ */
 
44
#ifdef _PR_USE_STATIC_TLS
 
45
__declspec(thread) struct PRThread  *_pr_thread_last_run;
 
46
__declspec(thread) struct PRThread  *_pr_currentThread;
 
47
__declspec(thread) struct _PRCPU    *_pr_currentCPU;
 
48
#else
 
49
DWORD _pr_currentThreadIndex;
 
50
DWORD _pr_lastThreadIndex;
 
51
DWORD _pr_currentCPUIndex;
 
52
#endif
 
53
int                           _pr_intsOff = 0; 
 
54
_PRInterruptTable             _pr_interruptTable[] = { { 0 } };
 
55
 
 
56
void
 
57
_PR_MD_EARLY_INIT()
 
58
{
 
59
    _PR_Win32InitTimeZone();
 
60
 
 
61
#ifndef _PR_USE_STATIC_TLS
 
62
    _pr_currentThreadIndex = TlsAlloc();
 
63
    _pr_lastThreadIndex = TlsAlloc();
 
64
    _pr_currentCPUIndex = TlsAlloc();
 
65
#endif
 
66
}
 
67
 
 
68
void _PR_MD_CLEANUP_BEFORE_EXIT(void)
 
69
{
 
70
    _PR_NT_FreeSids();
 
71
 
 
72
    WSACleanup();
 
73
 
 
74
#ifndef _PR_USE_STATIC_TLS
 
75
    TlsFree(_pr_currentThreadIndex);
 
76
    TlsFree(_pr_lastThreadIndex);
 
77
    TlsFree(_pr_currentCPUIndex);
 
78
#endif
 
79
}
 
80
 
 
81
PRStatus
 
82
_PR_MD_INIT_THREAD(PRThread *thread)
 
83
{
 
84
    if (thread->flags & (_PR_PRIMORDIAL | _PR_ATTACHED)) {
 
85
        /*
 
86
        ** Warning:
 
87
        ** --------
 
88
        ** NSPR requires a real handle to every thread.
 
89
        ** GetCurrentThread() returns a pseudo-handle which
 
90
        ** is not suitable for some thread operations (e.g.,
 
91
        ** suspending).  Therefore, get a real handle from
 
92
        ** the pseudo handle via DuplicateHandle(...)
 
93
        */
 
94
        DuplicateHandle(
 
95
                GetCurrentProcess(),     /* Process of source handle */
 
96
                GetCurrentThread(),      /* Pseudo Handle to dup */
 
97
                GetCurrentProcess(),     /* Process of handle */
 
98
                &(thread->md.handle),    /* resulting handle */
 
99
                0L,                      /* access flags */
 
100
                FALSE,                   /* Inheritable */
 
101
                DUPLICATE_SAME_ACCESS);  /* Options */
 
102
    }
 
103
 
 
104
    /* Create the blocking IO semaphore */
 
105
    thread->md.blocked_sema = CreateSemaphore(NULL, 0, 1, NULL);
 
106
    if (thread->md.blocked_sema == NULL)
 
107
        return PR_FAILURE;
 
108
        else
 
109
                return PR_SUCCESS;
 
110
}
 
111
 
 
112
static unsigned __stdcall
 
113
pr_root(void *arg)
 
114
{
 
115
    PRThread *thread = (PRThread *)arg;
 
116
    thread->md.start(thread);
 
117
    return 0;
 
118
}
 
119
 
 
120
PRStatus 
 
121
_PR_MD_CREATE_THREAD(PRThread *thread, 
 
122
                  void (*start)(void *), 
 
123
                  PRThreadPriority priority, 
 
124
                  PRThreadScope scope, 
 
125
                  PRThreadState state, 
 
126
                  PRUint32 stackSize)
 
127
{
 
128
 
 
129
    thread->md.start = start;
 
130
    thread->md.handle = (HANDLE) _beginthreadex(
 
131
                    NULL,
 
132
                    thread->stack->stackSize,
 
133
                    pr_root,
 
134
                    (void *)thread,
 
135
                    CREATE_SUSPENDED,
 
136
                    &(thread->id));
 
137
    if(!thread->md.handle) {
 
138
        return PR_FAILURE;
 
139
    }
 
140
 
 
141
    thread->md.id = thread->id;
 
142
    /*
 
143
     * On windows, a thread is created with a thread priority of
 
144
     * THREAD_PRIORITY_NORMAL.
 
145
     */
 
146
    if (priority != PR_PRIORITY_NORMAL) {
 
147
        _PR_MD_SET_PRIORITY(&(thread->md), priority);
 
148
    }
 
149
 
 
150
    /* Activate the thread */
 
151
    if ( ResumeThread( thread->md.handle ) != -1)
 
152
        return PR_SUCCESS;
 
153
 
 
154
    return PR_FAILURE;
 
155
}
 
156
 
 
157
void    
 
158
_PR_MD_YIELD(void)
 
159
{
 
160
    /* Can NT really yield at all? */
 
161
    Sleep(0);
 
162
}
 
163
 
 
164
void     
 
165
_PR_MD_SET_PRIORITY(_MDThread *thread, PRThreadPriority newPri)
 
166
{
 
167
    int nativePri;
 
168
    BOOL rv;
 
169
 
 
170
    if (newPri < PR_PRIORITY_FIRST) {
 
171
        newPri = PR_PRIORITY_FIRST;
 
172
    } else if (newPri > PR_PRIORITY_LAST) {
 
173
        newPri = PR_PRIORITY_LAST;
 
174
    }
 
175
    switch (newPri) {
 
176
        case PR_PRIORITY_LOW:
 
177
            nativePri = THREAD_PRIORITY_BELOW_NORMAL;
 
178
            break;
 
179
        case PR_PRIORITY_NORMAL:
 
180
            nativePri = THREAD_PRIORITY_NORMAL;
 
181
            break;
 
182
        case PR_PRIORITY_HIGH:
 
183
            nativePri = THREAD_PRIORITY_ABOVE_NORMAL;
 
184
            break;
 
185
        case PR_PRIORITY_URGENT:
 
186
            nativePri = THREAD_PRIORITY_HIGHEST;
 
187
    }
 
188
    rv = SetThreadPriority(thread->handle, nativePri);
 
189
    PR_ASSERT(rv);
 
190
    if (!rv) {
 
191
        PR_LOG(_pr_thread_lm, PR_LOG_MIN,
 
192
                ("PR_SetThreadPriority: can't set thread priority\n"));
 
193
    }
 
194
    return;
 
195
}
 
196
 
 
197
void
 
198
_PR_MD_CLEAN_THREAD(PRThread *thread)
 
199
{
 
200
    BOOL rv;
 
201
 
 
202
    if (thread->md.blocked_sema) {
 
203
        rv = CloseHandle(thread->md.blocked_sema);
 
204
        PR_ASSERT(rv);
 
205
        thread->md.blocked_sema = 0;
 
206
    }
 
207
 
 
208
    if (thread->md.handle) {
 
209
        rv = CloseHandle(thread->md.handle);
 
210
        PR_ASSERT(rv);
 
211
        thread->md.handle = 0;
 
212
    }
 
213
}
 
214
 
 
215
void
 
216
_PR_MD_EXIT_THREAD(PRThread *thread)
 
217
{
 
218
    _PR_MD_CLEAN_THREAD(thread);
 
219
    _PR_MD_SET_CURRENT_THREAD(NULL);
 
220
}
 
221
 
 
222
 
 
223
void
 
224
_PR_MD_EXIT(PRIntn status)
 
225
{
 
226
    _exit(status);
 
227
}
 
228
 
 
229
PRInt32 _PR_MD_SETTHREADAFFINITYMASK(PRThread *thread, PRUint32 mask )
 
230
{
 
231
    int rv;
 
232
 
 
233
    rv = SetThreadAffinityMask(thread->md.handle, mask);
 
234
 
 
235
    return rv?0:-1;
 
236
}
 
237
 
 
238
PRInt32 _PR_MD_GETTHREADAFFINITYMASK(PRThread *thread, PRUint32 *mask)
 
239
{
 
240
    PRInt32 rv, system_mask;
 
241
 
 
242
    rv = GetProcessAffinityMask(GetCurrentProcess(), mask, &system_mask);
 
243
    
 
244
    return rv?0:-1;
 
245
}
 
246
 
 
247
void 
 
248
_PR_MD_SUSPEND_CPU(_PRCPU *cpu) 
 
249
{
 
250
    _PR_MD_SUSPEND_THREAD(cpu->thread);
 
251
}
 
252
 
 
253
void
 
254
_PR_MD_RESUME_CPU(_PRCPU *cpu)
 
255
{
 
256
    _PR_MD_RESUME_THREAD(cpu->thread);
 
257
}
 
258
 
 
259
void
 
260
_PR_MD_SUSPEND_THREAD(PRThread *thread)
 
261
{
 
262
    if (_PR_IS_NATIVE_THREAD(thread)) {
 
263
        DWORD previousSuspendCount;
 
264
        /* XXXMB - SuspendThread() is not a blocking call; how do we
 
265
         * know when the thread is *REALLY* suspended?
 
266
         */
 
267
        previousSuspendCount = SuspendThread(thread->md.handle);
 
268
        PR_ASSERT(previousSuspendCount == 0);
 
269
    }
 
270
}
 
271
 
 
272
void
 
273
_PR_MD_RESUME_THREAD(PRThread *thread)
 
274
{
 
275
    if (_PR_IS_NATIVE_THREAD(thread)) {
 
276
        DWORD previousSuspendCount;
 
277
        previousSuspendCount = ResumeThread(thread->md.handle);
 
278
        PR_ASSERT(previousSuspendCount == 1);
 
279
    }
 
280
}
 
281
 
 
282
PRThread*
 
283
_MD_CURRENT_THREAD(void)
 
284
{
 
285
PRThread *thread;
 
286
 
 
287
        thread = _MD_GET_ATTACHED_THREAD();
 
288
 
 
289
        if (NULL == thread) {
 
290
                thread = _PRI_AttachThread(
 
291
            PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL, 0);
 
292
        }
 
293
        PR_ASSERT(thread != NULL);
 
294
        return thread;
 
295
}