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
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/
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
15
* The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36
* ***** END LICENSE BLOCK ***** */
39
#include <process.h> /* for _beginthreadex() */
41
extern void _PR_Win32InitTimeZone(void); /* defined in ntmisc.c */
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;
49
DWORD _pr_currentThreadIndex;
50
DWORD _pr_lastThreadIndex;
51
DWORD _pr_currentCPUIndex;
54
_PRInterruptTable _pr_interruptTable[] = { { 0 } };
59
_PR_Win32InitTimeZone();
61
#ifndef _PR_USE_STATIC_TLS
62
_pr_currentThreadIndex = TlsAlloc();
63
_pr_lastThreadIndex = TlsAlloc();
64
_pr_currentCPUIndex = TlsAlloc();
68
void _PR_MD_CLEANUP_BEFORE_EXIT(void)
74
#ifndef _PR_USE_STATIC_TLS
75
TlsFree(_pr_currentThreadIndex);
76
TlsFree(_pr_lastThreadIndex);
77
TlsFree(_pr_currentCPUIndex);
82
_PR_MD_INIT_THREAD(PRThread *thread)
84
if (thread->flags & (_PR_PRIMORDIAL | _PR_ATTACHED)) {
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(...)
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 */
104
/* Create the blocking IO semaphore */
105
thread->md.blocked_sema = CreateSemaphore(NULL, 0, 1, NULL);
106
if (thread->md.blocked_sema == NULL)
112
static unsigned __stdcall
115
PRThread *thread = (PRThread *)arg;
116
thread->md.start(thread);
121
_PR_MD_CREATE_THREAD(PRThread *thread,
122
void (*start)(void *),
123
PRThreadPriority priority,
129
thread->md.start = start;
130
thread->md.handle = (HANDLE) _beginthreadex(
132
thread->stack->stackSize,
137
if(!thread->md.handle) {
141
thread->md.id = thread->id;
143
* On windows, a thread is created with a thread priority of
144
* THREAD_PRIORITY_NORMAL.
146
if (priority != PR_PRIORITY_NORMAL) {
147
_PR_MD_SET_PRIORITY(&(thread->md), priority);
150
/* Activate the thread */
151
if ( ResumeThread( thread->md.handle ) != -1)
160
/* Can NT really yield at all? */
165
_PR_MD_SET_PRIORITY(_MDThread *thread, PRThreadPriority newPri)
170
if (newPri < PR_PRIORITY_FIRST) {
171
newPri = PR_PRIORITY_FIRST;
172
} else if (newPri > PR_PRIORITY_LAST) {
173
newPri = PR_PRIORITY_LAST;
176
case PR_PRIORITY_LOW:
177
nativePri = THREAD_PRIORITY_BELOW_NORMAL;
179
case PR_PRIORITY_NORMAL:
180
nativePri = THREAD_PRIORITY_NORMAL;
182
case PR_PRIORITY_HIGH:
183
nativePri = THREAD_PRIORITY_ABOVE_NORMAL;
185
case PR_PRIORITY_URGENT:
186
nativePri = THREAD_PRIORITY_HIGHEST;
188
rv = SetThreadPriority(thread->handle, nativePri);
191
PR_LOG(_pr_thread_lm, PR_LOG_MIN,
192
("PR_SetThreadPriority: can't set thread priority\n"));
198
_PR_MD_CLEAN_THREAD(PRThread *thread)
202
if (thread->md.blocked_sema) {
203
rv = CloseHandle(thread->md.blocked_sema);
205
thread->md.blocked_sema = 0;
208
if (thread->md.handle) {
209
rv = CloseHandle(thread->md.handle);
211
thread->md.handle = 0;
216
_PR_MD_EXIT_THREAD(PRThread *thread)
218
_PR_MD_CLEAN_THREAD(thread);
219
_PR_MD_SET_CURRENT_THREAD(NULL);
224
_PR_MD_EXIT(PRIntn status)
229
PRInt32 _PR_MD_SETTHREADAFFINITYMASK(PRThread *thread, PRUint32 mask )
233
rv = SetThreadAffinityMask(thread->md.handle, mask);
238
PRInt32 _PR_MD_GETTHREADAFFINITYMASK(PRThread *thread, PRUint32 *mask)
240
PRInt32 rv, system_mask;
242
rv = GetProcessAffinityMask(GetCurrentProcess(), mask, &system_mask);
248
_PR_MD_SUSPEND_CPU(_PRCPU *cpu)
250
_PR_MD_SUSPEND_THREAD(cpu->thread);
254
_PR_MD_RESUME_CPU(_PRCPU *cpu)
256
_PR_MD_RESUME_THREAD(cpu->thread);
260
_PR_MD_SUSPEND_THREAD(PRThread *thread)
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?
267
previousSuspendCount = SuspendThread(thread->md.handle);
268
PR_ASSERT(previousSuspendCount == 0);
273
_PR_MD_RESUME_THREAD(PRThread *thread)
275
if (_PR_IS_NATIVE_THREAD(thread)) {
276
DWORD previousSuspendCount;
277
previousSuspendCount = ResumeThread(thread->md.handle);
278
PR_ASSERT(previousSuspendCount == 1);
283
_MD_CURRENT_THREAD(void)
287
thread = _MD_GET_ATTACHED_THREAD();
289
if (NULL == thread) {
290
thread = _PRI_AttachThread(
291
PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL, 0);
293
PR_ASSERT(thread != NULL);