1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/*
3 * The contents of this file are subject to the Mozilla Public
4 * License Version 1.1 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of
6 * the License at http://www.mozilla.org/MPL/
7 *
8 * Software distributed under the License is distributed on an "AS
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10 * implied. See the License for the specific language governing
11 * rights and limitations under the License.
12 *
13 * The Original Code is the Netscape Portable Runtime (NSPR).
14 *
15 * The Initial Developer of the Original Code is Netscape
16 * Communications Corporation.  Portions created by Netscape are
17 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
18 * Rights Reserved.
19 *
20 * Contributor(s):
21 *
22 * Alternatively, the contents of this file may be used under the
23 * terms of the GNU General Public License Version 2 or later (the
24 * "GPL"), in which case the provisions of the GPL are applicable
25 * instead of those above.  If you wish to allow use of your
26 * version of this file only under the terms of the GPL and not to
27 * allow others to use your version of this file under the MPL,
28 * indicate your decision by deleting the provisions above and
29 * replace them with the notice and other provisions required by
30 * the GPL.  If you do not delete the provisions above, a recipient
31 * may use your version of this file under either the MPL or the
32 * GPL.
33 */
34
35#ifndef nspr_win32_defs_h___
36#define nspr_win32_defs_h___
37
38/* Need to force service-pack 3 extensions to be defined by
39** setting _WIN32_WINNT to NT 4.0 for winsock.h, winbase.h, winnt.h.
40*/
41#ifndef  _WIN32_WINNT
42    #define _WIN32_WINNT 0x0400
43#elif   (_WIN32_WINNT < 0x0400)
44    #undef  _WIN32_WINNT
45    #define _WIN32_WINNT 0x0400
46#endif /* _WIN32_WINNT */
47
48#include <windows.h>
49#include <winsock.h>
50#ifdef __MINGW32__
51#include <mswsock.h>
52#endif
53#include <errno.h>
54
55#include "prio.h"
56#include "prclist.h"
57
58/*
59 * Internal configuration macros
60 */
61
62#define PR_LINKER_ARCH      "win32"
63#define _PR_SI_SYSNAME        "WINNT"
64#define _PR_SI_ARCHITECTURE   "x86"    /* XXXMB hardcode for now */
65
66#define HAVE_DLL
67#define HAVE_CUSTOM_USER_THREADS
68#define HAVE_THREAD_AFFINITY
69#define _PR_HAVE_GETADDRINFO
70#define _PR_INET6_PROBE
71#ifndef _PR_INET6
72#define AF_INET6 23
73/* newer ws2tcpip.h provides these */
74#ifndef AI_CANONNAME
75#define AI_CANONNAME 0x2
76struct addrinfo {
77    int ai_flags;
78    int ai_family;
79    int ai_socktype;
80    int ai_protocol;
81    size_t ai_addrlen;
82    char *ai_canonname;
83    struct sockaddr *ai_addr;
84    struct addrinfo *ai_next;
85};
86#endif
87#endif
88#define _PR_HAVE_THREADSAFE_GETHOST
89#define _PR_HAVE_ATOMIC_OPS
90#define _PR_HAVE_ATOMIC_CAS
91#define PR_HAVE_WIN32_NAMED_SHARED_MEMORY
92#define _PR_HAVE_PEEK_BUFFER
93#define _PR_PEEK_BUFFER_MAX (32 * 1024)
94#define _PR_FD_NEED_EMULATE_MSG_PEEK(fd) \
95    (!(fd)->secret->nonblocking && (fd)->secret->inheritable != _PR_TRI_TRUE)
96
97/* --- Common User-Thread/Native-Thread Definitions --------------------- */
98
99/* --- Globals --- */
100extern struct PRLock                      *_pr_schedLock;
101
102/* --- Typedefs --- */
103typedef void (*FiberFunc)(void *);
104
105#define PR_NUM_GCREGS           8
106typedef PRInt32	                PR_CONTEXT_TYPE[PR_NUM_GCREGS];
107#define GC_VMBASE               0x40000000
108#define GC_VMLIMIT              0x00FFFFFF
109
110#define _MD_MAGIC_THREAD	0x22222222
111#define _MD_MAGIC_THREADSTACK	0x33333333
112#define _MD_MAGIC_SEGMENT	0x44444444
113#define _MD_MAGIC_DIR		0x55555555
114
115struct _MDCPU {
116    int              unused;
117};
118
119enum _MDIOModel {
120    _MD_BlockingIO = 0x38,
121    _MD_MultiWaitIO = 0x49
122};
123
124typedef struct _MDOverlapped {
125    OVERLAPPED overlapped;              /* Used for async I/O */
126
127    enum _MDIOModel ioModel;            /* The I/O model to implement
128                                         * using overlapped I/O.
129                                         */
130    union {
131        struct _MDThread *mdThread;     /* For blocking I/O, this structure
132                                         * is embedded in the _MDThread
133                                         * structure.
134                                         */
135        struct {
136            PRCList links;              /* for group->io_ready list */
137            struct PRRecvWait *desc;    /* For multiwait I/O, this structure
138                                         * is associated with a PRRecvWait
139                                         * structure.
140                                         */
141            struct PRWaitGroup *group;
142            struct TimerEvent *timer;
143            DWORD error;
144        } mw;
145    } data;
146} _MDOverlapped;
147
148struct _MDThread {
149        /* The overlapped structure must be first! */
150    struct _MDOverlapped overlapped;    /* Used for async IO for this thread */
151    void            *acceptex_buf;      /* Used for AcceptEx() */
152    TRANSMIT_FILE_BUFFERS *xmit_bufs;   /* Used for TransmitFile() */
153    HANDLE           blocked_sema;      /* Threads block on this when waiting
154                                         * for IO or CondVar.
155                                         */
156    PRInt32          blocked_io_status; /* Status of the completed IO */
157    PRInt32          blocked_io_bytes;  /* Bytes transferred for completed IO */
158    PRInt32          blocked_io_error;  /* Save error if status is FALSE */
159    HANDLE           handle;
160    PRUint32         id;
161    void            *sp;                /* only valid when suspended */
162    PRUint32         magic;             /* for debugging */
163    PR_CONTEXT_TYPE  gcContext;         /* Thread context for GC */
164	struct _PRCPU    *thr_bound_cpu;		/* thread bound to cpu */
165	PRBool   		 interrupt_disabled;/* thread cannot be interrupted */
166	HANDLE 			 thr_event;			/* For native-threads-only support,
167											thread blocks on this event		*/
168
169    /* The following are used only if this is a fiber */
170    void            *fiber_id;          /* flag whether or not this is a fiber*/
171    FiberFunc        fiber_fn;          /* main fiber routine */
172    void            *fiber_arg;         /* arg to main fiber routine */
173    PRUint32         fiber_stacksize;   /* stacksize for fiber */
174    PRInt32          fiber_last_error;  /* last error for the fiber */
175    void (*start)(void *);              /* used by _PR_MD_CREATE_THREAD to
176                                         * pass its 'start' argument to
177                                         * pr_root. */
178};
179
180struct _MDThreadStack {
181    PRUint32           magic;          /* for debugging */
182};
183
184struct _MDSegment {
185    PRUint32           magic;          /* for debugging */
186};
187
188#undef PROFILE_LOCKS
189
190struct _MDLock {
191    CRITICAL_SECTION mutex;          /* this is recursive on NT */
192#ifdef PROFILE_LOCKS
193    PRInt32 hitcount;
194    PRInt32 misscount;
195#endif
196};
197
198struct _MDDir {
199    HANDLE           d_hdl;
200    WIN32_FIND_DATA  d_entry;
201    PRBool           firstEntry;     /* Is this the entry returned
202                                      * by FindFirstFile()? */
203    PRUint32         magic;          /* for debugging */
204};
205
206struct _MDCVar {
207    PRUint32         unused;
208};
209
210struct _MDSemaphore {
211    HANDLE           sem;
212};
213
214struct _MDFileDesc {
215    PRInt32 osfd;    /* The osfd can come from one of three spaces:
216                      * - For stdin, stdout, and stderr, we are using
217                      *   the libc file handle (0, 1, 2), which is an int.
218                      * - For files and pipes, we are using Win32 HANDLE,
219                      *   which is a void*.
220                      * - For sockets, we are using Winsock SOCKET, which
221                      *   is a u_int.
222                      */
223    PRBool io_model_committed;  /* The io model (blocking or nonblocking)
224                                 * for this osfd has been committed and
225                                 * cannot be changed.  The osfd has been
226                                 * either associated with the io
227                                 * completion port or made nonblocking. */
228    PRBool sync_file_io;        /* Use synchronous file I/O on the osfd
229                                 * (a file handle) */
230    PRBool accepted_socket;     /* Is this an accepted socket (on the
231                                 * server side)? */
232    PRNetAddr peer_addr;        /* If this is an accepted socket, cache
233                                 * the peer's address returned by
234                                 * AcceptEx().  This is to work around
235                                 * the bug that getpeername() on an
236                                 * socket accepted by AcceptEx() returns
237                                 * an all-zero net address. */
238};
239
240struct _MDProcess {
241    HANDLE handle;
242    DWORD id;
243};
244
245
246/* --- Misc stuff --- */
247#define _MD_GET_SP(thread)            (thread)->md.gcContext[6]
248
249/* --- NT security stuff --- */
250
251extern void _PR_NT_InitSids(void);
252extern void _PR_NT_FreeSids(void);
253extern PRStatus _PR_NT_MakeSecurityDescriptorACL(
254    PRIntn mode,
255    DWORD accessTable[],
256    PSECURITY_DESCRIPTOR *resultSD,
257    PACL *resultACL
258);
259extern void _PR_NT_FreeSecurityDescriptorACL(
260    PSECURITY_DESCRIPTOR pSD, PACL pACL);
261
262/* --- IO stuff --- */
263
264extern PRInt32 _md_Associate(HANDLE);
265extern PRInt32 _PR_MD_CLOSE(PRInt32 osfd, PRBool socket);
266
267#define _MD_OPEN                      _PR_MD_OPEN
268#define _MD_OPEN_FILE                 _PR_MD_OPEN_FILE
269#define _MD_READ                      _PR_MD_READ
270#define _MD_WRITE                     _PR_MD_WRITE
271#define _MD_WRITEV                    _PR_MD_WRITEV
272#define _MD_LSEEK                     _PR_MD_LSEEK
273#define _MD_LSEEK64                   _PR_MD_LSEEK64
274#define _MD_CLOSE_FILE(f)             _PR_MD_CLOSE(f, PR_FALSE)
275#define _MD_GETFILEINFO               _PR_MD_GETFILEINFO
276#define _MD_GETFILEINFO64             _PR_MD_GETFILEINFO64
277#define _MD_GETOPENFILEINFO           _PR_MD_GETOPENFILEINFO
278#define _MD_GETOPENFILEINFO64         _PR_MD_GETOPENFILEINFO64
279#define _MD_STAT                      _PR_MD_STAT
280#define _MD_RENAME                    _PR_MD_RENAME
281#define _MD_ACCESS                    _PR_MD_ACCESS
282#define _MD_DELETE                    _PR_MD_DELETE
283#define _MD_MKDIR                     _PR_MD_MKDIR
284#define _MD_MAKE_DIR                  _PR_MD_MAKE_DIR
285#define _MD_RMDIR                     _PR_MD_RMDIR
286#define _MD_LOCKFILE                  _PR_MD_LOCKFILE
287#define _MD_TLOCKFILE                 _PR_MD_TLOCKFILE
288#define _MD_UNLOCKFILE                _PR_MD_UNLOCKFILE
289
290/* --- Socket IO stuff --- */
291#define _MD_GET_SOCKET_ERROR()    WSAGetLastError()
292#define _MD_SET_SOCKET_ERROR(_err) WSASetLastError(_err)
293
294#define _MD_INIT_FILEDESC(fd)
295#define _MD_MAKE_NONBLOCK             _PR_MD_MAKE_NONBLOCK
296#define _MD_INIT_FD_INHERITABLE       _PR_MD_INIT_FD_INHERITABLE
297#define _MD_QUERY_FD_INHERITABLE      _PR_MD_QUERY_FD_INHERITABLE
298#define _MD_SHUTDOWN                  _PR_MD_SHUTDOWN
299#define _MD_LISTEN                    _PR_MD_LISTEN
300#define _MD_CLOSE_SOCKET(s)           _PR_MD_CLOSE(s, PR_TRUE)
301#define _MD_SENDTO                    _PR_MD_SENDTO
302#define _MD_RECVFROM                  _PR_MD_RECVFROM
303#define _MD_SOCKETPAIR(s, type, proto, sv) -1
304#define _MD_GETSOCKNAME               _PR_MD_GETSOCKNAME
305#define _MD_GETPEERNAME               _PR_MD_GETPEERNAME
306#define _MD_GETSOCKOPT                _PR_MD_GETSOCKOPT
307#define _MD_SETSOCKOPT                _PR_MD_SETSOCKOPT
308#define _MD_SELECT                    select
309extern int _PR_NTFiberSafeSelect(int, fd_set *, fd_set *, fd_set *,
310    const struct timeval *);
311#define _MD_FSYNC                     _PR_MD_FSYNC
312#define _MD_SOCKETAVAILABLE           _PR_MD_SOCKETAVAILABLE
313#define _MD_PIPEAVAILABLE             _PR_MD_PIPEAVAILABLE
314#define _MD_SET_FD_INHERITABLE        _PR_MD_SET_FD_INHERITABLE
315
316#define _MD_INIT_ATOMIC()
317#if defined(_M_IX86) || defined(_X86_)
318#define _MD_ATOMIC_INCREMENT          _PR_MD_ATOMIC_INCREMENT
319#define _MD_ATOMIC_ADD          	  _PR_MD_ATOMIC_ADD
320#define _MD_ATOMIC_DECREMENT          _PR_MD_ATOMIC_DECREMENT
321#else /* non-x86 processors */
322#define _MD_ATOMIC_INCREMENT(x)       InterlockedIncrement((PLONG)x)
323#define _MD_ATOMIC_ADD(ptr,val)    (InterlockedExchangeAdd((PLONG)ptr, (LONG)val) + val)
324#define _MD_ATOMIC_DECREMENT(x)       InterlockedDecrement((PLONG)x)
325#endif /* x86 */
326#define _MD_ATOMIC_SET(x,y)           InterlockedExchange((PLONG)x, (LONG)y)
327
328#define _MD_INIT_IO                   _PR_MD_INIT_IO
329#define _MD_SOCKET                    _PR_MD_SOCKET
330#define _MD_CONNECT                   _PR_MD_CONNECT
331
332#define _MD_ACCEPT(s, a, l, to)       \
333        _MD_FAST_ACCEPT(s, a, l, to, PR_FALSE, NULL, NULL)
334#define _MD_FAST_ACCEPT(s, a, l, to, fast, cb, cba) \
335        _PR_MD_FAST_ACCEPT(s, a, l, to, fast, cb, cba)
336#define _MD_ACCEPT_READ(s, ns, ra, buf, l, t) \
337        _MD_FAST_ACCEPT_READ(s, ns, ra, buf, l, t, PR_FALSE, NULL, NULL)
338#define _MD_FAST_ACCEPT_READ(s, ns, ra, buf, l, t, fast, cb, cba) \
339        _PR_MD_FAST_ACCEPT_READ(s, ns, ra, buf, l, t, fast, cb, cba)
340#define _MD_UPDATE_ACCEPT_CONTEXT     _PR_MD_UPDATE_ACCEPT_CONTEXT
341
342#define _MD_BIND                      _PR_MD_BIND
343#define _MD_RECV                      _PR_MD_RECV
344#define _MD_SEND                      _PR_MD_SEND
345#define _MD_SENDFILE              	  _PR_MD_SENDFILE
346#define _MD_PR_POLL                   _PR_MD_PR_POLL
347
348/* --- Scheduler stuff --- */
349#define _MD_PAUSE_CPU                   _PR_MD_PAUSE_CPU
350
351/* --- DIR stuff --- */
352#define PR_DIRECTORY_SEPARATOR        '\\'
353#define PR_DIRECTORY_SEPARATOR_STR    "\\"
354#define PR_PATH_SEPARATOR		';'
355#define PR_PATH_SEPARATOR_STR		";"
356#define _MD_ERRNO()                   GetLastError()
357#define _MD_OPEN_DIR                  _PR_MD_OPEN_DIR
358#define _MD_CLOSE_DIR                 _PR_MD_CLOSE_DIR
359#define _MD_READ_DIR                  _PR_MD_READ_DIR
360
361/* --- Segment stuff --- */
362#define _MD_INIT_SEGS()
363#define _MD_ALLOC_SEGMENT(seg, size, vaddr)   0
364#define _MD_FREE_SEGMENT(seg)
365
366/* --- Environment Stuff --- */
367#define _MD_GET_ENV                 _PR_MD_GET_ENV
368#define _MD_PUT_ENV                 _PR_MD_PUT_ENV
369
370/* --- Threading Stuff --- */
371#define _MD_DEFAULT_STACK_SIZE            0
372#define _MD_INIT_THREAD             _PR_MD_INIT_THREAD
373#define _MD_INIT_ATTACHED_THREAD    _PR_MD_INIT_THREAD
374#define _MD_CREATE_THREAD           _PR_MD_CREATE_THREAD
375#define _MD_JOIN_THREAD             _PR_MD_JOIN_THREAD
376#define _MD_END_THREAD              _PR_MD_END_THREAD
377#define _MD_YIELD                   _PR_MD_YIELD
378#define _MD_SET_PRIORITY            _PR_MD_SET_PRIORITY
379#define _MD_CLEAN_THREAD            _PR_MD_CLEAN_THREAD
380#define _MD_SETTHREADAFFINITYMASK   _PR_MD_SETTHREADAFFINITYMASK
381#define _MD_GETTHREADAFFINITYMASK   _PR_MD_GETTHREADAFFINITYMASK
382#define _MD_EXIT_THREAD             _PR_MD_EXIT_THREAD
383#define _MD_SUSPEND_THREAD          _PR_MD_SUSPEND_THREAD
384#define _MD_RESUME_THREAD           _PR_MD_RESUME_THREAD
385#define _MD_SUSPEND_CPU             _PR_MD_SUSPEND_CPU
386#define _MD_RESUME_CPU              _PR_MD_RESUME_CPU
387#define _MD_BEGIN_SUSPEND_ALL()
388#define _MD_BEGIN_RESUME_ALL()
389#define _MD_END_SUSPEND_ALL()
390#define _MD_END_RESUME_ALL()
391
392extern void _PR_Unblock_IO_Wait(PRThread *thr);
393
394/* --- Lock stuff --- */
395#define _MD_NEW_LOCK(lock)            (InitializeCriticalSection(&((lock)->mutex)),PR_SUCCESS)
396#define _MD_FREE_LOCK(lock)           DeleteCriticalSection(&((lock)->mutex))
397#ifndef PROFILE_LOCKS
398#define _MD_LOCK(lock)                EnterCriticalSection(&((lock)->mutex))
399#define _MD_TEST_AND_LOCK(lock)       (TryEnterCriticalSection(&((lock)->mutex))== FALSE)
400#define _MD_UNLOCK(lock)              LeaveCriticalSection(&((lock)->mutex))
401#else
402#define _MD_LOCK(lock)                 \
403    PR_BEGIN_MACRO \
404    BOOL rv = TryEnterCriticalSection(&((lock)->mutex)); \
405    if (rv == TRUE) { \
406        InterlockedIncrement(&((lock)->hitcount)); \
407    } else { \
408        InterlockedIncrement(&((lock)->misscount)); \
409        EnterCriticalSection(&((lock)->mutex)); \
410    } \
411    PR_END_MACRO
412#define _MD_TEST_AND_LOCK(lock)       0  /* XXXMB */
413#define _MD_UNLOCK(lock)              LeaveCriticalSection(&((lock)->mutex))
414#endif
415#define _PR_LOCK                      _MD_LOCK
416#define _PR_UNLOCK					  _MD_UNLOCK
417
418/* --- lock and cv waiting --- */
419#define _MD_WAIT                      _PR_MD_WAIT
420#define _MD_WAKEUP_WAITER             _PR_MD_WAKEUP_WAITER
421
422   /* XXXMB- the IOQ stuff is certainly not working correctly yet. */
423extern  struct _MDLock              _pr_ioq_lock;
424#define _MD_IOQ_LOCK()                _MD_LOCK(&_pr_ioq_lock)
425#define _MD_IOQ_UNLOCK()              _MD_UNLOCK(&_pr_ioq_lock)
426
427
428/* --- Initialization stuff --- */
429#define _MD_START_INTERRUPTS()
430#define _MD_STOP_INTERRUPTS()
431#define _MD_DISABLE_CLOCK_INTERRUPTS()
432#define _MD_ENABLE_CLOCK_INTERRUPTS()
433#define _MD_BLOCK_CLOCK_INTERRUPTS()
434#define _MD_UNBLOCK_CLOCK_INTERRUPTS()
435#define _MD_EARLY_INIT                _PR_MD_EARLY_INIT
436#define _MD_FINAL_INIT()
437#define _MD_INIT_CPUS()
438#define _MD_INIT_RUNNING_CPU(cpu)
439
440struct PRProcess;
441struct PRProcessAttr;
442
443/* --- Create a new process --- */
444#define _MD_CREATE_PROCESS _PR_CreateWindowsProcess
445extern struct PRProcess * _PR_CreateWindowsProcess(
446    const char *path,
447    char *const *argv,
448    char *const *envp,
449    const struct PRProcessAttr *attr
450);
451
452#define _MD_DETACH_PROCESS _PR_DetachWindowsProcess
453extern PRStatus _PR_DetachWindowsProcess(struct PRProcess *process);
454
455/* --- Wait for a child process to terminate --- */
456#define _MD_WAIT_PROCESS _PR_WaitWindowsProcess
457extern PRStatus _PR_WaitWindowsProcess(struct PRProcess *process,
458    PRInt32 *exitCode);
459
460#define _MD_KILL_PROCESS _PR_KillWindowsProcess
461extern PRStatus _PR_KillWindowsProcess(struct PRProcess *process);
462
463/* --- User Threading stuff --- */
464#define HAVE_FIBERS
465#define _MD_CREATE_USER_THREAD            _PR_MD_CREATE_USER_THREAD
466#define _MD_CREATE_PRIMORDIAL_USER_THREAD _PR_MD_CREATE_PRIMORDIAL_USER_THREAD
467#define _MD_CLEANUP_BEFORE_EXIT           _PR_MD_CLEANUP_BEFORE_EXIT
468#define _MD_EXIT                          _PR_MD_EXIT
469#define _MD_INIT_CONTEXT                  _PR_MD_INIT_CONTEXT
470#define _MD_SWITCH_CONTEXT                _PR_MD_SWITCH_CONTEXT
471#define _MD_RESTORE_CONTEXT               _PR_MD_RESTORE_CONTEXT
472
473/* --- Intervals --- */
474#define _MD_INTERVAL_INIT                 _PR_MD_INTERVAL_INIT
475#define _MD_GET_INTERVAL                  _PR_MD_GET_INTERVAL
476#define _MD_INTERVAL_PER_SEC              _PR_MD_INTERVAL_PER_SEC
477#define _MD_INTERVAL_PER_MILLISEC()       (_PR_MD_INTERVAL_PER_SEC() / 1000)
478#define _MD_INTERVAL_PER_MICROSEC()       (_PR_MD_INTERVAL_PER_SEC() / 1000000)
479
480/* --- Time --- */
481extern void _PR_FileTimeToPRTime(const FILETIME *filetime, PRTime *prtm);
482
483/* --- Native-Thread Specific Definitions ------------------------------- */
484
485extern BOOL _pr_use_static_tls;
486
487extern __declspec(thread) struct PRThread *_pr_current_fiber;
488extern DWORD _pr_currentFiberIndex;
489
490#define _MD_GET_ATTACHED_THREAD() \
491    (_pr_use_static_tls ? _pr_current_fiber \
492    : (PRThread *) TlsGetValue(_pr_currentFiberIndex))
493
494extern struct PRThread * _MD_CURRENT_THREAD(void);
495
496#define _MD_SET_CURRENT_THREAD(_thread) \
497    PR_BEGIN_MACRO \
498        if (_pr_use_static_tls) { \
499            _pr_current_fiber = (_thread); \
500        } else { \
501            TlsSetValue(_pr_currentFiberIndex, (_thread)); \
502        } \
503    PR_END_MACRO
504
505extern __declspec(thread) struct PRThread *_pr_fiber_last_run;
506extern DWORD _pr_lastFiberIndex;
507
508#define _MD_LAST_THREAD() \
509    (_pr_use_static_tls ? _pr_fiber_last_run \
510    : (PRThread *) TlsGetValue(_pr_lastFiberIndex))
511
512#define _MD_SET_LAST_THREAD(_thread) \
513    PR_BEGIN_MACRO \
514        if (_pr_use_static_tls) { \
515            _pr_fiber_last_run = (_thread); \
516        } else { \
517            TlsSetValue(_pr_lastFiberIndex, (_thread)); \
518        } \
519    PR_END_MACRO
520
521extern __declspec(thread) struct _PRCPU *_pr_current_cpu;
522extern DWORD _pr_currentCPUIndex;
523
524#define _MD_CURRENT_CPU() \
525    (_pr_use_static_tls ? _pr_current_cpu \
526    : (struct _PRCPU *) TlsGetValue(_pr_currentCPUIndex))
527
528#define _MD_SET_CURRENT_CPU(_cpu) \
529    PR_BEGIN_MACRO \
530        if (_pr_use_static_tls) { \
531            _pr_current_cpu = (_cpu); \
532        } else { \
533            TlsSetValue(_pr_currentCPUIndex, (_cpu)); \
534        } \
535    PR_END_MACRO
536
537extern __declspec(thread) PRUintn _pr_ints_off;
538extern DWORD _pr_intsOffIndex;
539
540#define _MD_GET_INTSOFF() \
541    (_pr_use_static_tls ? _pr_ints_off \
542    : (PRUintn) TlsGetValue(_pr_intsOffIndex))
543
544#define _MD_SET_INTSOFF(_val) \
545    PR_BEGIN_MACRO \
546        if (_pr_use_static_tls) { \
547            _pr_ints_off = (_val); \
548        } else { \
549            TlsSetValue(_pr_intsOffIndex, (LPVOID) (_val)); \
550        } \
551    PR_END_MACRO
552
553/* --- Initialization stuff --- */
554#define _MD_INIT_LOCKS()
555
556/* --- Stack stuff --- */
557#define _MD_INIT_STACK(stack, redzone)
558#define _MD_CLEAR_STACK(stack)
559
560/* --- Memory-mapped files stuff --- */
561
562struct _MDFileMap {
563    HANDLE hFileMap;
564    DWORD dwAccess;
565};
566
567extern PRStatus _MD_CreateFileMap(struct PRFileMap *fmap, PRInt64 size);
568#define _MD_CREATE_FILE_MAP _MD_CreateFileMap
569
570extern PRInt32 _MD_GetMemMapAlignment(void);
571#define _MD_GET_MEM_MAP_ALIGNMENT _MD_GetMemMapAlignment
572
573extern void * _MD_MemMap(struct PRFileMap *fmap, PRInt64 offset,
574        PRUint32 len);
575#define _MD_MEM_MAP _MD_MemMap
576
577extern PRStatus _MD_MemUnmap(void *addr, PRUint32 size);
578#define _MD_MEM_UNMAP _MD_MemUnmap
579
580extern PRStatus _MD_CloseFileMap(struct PRFileMap *fmap);
581#define _MD_CLOSE_FILE_MAP _MD_CloseFileMap
582
583/* --- Named semaphores stuff --- */
584#define _PR_HAVE_NAMED_SEMAPHORES
585#define _MD_OPEN_SEMAPHORE            _PR_MD_OPEN_SEMAPHORE
586#define _MD_WAIT_SEMAPHORE            _PR_MD_WAIT_SEMAPHORE
587#define _MD_POST_SEMAPHORE            _PR_MD_POST_SEMAPHORE
588#define _MD_CLOSE_SEMAPHORE           _PR_MD_CLOSE_SEMAPHORE
589#define _MD_DELETE_SEMAPHORE(name)    PR_SUCCESS  /* no op */
590
591#endif /* nspr_win32_defs_h___ */
592