1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef primpl_h___
7 #define primpl_h___
8 
9 #if defined(_PR_PTHREADS)
10 #include <pthread.h>
11 #endif
12 
13 #if defined(_PR_BTHREADS)
14 #include <kernel/OS.h>
15 #endif
16 
17 #ifdef WIN32
18 /*
19  * Allow use of functions and symbols first defined in Win2k.
20  */
21 #if !defined(WINVER) || (WINVER < 0x0500)
22 #undef WINVER
23 #define WINVER 0x0500
24 #endif
25 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
26 #undef _WIN32_WINNT
27 #define _WIN32_WINNT 0x0500
28 #endif
29 #endif /* WIN32 */
30 
31 #include "nspr.h"
32 #include "prpriv.h"
33 
34 typedef struct PRSegment PRSegment;
35 
36 #include "md/prosdep.h"
37 #include "obsolete/probslet.h"
38 
39 #ifdef _PR_HAVE_POSIX_SEMAPHORES
40 #include <semaphore.h>
41 #elif defined(_PR_HAVE_SYSV_SEMAPHORES)
42 #include <sys/sem.h>
43 #endif
44 
45 #ifdef HAVE_SYSCALL
46 #include <sys/syscall.h>
47 #endif
48 
49 /*************************************************************************
50 *****  A Word about Model Dependent Function Naming Convention ***********
51 *************************************************************************/
52 
53 /*
54 NSPR 2.0 must implement its function across a range of platforms
55 including: MAC, Windows/16, Windows/95, Windows/NT, and several
56 variants of Unix. Each implementation shares common code as well
57 as having platform dependent portions. This standard describes how
58 the model dependent portions are to be implemented.
59 
60 In header file pr/include/primpl.h, each publicly declared
61 platform dependent function is declared as:
62 
63 NSPR_API void _PR_MD_FUNCTION( long arg1, long arg2 );
64 #define _PR_MD_FUNCTION _MD_FUNCTION
65 
66 In header file pr/include/md/<platform>/_<platform>.h,
67 each #define'd macro is redefined as one of:
68 
69 #define _MD_FUNCTION <blanks>
70 #define _MD_FUNCTION <expanded macro>
71 #define _MD_FUNCTION <osFunction>
72 #define _MD_FUNCTION <_MD_Function>
73 
74 Where:
75 
76 <blanks> is no definition at all. In this case, the function is not implemented
77 and is never called for this platform.
78 For example:
79 #define _MD_INIT_CPUS()
80 
81 <expanded macro> is a C language macro expansion.
82 For example:
83 #define        _MD_CLEAN_THREAD(_thread) \
84     PR_BEGIN_MACRO \
85         PR_DestroyCondVar(_thread->md.asyncIOCVar); \
86         PR_DestroyLock(_thread->md.asyncIOLock); \
87     PR_END_MACRO
88 
89 <osFunction> is some function implemented by the host operating system.
90 For example:
91 #define _MD_EXIT        exit
92 
93 <_MD_function> is the name of a function implemented for this platform in
94 pr/src/md/<platform>/<soruce>.c file.
95 For example:
96 #define        _MD_GETFILEINFO         _MD_GetFileInfo
97 
98 In <source>.c, the implementation is:
99 PR_IMPLEMENT(PRInt32) _MD_GetFileInfo(const char *fn, PRFileInfo *info);
100 */
101 
102 PR_BEGIN_EXTERN_C
103 
104 typedef struct _MDLock _MDLock;
105 typedef struct _MDCVar _MDCVar;
106 typedef struct _MDSegment _MDSegment;
107 typedef struct _MDThread _MDThread;
108 typedef struct _MDThreadStack _MDThreadStack;
109 typedef struct _MDSemaphore _MDSemaphore;
110 typedef struct _MDDir _MDDir;
111 #ifdef MOZ_UNICODE
112 typedef struct _MDDirUTF16 _MDDirUTF16;
113 #endif /* MOZ_UNICODE */
114 typedef struct _MDFileDesc _MDFileDesc;
115 typedef struct _MDProcess _MDProcess;
116 typedef struct _MDFileMap _MDFileMap;
117 
118 #if defined(_PR_PTHREADS)
119 
120 /*
121 ** The following definitions are unique to implementing NSPR using pthreads.
122 ** Since pthreads defines most of the thread and thread synchronization
123 ** stuff, this is a pretty small set.
124 */
125 
126 #define PT_CV_NOTIFIED_LENGTH 6
127 typedef struct _PT_Notified _PT_Notified;
128 struct _PT_Notified
129 {
130     PRIntn length;              /* # of used entries in this structure */
131     struct
132     {
133         PRCondVar *cv;          /* the condition variable notified */
134         PRIntn times;           /* and the number of times notified */
135     } cv[PT_CV_NOTIFIED_LENGTH];
136     _PT_Notified *link;         /* link to another of these | NULL */
137 };
138 
139 /*
140  * bits defined for pthreads 'state' field
141  */
142 #define PT_THREAD_DETACHED  0x01    /* thread can't be joined */
143 #define PT_THREAD_GLOBAL    0x02    /* a global thread (unlikely) */
144 #define PT_THREAD_SYSTEM    0x04    /* system (not user) thread */
145 #define PT_THREAD_PRIMORD   0x08    /* this is the primordial thread */
146 #define PT_THREAD_ABORTED   0x10    /* thread has been interrupted */
147 #define PT_THREAD_GCABLE    0x20    /* thread is garbage collectible */
148 #define PT_THREAD_SUSPENDED 0x40    /* thread has been suspended */
149 #define PT_THREAD_FOREIGN   0x80    /* thread is not one of ours */
150 #define PT_THREAD_BOUND     0x100    /* a bound-global thread */
151 
152 #define _PT_THREAD_INTERRUPTED(thr)					\
153 		(!(thr->interrupt_blocked) && (thr->state & PT_THREAD_ABORTED))
154 #define _PT_THREAD_BLOCK_INTERRUPT(thr)				\
155 		(thr->interrupt_blocked = 1)
156 #define _PT_THREAD_UNBLOCK_INTERRUPT(thr)			\
157 		(thr->interrupt_blocked = 0)
158 
159 #define _PT_IS_GCABLE_THREAD(thr) ((thr)->state & PT_THREAD_GCABLE)
160 
161 /*
162 ** Possible values for thread's suspend field
163 ** Note that the first two can be the same as they are really mutually exclusive,
164 ** i.e. both cannot be happening at the same time. We have two symbolic names
165 ** just as a mnemonic.
166 **/
167 #define PT_THREAD_RESUMED   0x80    /* thread has been resumed */
168 #define PT_THREAD_SETGCABLE 0x100   /* set the GCAble flag */
169 
170 #if defined(DEBUG)
171 
172 typedef struct PTDebug
173 {
174     PRTime timeStarted;
175     PRUintn locks_created, locks_destroyed;
176     PRUintn locks_acquired, locks_released;
177     PRUintn cvars_created, cvars_destroyed;
178     PRUintn cvars_notified, delayed_cv_deletes;
179 } PTDebug;
180 
181 #endif /* defined(DEBUG) */
182 
183 NSPR_API(void) PT_FPrintStats(PRFileDesc *fd, const char *msg);
184 
185 /*
186  * On Linux and its derivatives POSIX priority scheduling works only for
187  * real-time threads. On those platforms we set thread's nice values
188  * instead which requires us to track kernel thread IDs for each POSIX
189  * thread we create.
190  */
191 #if defined(LINUX) && defined(HAVE_SETPRIORITY) && \
192     ((defined(HAVE_SYSCALL) && defined(SYS_gettid)) || defined(HAVE_GETTID))
193 #define _PR_NICE_PRIORITY_SCHEDULING
194 #endif
195 
196 #else /* defined(_PR_PTHREADS) */
197 
198 NSPR_API(void) PT_FPrintStats(PRFileDesc *fd, const char *msg);
199 
200 /*
201 ** This section is contains those parts needed to implement NSPR on
202 ** platforms in general. One would assume that the pthreads implementation
203 ** included lots of the same types, at least conceptually.
204 */
205 
206 /*
207  * Local threads only.  No multiple CPU support and hence all the
208  * following routines are no-op.
209  */
210 #ifdef _PR_LOCAL_THREADS_ONLY
211 
212 #define        _PR_MD_SUSPEND_THREAD(thread)
213 #define        _PR_MD_RESUME_THREAD(thread)
214 #define        _PR_MD_SUSPEND_CPU(cpu)
215 #define        _PR_MD_RESUME_CPU(cpu)
216 #define        _PR_MD_BEGIN_SUSPEND_ALL()
217 #define        _PR_MD_END_SUSPEND_ALL()
218 #define        _PR_MD_BEGIN_RESUME_ALL()
219 #define        _PR_MD_END_RESUME_ALL()
220 #define _PR_MD_INIT_ATTACHED_THREAD(thread) PR_FAILURE
221 
222 #endif
223 
224 typedef struct _PRCPUQueue _PRCPUQueue;
225 typedef struct _PRCPU _PRCPU;
226 typedef struct _MDCPU _MDCPU;
227 
228 struct _PRCPUQueue {
229     _MDLock  runQLock;          /* lock for the run + wait queues */
230     _MDLock  sleepQLock;        /* lock for the run + wait queues */
231     _MDLock  miscQLock;         /* lock for the run + wait queues */
232 
233     PRCList runQ[PR_PRIORITY_LAST + 1]; /* run queue for this CPU */
234     PRUint32  runQReadyMask;
235     PRCList sleepQ;
236     PRIntervalTime sleepQmax;
237     PRCList pauseQ;
238     PRCList suspendQ;
239     PRCList waitingToJoinQ;
240 
241     PRUintn   numCPUs;          /* number of CPUs using this Q */
242 };
243 
244 struct _PRCPU {
245     PRCList links;              /* link list of CPUs */
246     PRUint32 id;                /* id for this CPU */
247 
248     union {
249         PRInt32 bits;
250         PRUint8 missed[4];
251     } u;
252     PRIntn where;               /* index into u.missed */
253     PRPackedBool paused;        /* cpu is paused */
254     PRPackedBool exit;          /* cpu should exit */
255 
256     PRThread *thread;           /* native thread for this CPUThread */
257     PRThread *idle_thread;      /* user-level idle thread for this CPUThread */
258 
259     PRIntervalTime last_clock;  /* the last time we went into
260                                  * _PR_ClockInterrupt() on this CPU
261                                  */
262 
263     _PRCPUQueue *queue;
264 
265     _MDCPU md;
266 };
267 
268 typedef struct _PRInterruptTable {
269     const char *name;
270     PRUintn missed_bit;
271     void (*handler)(void);
272 } _PRInterruptTable;
273 
274 #define _PR_CPU_PTR(_qp) \
275     ((_PRCPU*) ((char*) (_qp) - offsetof(_PRCPU,links)))
276 
277 #if !defined(IRIX) && !defined(WIN32) && !defined(XP_OS2) \
278         && !(defined(SOLARIS) && defined(_PR_GLOBAL_THREADS_ONLY))
279 #define _MD_GET_ATTACHED_THREAD()        (_PR_MD_CURRENT_THREAD())
280 #endif
281 
282 #ifdef _PR_LOCAL_THREADS_ONLY
283 
284 NSPR_API(struct _PRCPU *)              _pr_currentCPU;
285 NSPR_API(PRThread *)                   _pr_currentThread;
286 NSPR_API(PRThread *)                   _pr_lastThread;
287 NSPR_API(PRInt32)                      _pr_intsOff;
288 
289 #define _MD_CURRENT_CPU()               (_pr_currentCPU)
290 #define _MD_SET_CURRENT_CPU(_cpu)       (_pr_currentCPU = (_cpu))
291 #define _MD_CURRENT_THREAD()            (_pr_currentThread)
292 #define _MD_SET_CURRENT_THREAD(_thread) (_pr_currentThread = (_thread))
293 #define _MD_LAST_THREAD()               (_pr_lastThread)
294 #define _MD_SET_LAST_THREAD(t)          (_pr_lastThread = t)
295 
296 #define _MD_GET_INTSOFF()               (_pr_intsOff)
297 #define _MD_SET_INTSOFF(_val)           (_pr_intsOff = _val)
298 
299 
300 /* The unbalanced curly braces in these two macros are intentional */
301 #define _PR_LOCK_HEAP() { PRIntn _is; if (_pr_currentCPU) _PR_INTSOFF(_is);
302 #define _PR_UNLOCK_HEAP() if (_pr_currentCPU) _PR_INTSON(_is); }
303 
304 #endif /* _PR_LOCAL_THREADS_ONLY */
305 
306 extern PRInt32                  _native_threads_only;
307 
308 #if defined(_PR_GLOBAL_THREADS_ONLY)
309 
310 #define _MD_GET_INTSOFF() 0
311 #define _MD_SET_INTSOFF(_val)
312 #define _PR_INTSOFF(_is)
313 #define _PR_FAST_INTSON(_is)
314 #define _PR_INTSON(_is)
315 #define _PR_THREAD_LOCK(_thread)
316 #define _PR_THREAD_UNLOCK(_thread)
317 #define _PR_RUNQ_LOCK(cpu)
318 #define _PR_RUNQ_UNLOCK(cpu)
319 #define _PR_SLEEPQ_LOCK(thread)
320 #define _PR_SLEEPQ_UNLOCK(thread)
321 #define _PR_MISCQ_LOCK(thread)
322 #define _PR_MISCQ_UNLOCK(thread)
323 #define _PR_CPU_LIST_LOCK()
324 #define _PR_CPU_LIST_UNLOCK()
325 
326 #define _PR_ADD_RUNQ(_thread, _cpu, _pri)
327 #define _PR_DEL_RUNQ(_thread)
328 #define _PR_ADD_SLEEPQ(_thread, _timeout)
329 #define _PR_DEL_SLEEPQ(_thread, _propogate)
330 #define _PR_ADD_JOINQ(_thread, _cpu)
331 #define _PR_DEL_JOINQ(_thread)
332 #define _PR_ADD_SUSPENDQ(_thread, _cpu)
333 #define _PR_DEL_SUSPENDQ(_thread)
334 
335 #define _PR_THREAD_SWITCH_CPU(_thread, _newCPU)
336 
337 #define _PR_IS_NATIVE_THREAD(thread) 1
338 #define _PR_IS_NATIVE_THREAD_SUPPORTED() 1
339 
340 #else
341 
342 #define _PR_INTSOFF(_is) \
343     PR_BEGIN_MACRO \
344         (_is) = _PR_MD_GET_INTSOFF(); \
345         _PR_MD_SET_INTSOFF(1); \
346     PR_END_MACRO
347 
348 #define _PR_FAST_INTSON(_is) \
349     PR_BEGIN_MACRO \
350         _PR_MD_SET_INTSOFF(_is); \
351     PR_END_MACRO
352 
353 #define _PR_INTSON(_is) \
354     PR_BEGIN_MACRO \
355         if ((_is == 0) && (_PR_MD_CURRENT_CPU())->u.bits) \
356                 _PR_IntsOn((_PR_MD_CURRENT_CPU())); \
357         _PR_MD_SET_INTSOFF(_is); \
358     PR_END_MACRO
359 
360 #ifdef _PR_LOCAL_THREADS_ONLY
361 
362 #define _PR_IS_NATIVE_THREAD(thread) 0
363 #define _PR_THREAD_LOCK(_thread)
364 #define _PR_THREAD_UNLOCK(_thread)
365 #define _PR_RUNQ_LOCK(cpu)
366 #define _PR_RUNQ_UNLOCK(cpu)
367 #define _PR_SLEEPQ_LOCK(thread)
368 #define _PR_SLEEPQ_UNLOCK(thread)
369 #define _PR_MISCQ_LOCK(thread)
370 #define _PR_MISCQ_UNLOCK(thread)
371 #define _PR_CPU_LIST_LOCK()
372 #define _PR_CPU_LIST_UNLOCK()
373 
374 #define _PR_ADD_RUNQ(_thread, _cpu, _pri) \
375     PR_BEGIN_MACRO \
376     PR_APPEND_LINK(&(_thread)->links, &_PR_RUNQ(_cpu)[_pri]); \
377     _PR_RUNQREADYMASK(_cpu) |= (1L << _pri); \
378     PR_END_MACRO
379 
380 #define _PR_DEL_RUNQ(_thread) \
381     PR_BEGIN_MACRO \
382     _PRCPU *_cpu = _thread->cpu; \
383     PRInt32 _pri = _thread->priority; \
384     PR_REMOVE_LINK(&(_thread)->links); \
385     if (PR_CLIST_IS_EMPTY(&_PR_RUNQ(_cpu)[_pri])) \
386         _PR_RUNQREADYMASK(_cpu) &= ~(1L << _pri); \
387     PR_END_MACRO
388 
389 #define _PR_ADD_SLEEPQ(_thread, _timeout) \
390     _PR_AddSleepQ(_thread, _timeout);
391 
392 #define _PR_DEL_SLEEPQ(_thread, _propogate) \
393     _PR_DelSleepQ(_thread, _propogate);
394 
395 #define _PR_ADD_JOINQ(_thread, _cpu) \
396     PR_APPEND_LINK(&(_thread)->links, &_PR_WAITINGTOJOINQ(_cpu));
397 
398 #define _PR_DEL_JOINQ(_thread) \
399     PR_REMOVE_LINK(&(_thread)->links);
400 
401 #define _PR_ADD_SUSPENDQ(_thread, _cpu) \
402     PR_APPEND_LINK(&(_thread)->links, &_PR_SUSPENDQ(_cpu));
403 
404 #define _PR_DEL_SUSPENDQ(_thread) \
405     PR_REMOVE_LINK(&(_thread)->links);
406 
407 #define _PR_THREAD_SWITCH_CPU(_thread, _newCPU)
408 
409 #define _PR_IS_NATIVE_THREAD_SUPPORTED() 0
410 
411 #else        /* _PR_LOCAL_THREADS_ONLY */
412 
413 /* These are for the "combined" thread model */
414 
415 #define _PR_THREAD_LOCK(_thread) \
416     _PR_MD_LOCK(&(_thread)->threadLock);
417 
418 #define _PR_THREAD_UNLOCK(_thread) \
419     _PR_MD_UNLOCK(&(_thread)->threadLock);
420 
421 #define _PR_RUNQ_LOCK(_cpu) \
422     PR_BEGIN_MACRO \
423     _PR_MD_LOCK(&(_cpu)->queue->runQLock );\
424     PR_END_MACRO
425 
426 #define _PR_RUNQ_UNLOCK(_cpu) \
427     PR_BEGIN_MACRO \
428     _PR_MD_UNLOCK(&(_cpu)->queue->runQLock );\
429     PR_END_MACRO
430 
431 #define _PR_SLEEPQ_LOCK(_cpu) \
432     _PR_MD_LOCK(&(_cpu)->queue->sleepQLock );
433 
434 #define _PR_SLEEPQ_UNLOCK(_cpu) \
435     _PR_MD_UNLOCK(&(_cpu)->queue->sleepQLock );
436 
437 #define _PR_MISCQ_LOCK(_cpu) \
438     _PR_MD_LOCK(&(_cpu)->queue->miscQLock );
439 
440 #define _PR_MISCQ_UNLOCK(_cpu) \
441     _PR_MD_UNLOCK(&(_cpu)->queue->miscQLock );
442 
443 #define _PR_CPU_LIST_LOCK()                 _PR_MD_LOCK(&_pr_cpuLock)
444 #define _PR_CPU_LIST_UNLOCK()               _PR_MD_UNLOCK(&_pr_cpuLock)
445 
446 #define QUEUE_RUN           0x1
447 #define QUEUE_SLEEP         0x2
448 #define QUEUE_JOIN          0x4
449 #define QUEUE_SUSPEND       0x8
450 #define QUEUE_LOCK          0x10
451 
452 #define _PR_ADD_RUNQ(_thread, _cpu, _pri) \
453     PR_BEGIN_MACRO \
454     PR_APPEND_LINK(&(_thread)->links, &_PR_RUNQ(_cpu)[_pri]); \
455     _PR_RUNQREADYMASK(_cpu) |= (1L << _pri); \
456     PR_ASSERT((_thread)->queueCount == 0); \
457     (_thread)->queueCount = QUEUE_RUN; \
458     PR_END_MACRO
459 
460 #define _PR_DEL_RUNQ(_thread) \
461     PR_BEGIN_MACRO \
462     _PRCPU *_cpu = _thread->cpu; \
463     PRInt32 _pri = _thread->priority; \
464     PR_REMOVE_LINK(&(_thread)->links); \
465     if (PR_CLIST_IS_EMPTY(&_PR_RUNQ(_cpu)[_pri])) \
466         _PR_RUNQREADYMASK(_cpu) &= ~(1L << _pri); \
467     PR_ASSERT((_thread)->queueCount == QUEUE_RUN);\
468     (_thread)->queueCount = 0; \
469     PR_END_MACRO
470 
471 #define _PR_ADD_SLEEPQ(_thread, _timeout) \
472     PR_ASSERT((_thread)->queueCount == 0); \
473     (_thread)->queueCount = QUEUE_SLEEP; \
474     _PR_AddSleepQ(_thread, _timeout);
475 
476 #define _PR_DEL_SLEEPQ(_thread, _propogate) \
477     PR_ASSERT((_thread)->queueCount == QUEUE_SLEEP);\
478     (_thread)->queueCount = 0; \
479     _PR_DelSleepQ(_thread, _propogate);
480 
481 #define _PR_ADD_JOINQ(_thread, _cpu) \
482     PR_ASSERT((_thread)->queueCount == 0); \
483     (_thread)->queueCount = QUEUE_JOIN; \
484     PR_APPEND_LINK(&(_thread)->links, &_PR_WAITINGTOJOINQ(_cpu));
485 
486 #define _PR_DEL_JOINQ(_thread) \
487     PR_ASSERT((_thread)->queueCount == QUEUE_JOIN);\
488     (_thread)->queueCount = 0; \
489     PR_REMOVE_LINK(&(_thread)->links);
490 
491 #define _PR_ADD_SUSPENDQ(_thread, _cpu) \
492     PR_ASSERT((_thread)->queueCount == 0); \
493     (_thread)->queueCount = QUEUE_SUSPEND; \
494     PR_APPEND_LINK(&(_thread)->links, &_PR_SUSPENDQ(_cpu));
495 
496 #define _PR_DEL_SUSPENDQ(_thread) \
497     PR_ASSERT((_thread)->queueCount == QUEUE_SUSPEND);\
498     (_thread)->queueCount = 0; \
499     PR_REMOVE_LINK(&(_thread)->links);
500 
501 #define _PR_THREAD_SWITCH_CPU(_thread, _newCPU) \
502     (_thread)->cpu = (_newCPU);
503 
504 #define _PR_IS_NATIVE_THREAD(thread) (thread->flags & _PR_GLOBAL_SCOPE)
505 #define _PR_IS_NATIVE_THREAD_SUPPORTED() 1
506 
507 #endif /* _PR_LOCAL_THREADS_ONLY */
508 
509 #endif /* _PR_GLOBAL_THREADS_ONLY */
510 
511 #define _PR_SET_RESCHED_FLAG() _PR_MD_CURRENT_CPU()->u.missed[3] = 1
512 #define _PR_CLEAR_RESCHED_FLAG() _PR_MD_CURRENT_CPU()->u.missed[3] = 0
513 
514 extern _PRInterruptTable _pr_interruptTable[];
515 
516 /* Bits for _pr_interruptState.u.missed[0,1] */
517 #define _PR_MISSED_CLOCK    0x1
518 #define _PR_MISSED_IO        0x2
519 #define _PR_MISSED_CHILD    0x4
520 
521 extern void _PR_IntsOn(_PRCPU *cpu);
522 
523 NSPR_API(void) _PR_WakeupCPU(void);
524 NSPR_API(void) _PR_PauseCPU(void);
525 
526 /************************************************************************/
527 
528 #define _PR_LOCK_LOCK(_lock) \
529     _PR_MD_LOCK(&(_lock)->ilock);
530 #define _PR_LOCK_UNLOCK(_lock) \
531     _PR_MD_UNLOCK(&(_lock)->ilock);
532 
533 extern void _PR_UnblockLockWaiter(PRLock *lock);
534 extern PRStatus _PR_InitLock(PRLock *lock);
535 extern void _PR_FreeLock(PRLock *lock);
536 
537 #define _PR_LOCK_PTR(_qp) \
538     ((PRLock*) ((char*) (_qp) - offsetof(PRLock,links)))
539 
540 /************************************************************************/
541 
542 #define _PR_CVAR_LOCK(_cvar) \
543     _PR_MD_LOCK(&(_cvar)->ilock);
544 #define _PR_CVAR_UNLOCK(_cvar) \
545     _PR_MD_UNLOCK(&(_cvar)->ilock);
546 
547 extern PRStatus _PR_InitCondVar(PRCondVar *cvar, PRLock *lock);
548 extern void _PR_FreeCondVar(PRCondVar *cvar);
549 extern PRStatus _PR_WaitCondVar(
550     PRThread *thread, PRCondVar *cvar, PRLock *lock, PRIntervalTime timeout);
551 extern void _PR_NotifyCondVar(PRCondVar *cvar, PRThread *me);
552 extern PRUint32 _PR_CondVarToString(PRCondVar *cvar, char *buf, PRUint32 buflen);
553 
554 NSPR_API(void) _PR_Notify(PRMonitor *mon, PRBool all, PRBool sticky);
555 
556 /* PRThread.flags */
557 #define _PR_SYSTEM          0x01
558 #define _PR_INTERRUPT       0x02
559 #define _PR_ATTACHED        0x04        /* created via PR_AttachThread */
560 #define _PR_PRIMORDIAL      0x08        /* the thread that called PR_Init */
561 #define _PR_ON_SLEEPQ       0x10        /* thread is on the sleepQ */
562 #define _PR_ON_PAUSEQ       0x20        /* thread is on the pauseQ */
563 #define _PR_SUSPENDING      0x40        /* thread wants to suspend */
564 #define _PR_GLOBAL_SCOPE    0x80        /* thread is global scope */
565 #define _PR_IDLE_THREAD     0x200       /* this is an idle thread        */
566 #define _PR_GCABLE_THREAD   0x400       /* this is a collectable thread */
567 #define _PR_BOUND_THREAD    0x800       /* a bound thread */
568 #define _PR_INTERRUPT_BLOCKED	0x1000	/* interrupts blocked */
569 
570 /* PRThread.state */
571 #define _PR_UNBORN       0
572 #define _PR_RUNNABLE     1
573 #define _PR_RUNNING      2
574 #define _PR_LOCK_WAIT    3
575 #define _PR_COND_WAIT    4
576 #define _PR_JOIN_WAIT    5
577 #define _PR_IO_WAIT      6
578 #define _PR_SUSPENDED    7
579 #define _PR_DEAD_STATE   8  /* for debugging */
580 
581 /* PRThreadStack.flags */
582 #define _PR_STACK_VM            0x1    /* using vm instead of malloc */
583 #define _PR_STACK_MAPPED        0x2    /* vm is mapped */
584 #define _PR_STACK_PRIMORDIAL    0x4    /* stack for primordial thread */
585 
586 /*
587 ** If the default stcksize from the client is zero, we need to pick a machine
588 ** dependent value.  This is only for standard user threads.  For custom threads,
589 ** 0 has a special meaning.
590 ** Adjust stackSize. Round up to a page boundary.
591 */
592 
593 #ifndef _MD_MINIMUM_STACK_SIZE
594 #define _MD_MINIMUM_STACK_SIZE	0
595 #endif
596 
597 #if (!defined(HAVE_CUSTOM_USER_THREADS))
598 #define        _PR_ADJUST_STACKSIZE(stackSize) \
599         PR_BEGIN_MACRO \
600     if (stackSize == 0) \
601                 stackSize = _MD_DEFAULT_STACK_SIZE; \
602     if (stackSize < _MD_MINIMUM_STACK_SIZE) \
603                 stackSize = _MD_MINIMUM_STACK_SIZE; \
604     stackSize = (stackSize + (1 << _pr_pageShift) - 1) >> _pr_pageShift; \
605     stackSize <<= _pr_pageShift; \
606         PR_END_MACRO
607 #else
608 #define        _PR_ADJUST_STACKSIZE(stackSize)
609 #endif
610 
611 #define _PR_IS_GCABLE_THREAD(thr) ((thr)->flags & _PR_GCABLE_THREAD)
612 
613 #define _PR_PENDING_INTERRUPT(thr)					\
614 		(!((thr)->flags & _PR_INTERRUPT_BLOCKED) && ((thr)->flags & _PR_INTERRUPT))
615 #define _PR_THREAD_BLOCK_INTERRUPT(thr)			\
616 		(thr->flags |= _PR_INTERRUPT_BLOCKED)
617 #define _PR_THREAD_UNBLOCK_INTERRUPT(thr)			\
618 		(thr->flags &= ~_PR_INTERRUPT_BLOCKED)
619 
620 #define _PR_THREAD_PTR(_qp) \
621     ((PRThread*) ((char*) (_qp) - offsetof(PRThread,links)))
622 
623 #define _PR_ACTIVE_THREAD_PTR(_qp) \
624     ((PRThread*) ((char*) (_qp) - offsetof(PRThread,active)))
625 
626 #define _PR_THREAD_CONDQ_PTR(_qp) \
627     ((PRThread*) ((char*) (_qp) - offsetof(PRThread,waitQLinks)))
628 
629 #define _PR_THREAD_MD_TO_PTR(_md) \
630     ((PRThread*) ((char*) (_md) - offsetof(PRThread,md)))
631 
632 #define _PR_THREAD_STACK_TO_PTR(_stack) \
633     ((PRThread*) (_stack->thr))
634 
635 extern PRCList _pr_active_local_threadQ;
636 extern PRCList _pr_active_global_threadQ;
637 extern PRCList _pr_cpuQ;
638 extern _MDLock  _pr_cpuLock;
639 extern PRInt32 _pr_md_idle_cpus;
640 
641 #define _PR_ACTIVE_LOCAL_THREADQ()          _pr_active_local_threadQ
642 #define _PR_ACTIVE_GLOBAL_THREADQ()         _pr_active_global_threadQ
643 #define _PR_CPUQ()                          _pr_cpuQ
644 #define _PR_RUNQ(_cpu)                      ((_cpu)->queue->runQ)
645 #define _PR_RUNQREADYMASK(_cpu)             ((_cpu)->queue->runQReadyMask)
646 #define _PR_SLEEPQ(_cpu)                    ((_cpu)->queue->sleepQ)
647 #define _PR_SLEEPQMAX(_cpu)                 ((_cpu)->queue->sleepQmax)
648 #define _PR_PAUSEQ(_cpu)                    ((_cpu)->queue->pauseQ)
649 #define _PR_SUSPENDQ(_cpu)                  ((_cpu)->queue->suspendQ)
650 #define _PR_WAITINGTOJOINQ(_cpu)            ((_cpu)->queue->waitingToJoinQ)
651 
652 extern PRUint32 _pr_recycleThreads;   /* Flag for behavior on thread cleanup */
653 extern PRLock *_pr_deadQLock;
654 extern PRUint32 _pr_numNativeDead;
655 extern PRUint32 _pr_numUserDead;
656 extern PRCList _pr_deadNativeQ;
657 extern PRCList _pr_deadUserQ;
658 #define _PR_DEADNATIVEQ     _pr_deadNativeQ
659 #define _PR_DEADUSERQ       _pr_deadUserQ
660 #define _PR_DEADQ_LOCK      PR_Lock(_pr_deadQLock);
661 #define _PR_DEADQ_UNLOCK    PR_Unlock(_pr_deadQLock);
662 #define _PR_INC_DEADNATIVE  (_pr_numNativeDead++)
663 #define _PR_DEC_DEADNATIVE  (_pr_numNativeDead--)
664 #define _PR_NUM_DEADNATIVE  (_pr_numNativeDead)
665 #define _PR_INC_DEADUSER    (_pr_numUserDead++)
666 #define _PR_DEC_DEADUSER    (_pr_numUserDead--)
667 #define _PR_NUM_DEADUSER    (_pr_numUserDead)
668 
669 extern PRUint32 _pr_utid;
670 
671 extern struct _PRCPU  *_pr_primordialCPU;
672 
673 extern PRLock *_pr_activeLock;          /* lock for userActive and systemActive */
674 extern PRInt32 _pr_userActive;          /* number of active user threads */
675 extern PRInt32 _pr_systemActive;        /* number of active system threads */
676 extern PRInt32 _pr_primordialExitCount; /* number of user threads left
677                                          * before the primordial thread
678                                          * can exit.  */
679 extern PRCondVar *_pr_primordialExitCVar; /* the condition variable for
680                                            * notifying the primordial thread
681                                            * when all other user threads
682                                            * have terminated.  */
683 
684 extern PRUintn _pr_maxPTDs;
685 
686 extern PRLock *_pr_terminationCVLock;
687 
688 /*************************************************************************
689 * Internal routines either called by PR itself or from machine-dependent *
690 * code.                                                                  *
691 *************************************************************************/
692 
693 extern void _PR_ClockInterrupt(void);
694 
695 extern void _PR_Schedule(void);
696 extern void _PR_SetThreadPriority(
697     PRThread* thread, PRThreadPriority priority);
698 
699 /***********************************************************************
700 ** FUNCTION:	_PR_NewSegment()
701 ** DESCRIPTION:
702 **   Allocate a memory segment. The "size" value is rounded up to the
703 **   native system page size and a page aligned portion of memory is
704 **   returned.  This memory is not part of the malloc heap. If "vaddr" is
705 **   not NULL then PR tries to allocate the segment at the desired virtual
706 **   address.
707 ** INPUTS:	size:  size of the desired memory segment
708 **          vaddr:  address at which the newly aquired segment is to be
709 **                  mapped into memory.
710 ** OUTPUTS:	a memory segment is allocated, a PRSegment is allocated
711 ** RETURN:	pointer to PRSegment
712 ***********************************************************************/
713 extern PRSegment* _PR_NewSegment(PRUint32 size, void *vaddr);
714 
715 /***********************************************************************
716 ** FUNCTION:	_PR_DestroySegment()
717 ** DESCRIPTION:
718 **   The memory segment and the PRSegment are freed
719 ** INPUTS:	seg:  pointer to PRSegment to be freed
720 ** OUTPUTS:	the the PRSegment and its associated memory segment are freed
721 ** RETURN:	void
722 ***********************************************************************/
723 extern void _PR_DestroySegment(PRSegment *seg);
724 
725 extern PRThreadStack * _PR_NewStack(PRUint32 stackSize);
726 extern void _PR_FreeStack(PRThreadStack *stack);
727 extern PRBool _PR_NotifyThread (PRThread *thread, PRThread *me);
728 extern void _PR_NotifyLockedThread (PRThread *thread);
729 
730 NSPR_API(void) _PR_AddSleepQ(PRThread *thread, PRIntervalTime timeout);
731 NSPR_API(void) _PR_DelSleepQ(PRThread *thread, PRBool propogate_time);
732 
733 extern void _PR_AddThreadToRunQ(PRThread *me, PRThread *thread);
734 
735 NSPR_API(PRThread*) _PR_CreateThread(PRThreadType type,
736                                      void (*start)(void *arg),
737                                      void *arg,
738                                      PRThreadPriority priority,
739                                      PRThreadScope scope,
740                                      PRThreadState state,
741                                      PRUint32 stackSize,
742                      PRUint32 flags);
743 
744 extern void _PR_NativeDestroyThread(PRThread *thread);
745 extern void _PR_UserDestroyThread(PRThread *thread);
746 
747 extern PRThread* _PRI_AttachThread(
748     PRThreadType type, PRThreadPriority priority,
749     PRThreadStack *stack, PRUint32 flags);
750 
751 extern void _PRI_DetachThread(void);
752 
753 
754 #define _PR_IO_PENDING(_thread) ((_thread)->io_pending)
755 
756 NSPR_API(void) _PR_MD_INIT_CPUS();
757 #define    _PR_MD_INIT_CPUS _MD_INIT_CPUS
758 
759 NSPR_API(void) _PR_MD_WAKEUP_CPUS();
760 #define    _PR_MD_WAKEUP_CPUS _MD_WAKEUP_CPUS
761 
762 /* Interrupts related */
763 
764 NSPR_API(void) _PR_MD_START_INTERRUPTS(void);
765 #define    _PR_MD_START_INTERRUPTS _MD_START_INTERRUPTS
766 
767 NSPR_API(void) _PR_MD_STOP_INTERRUPTS(void);
768 #define    _PR_MD_STOP_INTERRUPTS _MD_STOP_INTERRUPTS
769 
770 NSPR_API(void) _PR_MD_ENABLE_CLOCK_INTERRUPTS(void);
771 #define    _PR_MD_ENABLE_CLOCK_INTERRUPTS _MD_ENABLE_CLOCK_INTERRUPTS
772 
773 NSPR_API(void) _PR_MD_DISABLE_CLOCK_INTERRUPTS(void);
774 #define    _PR_MD_DISABLE_CLOCK_INTERRUPTS _MD_DISABLE_CLOCK_INTERRUPTS
775 
776 NSPR_API(void) _PR_MD_BLOCK_CLOCK_INTERRUPTS(void);
777 #define    _PR_MD_BLOCK_CLOCK_INTERRUPTS _MD_BLOCK_CLOCK_INTERRUPTS
778 
779 NSPR_API(void) _PR_MD_UNBLOCK_CLOCK_INTERRUPTS(void);
780 #define    _PR_MD_UNBLOCK_CLOCK_INTERRUPTS _MD_UNBLOCK_CLOCK_INTERRUPTS
781 
782 /* The _PR_MD_WAIT_LOCK and _PR_MD_WAKEUP_WAITER functions put to sleep and
783  * awaken a thread which is waiting on a lock or cvar.
784  */
785 extern PRStatus _PR_MD_WAIT(PRThread *, PRIntervalTime timeout);
786 #define    _PR_MD_WAIT _MD_WAIT
787 
788 extern PRStatus _PR_MD_WAKEUP_WAITER(PRThread *);
789 #define    _PR_MD_WAKEUP_WAITER _MD_WAKEUP_WAITER
790 
791 #ifndef _PR_LOCAL_THREADS_ONLY /* not if only local threads supported */
792 NSPR_API(void) _PR_MD_CLOCK_INTERRUPT(void);
793 #define    _PR_MD_CLOCK_INTERRUPT _MD_CLOCK_INTERRUPT
794 #endif
795 
796 /* Stack debugging */
797 NSPR_API(void) _PR_MD_INIT_STACK(PRThreadStack *ts, PRIntn redzone);
798 #define    _PR_MD_INIT_STACK _MD_INIT_STACK
799 
800 NSPR_API(void) _PR_MD_CLEAR_STACK(PRThreadStack* ts);
801 #define    _PR_MD_CLEAR_STACK _MD_CLEAR_STACK
802 
803 /* CPU related */
804 NSPR_API(PRInt32) _PR_MD_GET_INTSOFF(void);
805 #define    _PR_MD_GET_INTSOFF _MD_GET_INTSOFF
806 
807 NSPR_API(void) _PR_MD_SET_INTSOFF(PRInt32 _val);
808 #define    _PR_MD_SET_INTSOFF _MD_SET_INTSOFF
809 
810 NSPR_API(_PRCPU*) _PR_MD_CURRENT_CPU(void);
811 #define    _PR_MD_CURRENT_CPU _MD_CURRENT_CPU
812 
813 NSPR_API(void) _PR_MD_SET_CURRENT_CPU(_PRCPU *cpu);
814 #define    _PR_MD_SET_CURRENT_CPU _MD_SET_CURRENT_CPU
815 
816 NSPR_API(void) _PR_MD_INIT_RUNNING_CPU(_PRCPU *cpu);
817 #define    _PR_MD_INIT_RUNNING_CPU _MD_INIT_RUNNING_CPU
818 
819 /*
820  * Returns the number of threads awoken or 0 if a timeout occurred;
821  */
822 extern PRInt32 _PR_MD_PAUSE_CPU(PRIntervalTime timeout);
823 #define    _PR_MD_PAUSE_CPU _MD_PAUSE_CPU
824 
825 extern void _PR_MD_CLEANUP_BEFORE_EXIT(void);
826 #define _PR_MD_CLEANUP_BEFORE_EXIT _MD_CLEANUP_BEFORE_EXIT
827 
828 extern void _PR_MD_EXIT(PRIntn status);
829 #define    _PR_MD_EXIT _MD_EXIT
830 
831 /* Locks related */
832 
833 NSPR_API(void) _PR_MD_INIT_LOCKS(void);
834 #define    _PR_MD_INIT_LOCKS _MD_INIT_LOCKS
835 
836 NSPR_API(PRStatus) _PR_MD_NEW_LOCK(_MDLock *md);
837 #define    _PR_MD_NEW_LOCK _MD_NEW_LOCK
838 
839 NSPR_API(void) _PR_MD_FREE_LOCK(_MDLock *md);
840 #define    _PR_MD_FREE_LOCK _MD_FREE_LOCK
841 
842 NSPR_API(void) _PR_MD_LOCK(_MDLock *md);
843 #define    _PR_MD_LOCK _MD_LOCK
844 
845 /* Return 0 on success, a nonzero value on failure. */
846 NSPR_API(PRIntn) _PR_MD_TEST_AND_LOCK(_MDLock *md);
847 #define    _PR_MD_TEST_AND_LOCK _MD_TEST_AND_LOCK
848 
849 NSPR_API(void) _PR_MD_UNLOCK(_MDLock *md);
850 #define    _PR_MD_UNLOCK _MD_UNLOCK
851 
852 NSPR_API(void) _PR_MD_IOQ_LOCK(void);
853 #define    _PR_MD_IOQ_LOCK _MD_IOQ_LOCK
854 
855 NSPR_API(void) _PR_MD_IOQ_UNLOCK(void);
856 #define    _PR_MD_IOQ_UNLOCK _MD_IOQ_UNLOCK
857 
858 #ifndef _PR_LOCAL_THREADS_ONLY /* not if only local threads supported */
859 /* Semaphore related -- only for native threads */
860 #ifdef HAVE_CVAR_BUILT_ON_SEM
861 NSPR_API(void) _PR_MD_NEW_SEM(_MDSemaphore *md, PRUintn value);
862 #define _PR_MD_NEW_SEM _MD_NEW_SEM
863 
864 NSPR_API(void) _PR_MD_DESTROY_SEM(_MDSemaphore *md);
865 #define _PR_MD_DESTROY_SEM _MD_DESTROY_SEM
866 
867 NSPR_API(PRStatus) _PR_MD_TIMED_WAIT_SEM(
868     _MDSemaphore *md, PRIntervalTime timeout);
869 #define _PR_MD_TIMED_WAIT_SEM _MD_TIMED_WAIT_SEM
870 
871 NSPR_API(PRStatus) _PR_MD_WAIT_SEM(_MDSemaphore *md);
872 #define _PR_MD_WAIT_SEM _MD_WAIT_SEM
873 
874 NSPR_API(void) _PR_MD_POST_SEM(_MDSemaphore *md);
875 #define _PR_MD_POST_SEM _MD_POST_SEM
876 #endif /* HAVE_CVAR_BUILT_ON_SEM */
877 
878 #endif
879 
880 /* Condition Variables related -- only for native threads */
881 
882 #ifndef _PR_LOCAL_THREADS_ONLY /* not if only local threads supported */
883 NSPR_API(PRInt32) _PR_MD_NEW_CV(_MDCVar *md);
884 #define    _PR_MD_NEW_CV _MD_NEW_CV
885 
886 NSPR_API(void) _PR_MD_FREE_CV(_MDCVar *md);
887 #define    _PR_MD_FREE_CV _MD_FREE_CV
888 
889 NSPR_API(void) _PR_MD_WAIT_CV(
890     _MDCVar *mdCVar,_MDLock *mdLock,PRIntervalTime timeout);
891 #define    _PR_MD_WAIT_CV _MD_WAIT_CV
892 
893 NSPR_API(void) _PR_MD_NOTIFY_CV(_MDCVar *md, _MDLock *lock);
894 #define    _PR_MD_NOTIFY_CV _MD_NOTIFY_CV
895 
896 NSPR_API(void) _PR_MD_NOTIFYALL_CV(_MDCVar *md, _MDLock *lock);
897 #define    _PR_MD_NOTIFYALL_CV _MD_NOTIFYALL_CV
898 #endif /* _PR_LOCAL_THREADS_ONLY */
899 
900 /* Threads related */
901 NSPR_API(PRThread*) _PR_MD_CURRENT_THREAD(void);
902 #define    _PR_MD_CURRENT_THREAD _MD_CURRENT_THREAD
903 
904 NSPR_API(PRThread*) _PR_MD_GET_ATTACHED_THREAD(void);
905 #define    _PR_MD_GET_ATTACHED_THREAD _MD_GET_ATTACHED_THREAD
906 
907 NSPR_API(PRThread*) _PR_MD_LAST_THREAD(void);
908 #define    _PR_MD_LAST_THREAD _MD_LAST_THREAD
909 
910 NSPR_API(void) _PR_MD_SET_CURRENT_THREAD(PRThread *thread);
911 #define    _PR_MD_SET_CURRENT_THREAD _MD_SET_CURRENT_THREAD
912 
913 NSPR_API(void) _PR_MD_SET_LAST_THREAD(PRThread *thread);
914 #define    _PR_MD_SET_LAST_THREAD _MD_SET_LAST_THREAD
915 
916 extern PRStatus _PR_MD_INIT_THREAD(PRThread *thread);
917 #define    _PR_MD_INIT_THREAD _MD_INIT_THREAD
918 
919 extern void _PR_MD_EXIT_THREAD(PRThread *thread);
920 #define    _PR_MD_EXIT_THREAD _MD_EXIT_THREAD
921 
922 #ifndef _PR_LOCAL_THREADS_ONLY /* not if only local threads supported */
923 
924 NSPR_API(PRStatus) _PR_MD_INIT_ATTACHED_THREAD(PRThread *thread);
925 #define    _PR_MD_INIT_ATTACHED_THREAD _MD_INIT_ATTACHED_THREAD
926 
927 extern void _PR_MD_SUSPEND_THREAD(PRThread *thread);
928 #define    _PR_MD_SUSPEND_THREAD _MD_SUSPEND_THREAD
929 
930 extern void _PR_MD_RESUME_THREAD(PRThread *thread);
931 #define    _PR_MD_RESUME_THREAD _MD_RESUME_THREAD
932 
933 extern void _PR_MD_SUSPEND_CPU(_PRCPU  *cpu);
934 #define    _PR_MD_SUSPEND_CPU _MD_SUSPEND_CPU
935 
936 extern void _PR_MD_RESUME_CPU(_PRCPU  *cpu);
937 #define    _PR_MD_RESUME_CPU _MD_RESUME_CPU
938 
939 extern void _PR_MD_BEGIN_SUSPEND_ALL(void);
940 #define    _PR_MD_BEGIN_SUSPEND_ALL _MD_BEGIN_SUSPEND_ALL
941 
942 extern void _PR_MD_END_SUSPEND_ALL(void);
943 #define    _PR_MD_END_SUSPEND_ALL _MD_END_SUSPEND_ALL
944 
945 extern void _PR_MD_BEGIN_RESUME_ALL(void);
946 #define    _PR_MD_BEGIN_RESUME_ALL _MD_BEGIN_RESUME_ALL
947 
948 extern void _PR_MD_END_RESUME_ALL(void);
949 #define    _PR_MD_END_RESUME_ALL _MD_END_RESUME_ALL
950 
951 #if defined(IRIX)
952 NSPR_API(void) _PR_IRIX_CHILD_PROCESS(void);
953 #endif        /* IRIX */
954 
955 #endif        /* !_PR_LOCAL_THREADS_ONLY */
956 
957 extern void _PR_MD_CLEAN_THREAD(PRThread *thread);
958 #define    _PR_MD_CLEAN_THREAD _MD_CLEAN_THREAD
959 
960 #ifdef HAVE_CUSTOM_USER_THREADS
961 extern void _PR_MD_CREATE_PRIMORDIAL_USER_THREAD(PRThread *);
962 #define    _PR_MD_CREATE_PRIMORDIAL_USER_THREAD _MD_CREATE_PRIMORDIAL_USER_THREAD
963 
964 extern PRThread* _PR_MD_CREATE_USER_THREAD(
965                         PRUint32 stacksize,
966                         void (*start)(void *),
967                         void *arg);
968 #define    _PR_MD_CREATE_USER_THREAD _MD_CREATE_USER_THREAD
969 #endif
970 
971 extern PRStatus _PR_MD_CREATE_THREAD(
972                         PRThread *thread,
973                         void (*start) (void *),
974                         PRThreadPriority priority,
975                         PRThreadScope scope,
976                         PRThreadState state,
977                         PRUint32 stackSize);
978 #define    _PR_MD_CREATE_THREAD _MD_CREATE_THREAD
979 
980 extern void _PR_MD_JOIN_THREAD(_MDThread *md);
981 #define    _PR_MD_JOIN_THREAD _MD_JOIN_THREAD
982 
983 extern void _PR_MD_END_THREAD(void);
984 #define    _PR_MD_END_THREAD _MD_END_THREAD
985 
986 extern void _PR_MD_YIELD(void);
987 #define    _PR_MD_YIELD _MD_YIELD
988 
989 extern void _PR_MD_SET_PRIORITY(_MDThread *md, PRThreadPriority newPri);
990 #define    _PR_MD_SET_PRIORITY _MD_SET_PRIORITY
991 
992 extern void _PR_MD_SET_CURRENT_THREAD_NAME(const char *name);
993 #define    _PR_MD_SET_CURRENT_THREAD_NAME _MD_SET_CURRENT_THREAD_NAME
994 
995 NSPR_API(void) _PR_MD_SUSPENDALL(void);
996 #define    _PR_MD_SUSPENDALL _MD_SUSPENDALL
997 
998 NSPR_API(void) _PR_MD_RESUMEALL(void);
999 #define    _PR_MD_RESUMEALL _MD_RESUMEALL
1000 
1001 extern void _PR_MD_INIT_CONTEXT(
1002     PRThread *thread, char *top, void (*start) (void), PRBool *status);
1003 #define    _PR_MD_INIT_CONTEXT _MD_INIT_CONTEXT
1004 
1005 extern void _PR_MD_SWITCH_CONTEXT(PRThread *thread);
1006 #define    _PR_MD_SWITCH_CONTEXT _MD_SWITCH_CONTEXT
1007 
1008 extern void _PR_MD_RESTORE_CONTEXT(PRThread *thread);
1009 #define    _PR_MD_RESTORE_CONTEXT _MD_RESTORE_CONTEXT
1010 
1011 /* Segment related */
1012 extern void _PR_MD_INIT_SEGS(void);
1013 #define    _PR_MD_INIT_SEGS _MD_INIT_SEGS
1014 
1015 extern PRStatus _PR_MD_ALLOC_SEGMENT(PRSegment *seg, PRUint32 size, void *vaddr);
1016 #define    _PR_MD_ALLOC_SEGMENT _MD_ALLOC_SEGMENT
1017 
1018 extern void _PR_MD_FREE_SEGMENT(PRSegment *seg);
1019 #define    _PR_MD_FREE_SEGMENT _MD_FREE_SEGMENT
1020 
1021 /* Directory enumeration related */
1022 extern PRStatus _PR_MD_OPEN_DIR(_MDDir *md,const char *name);
1023 #define    _PR_MD_OPEN_DIR _MD_OPEN_DIR
1024 
1025 extern char * _PR_MD_READ_DIR(_MDDir *md, PRIntn flags);
1026 #define    _PR_MD_READ_DIR _MD_READ_DIR
1027 
1028 extern PRInt32 _PR_MD_CLOSE_DIR(_MDDir *md);
1029 #define    _PR_MD_CLOSE_DIR _MD_CLOSE_DIR
1030 
1031 /* Named semaphores related */
1032 extern PRSem * _PR_MD_OPEN_SEMAPHORE(
1033     const char *osname, PRIntn flags, PRIntn mode, PRUintn value);
1034 #define    _PR_MD_OPEN_SEMAPHORE _MD_OPEN_SEMAPHORE
1035 
1036 extern PRStatus _PR_MD_WAIT_SEMAPHORE(PRSem *sem);
1037 #define    _PR_MD_WAIT_SEMAPHORE _MD_WAIT_SEMAPHORE
1038 
1039 extern PRStatus _PR_MD_POST_SEMAPHORE(PRSem *sem);
1040 #define    _PR_MD_POST_SEMAPHORE _MD_POST_SEMAPHORE
1041 
1042 extern PRStatus _PR_MD_CLOSE_SEMAPHORE(PRSem *sem);
1043 #define    _PR_MD_CLOSE_SEMAPHORE _MD_CLOSE_SEMAPHORE
1044 
1045 extern PRStatus _PR_MD_DELETE_SEMAPHORE(const char *osname);
1046 #define    _PR_MD_DELETE_SEMAPHORE _MD_DELETE_SEMAPHORE
1047 
1048 /* I/O related */
1049 extern void _PR_MD_INIT_FILEDESC(PRFileDesc *fd);
1050 #define    _PR_MD_INIT_FILEDESC _MD_INIT_FILEDESC
1051 
1052 extern void _PR_MD_MAKE_NONBLOCK(PRFileDesc *fd);
1053 #define    _PR_MD_MAKE_NONBLOCK _MD_MAKE_NONBLOCK
1054 
1055 /* File I/O related */
1056 extern PROsfd _PR_MD_OPEN(const char *name, PRIntn osflags, PRIntn mode);
1057 #define    _PR_MD_OPEN _MD_OPEN
1058 
1059 extern PROsfd _PR_MD_OPEN_FILE(const char *name, PRIntn osflags, PRIntn mode);
1060 #define    _PR_MD_OPEN_FILE _MD_OPEN_FILE
1061 
1062 extern PRInt32 _PR_MD_CLOSE_FILE(PROsfd osfd);
1063 #define    _PR_MD_CLOSE_FILE _MD_CLOSE_FILE
1064 
1065 extern PRInt32 _PR_MD_READ(PRFileDesc *fd, void *buf, PRInt32 amount);
1066 #define    _PR_MD_READ _MD_READ
1067 
1068 extern PRInt32 _PR_MD_WRITE(PRFileDesc *fd, const void *buf, PRInt32 amount);
1069 #define    _PR_MD_WRITE _MD_WRITE
1070 
1071 extern PRInt32 _PR_MD_WRITEV(
1072     PRFileDesc *fd, const struct PRIOVec *iov,
1073     PRInt32 iov_size, PRIntervalTime timeout);
1074 #define    _PR_MD_WRITEV _MD_WRITEV
1075 
1076 extern PRInt32 _PR_MD_FSYNC(PRFileDesc *fd);
1077 #define    _PR_MD_FSYNC _MD_FSYNC
1078 
1079 extern PRInt32 _PR_MD_DELETE(const char *name);
1080 #define        _PR_MD_DELETE _MD_DELETE
1081 
1082 extern PRInt32 _PR_MD_RENAME(const char *from, const char *to);
1083 #define _PR_MD_RENAME _MD_RENAME
1084 
1085 extern PRInt32 _PR_MD_ACCESS(const char *name, PRAccessHow how);
1086 #define _PR_MD_ACCESS _MD_ACCESS
1087 
1088 extern PRInt32 _PR_MD_STAT(const char *name, struct stat *buf);
1089 #define _PR_MD_STAT _MD_STAT
1090 
1091 extern PRInt32 _PR_MD_MKDIR(const char *name, PRIntn mode);
1092 #define _PR_MD_MKDIR _MD_MKDIR
1093 
1094 extern PRInt32 _PR_MD_MAKE_DIR(const char *name, PRIntn mode);
1095 #define _PR_MD_MAKE_DIR _MD_MAKE_DIR
1096 
1097 extern PRInt32 _PR_MD_RMDIR(const char *name);
1098 #define _PR_MD_RMDIR _MD_RMDIR
1099 
1100 #ifdef MOZ_UNICODE
1101 /* UTF16 File I/O related */
1102 extern PRStatus _PR_MD_OPEN_DIR_UTF16(_MDDirUTF16 *md, const PRUnichar *name);
1103 #define    _PR_MD_OPEN_DIR_UTF16 _MD_OPEN_DIR_UTF16
1104 
1105 extern PROsfd _PR_MD_OPEN_FILE_UTF16(const PRUnichar *name, PRIntn osflags, PRIntn mode);
1106 #define    _PR_MD_OPEN_FILE_UTF16 _MD_OPEN_FILE_UTF16
1107 
1108 extern PRUnichar * _PR_MD_READ_DIR_UTF16(_MDDirUTF16 *md, PRIntn flags);
1109 #define    _PR_MD_READ_DIR_UTF16 _MD_READ_DIR_UTF16
1110 
1111 extern PRInt32 _PR_MD_CLOSE_DIR_UTF16(_MDDirUTF16 *md);
1112 #define    _PR_MD_CLOSE_DIR_UTF16 _MD_CLOSE_DIR_UTF16
1113 
1114 extern PRInt32 _PR_MD_GETFILEINFO64_UTF16(const PRUnichar *fn, PRFileInfo64 *info);
1115 #define _PR_MD_GETFILEINFO64_UTF16 _MD_GETFILEINFO64_UTF16
1116 #endif /* MOZ_UNICODE */
1117 
1118 /* Socket I/O related */
1119 extern void _PR_MD_INIT_IO(void);
1120 #define    _PR_MD_INIT_IO _MD_INIT_IO
1121 
1122 extern PRInt32 _PR_MD_CLOSE_SOCKET(PROsfd osfd);
1123 #define    _PR_MD_CLOSE_SOCKET _MD_CLOSE_SOCKET
1124 
1125 extern PRInt32 _PR_MD_CONNECT(
1126     PRFileDesc *fd, const PRNetAddr *addr,
1127     PRUint32 addrlen, PRIntervalTime timeout);
1128 #define    _PR_MD_CONNECT _MD_CONNECT
1129 
1130 extern PROsfd _PR_MD_ACCEPT(
1131     PRFileDesc *fd, PRNetAddr *addr,
1132     PRUint32 *addrlen, PRIntervalTime timeout);
1133 #define    _PR_MD_ACCEPT _MD_ACCEPT
1134 
1135 extern PRInt32 _PR_MD_BIND(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen);
1136 #define    _PR_MD_BIND _MD_BIND
1137 
1138 extern PRInt32 _PR_MD_LISTEN(PRFileDesc *fd, PRIntn backlog);
1139 #define    _PR_MD_LISTEN _MD_LISTEN
1140 
1141 extern PRInt32 _PR_MD_SHUTDOWN(PRFileDesc *fd, PRIntn how);
1142 #define    _PR_MD_SHUTDOWN _MD_SHUTDOWN
1143 
1144 extern PRInt32 _PR_MD_RECV(PRFileDesc *fd, void *buf, PRInt32 amount,
1145                                PRIntn flags, PRIntervalTime timeout);
1146 #define    _PR_MD_RECV _MD_RECV
1147 
1148 extern PRInt32 _PR_MD_SEND(
1149     PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,
1150     PRIntervalTime timeout);
1151 #define    _PR_MD_SEND _MD_SEND
1152 
1153 extern PRInt32 _PR_MD_ACCEPT_READ(PRFileDesc *sd, PROsfd *newSock,
1154                                 PRNetAddr **raddr, void *buf, PRInt32 amount,
1155                                 PRIntervalTime timeout);
1156 #define _PR_MD_ACCEPT_READ _MD_ACCEPT_READ
1157 
1158 #ifdef WIN32
1159 extern PROsfd _PR_MD_FAST_ACCEPT(PRFileDesc *fd, PRNetAddr *addr,
1160                                 PRUint32 *addrlen, PRIntervalTime timeout,
1161                                 PRBool fast,
1162                                 _PR_AcceptTimeoutCallback callback,
1163                                 void *callbackArg);
1164 
1165 extern PRInt32 _PR_MD_FAST_ACCEPT_READ(PRFileDesc *sd, PROsfd *newSock,
1166                                 PRNetAddr **raddr, void *buf, PRInt32 amount,
1167                                 PRIntervalTime timeout, PRBool fast,
1168                                 _PR_AcceptTimeoutCallback callback,
1169                                 void *callbackArg);
1170 
1171 extern void _PR_MD_UPDATE_ACCEPT_CONTEXT(PROsfd s, PROsfd ls);
1172 #define _PR_MD_UPDATE_ACCEPT_CONTEXT _MD_UPDATE_ACCEPT_CONTEXT
1173 /*
1174  * The NSPR epoch (00:00:00 1 Jan 1970 UTC) in FILETIME.
1175  * We store the value in a PRTime variable for convenience.
1176  * This constant is used by _PR_FileTimeToPRTime().
1177  * This is defined in ntmisc.c
1178  */
1179 extern const PRTime _pr_filetime_offset;
1180 #endif /* WIN32 */
1181 
1182 extern PRInt32 _PR_MD_SENDFILE(
1183     PRFileDesc *sock, PRSendFileData *sfd,
1184 	PRInt32 flags, PRIntervalTime timeout);
1185 #define _PR_MD_SENDFILE _MD_SENDFILE
1186 
1187 extern PRStatus _PR_MD_GETSOCKNAME(
1188     PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen);
1189 #define    _PR_MD_GETSOCKNAME _MD_GETSOCKNAME
1190 
1191 extern PRStatus _PR_MD_GETPEERNAME(
1192     PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen);
1193 #define    _PR_MD_GETPEERNAME _MD_GETPEERNAME
1194 
1195 extern PRStatus _PR_MD_GETSOCKOPT(
1196     PRFileDesc *fd, PRInt32 level, PRInt32 optname, char* optval, PRInt32* optlen);
1197 #define    _PR_MD_GETSOCKOPT _MD_GETSOCKOPT
1198 
1199 extern PRStatus _PR_MD_SETSOCKOPT(
1200     PRFileDesc *fd, PRInt32 level, PRInt32 optname,
1201     const char* optval, PRInt32 optlen);
1202 #define    _PR_MD_SETSOCKOPT _MD_SETSOCKOPT
1203 
1204 extern PRStatus PR_CALLBACK _PR_SocketGetSocketOption(
1205     PRFileDesc *fd, PRSocketOptionData *data);
1206 
1207 extern PRStatus PR_CALLBACK _PR_SocketSetSocketOption(
1208     PRFileDesc *fd, const PRSocketOptionData *data);
1209 
1210 extern PRInt32 _PR_MD_RECVFROM(
1211     PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,
1212     PRNetAddr *addr, PRUint32 *addrlen, PRIntervalTime timeout);
1213 #define    _PR_MD_RECVFROM _MD_RECVFROM
1214 
1215 extern PRInt32 _PR_MD_SENDTO(
1216     PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,
1217     const PRNetAddr *addr, PRUint32 addrlen, PRIntervalTime timeout);
1218 #define    _PR_MD_SENDTO _MD_SENDTO
1219 
1220 #if defined(_WIN64) && defined(WIN95)
1221 extern PRInt32 _PR_MD_TCPSENDTO(
1222     PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,
1223     const PRNetAddr *addr, PRUint32 addrlen, PRIntervalTime timeout);
1224 #define    _PR_MD_TCPSENDTO _MD_TCPSENDTO
1225 #endif
1226 
1227 extern PRInt32 _PR_MD_SOCKETPAIR(int af, int type, int flags, PROsfd *osfd);
1228 #define    _PR_MD_SOCKETPAIR _MD_SOCKETPAIR
1229 
1230 extern PROsfd _PR_MD_SOCKET(int af, int type, int flags);
1231 #define    _PR_MD_SOCKET _MD_SOCKET
1232 
1233 extern PRInt32 _PR_MD_SOCKETAVAILABLE(PRFileDesc *fd);
1234 #define    _PR_MD_SOCKETAVAILABLE _MD_SOCKETAVAILABLE
1235 
1236 extern PRInt32 _PR_MD_PIPEAVAILABLE(PRFileDesc *fd);
1237 #define    _PR_MD_PIPEAVAILABLE _MD_PIPEAVAILABLE
1238 
1239 extern PRInt32 _PR_MD_PR_POLL(PRPollDesc *pds, PRIntn npds,
1240                                                                                         PRIntervalTime timeout);
1241 #define    _PR_MD_PR_POLL _MD_PR_POLL
1242 
1243 /*
1244  * Initialize fd->secret->inheritable for a newly created fd.
1245  * If 'imported' is false, the osfd (i.e., fd->secret->md.osfd)
1246  * was created by NSPR and hence has the OS-dependent default
1247  * inheritable attribute.  If 'imported' is true, the osfd was
1248  * not created by NSPR and hence a system call is required to
1249  * query its inheritable attribute.  Since we may never need to
1250  * know the inheritable attribute of a fd, a platform may choose
1251  * to initialize fd->secret->inheritable of an imported fd to
1252  * _PR_TRI_UNKNOWN and only pay the cost of the system call
1253  * (in _PR_MD_QUERY_FD_INHERITABLE) when necessary.
1254  */
1255 extern void _PR_MD_INIT_FD_INHERITABLE(PRFileDesc *fd, PRBool imported);
1256 #define    _PR_MD_INIT_FD_INHERITABLE _MD_INIT_FD_INHERITABLE
1257 
1258 extern PRStatus _PR_MD_SET_FD_INHERITABLE(PRFileDesc *fd, PRBool inheritable);
1259 #define    _PR_MD_SET_FD_INHERITABLE _MD_SET_FD_INHERITABLE
1260 
1261 
1262 #define _PR_PROCESS_TIMEOUT_INTERRUPT_ERRORS(me) \
1263         if (_PR_PENDING_INTERRUPT(me)) { \
1264                 me->flags &= ~_PR_INTERRUPT; \
1265                 PR_SetError( PR_PENDING_INTERRUPT_ERROR, 0); \
1266         } else { \
1267                 PR_SetError(PR_IO_TIMEOUT_ERROR, 0); \
1268         }
1269 
1270 extern void *_PR_MD_GET_SP(PRThread *thread);
1271 #define    _PR_MD_GET_SP _MD_GET_SP
1272 
1273 #endif /* defined(_PR_PTHREADS) */
1274 
1275 /************************************************************************/
1276 /*************************************************************************
1277 ** The remainder of the definitions are shared by pthreads and the classic
1278 ** NSPR code. These too may be conditionalized.
1279 *************************************************************************/
1280 /************************************************************************/
1281 
1282 extern PROffset32 _PR_MD_LSEEK(PRFileDesc *fd, PROffset32 offset, PRSeekWhence whence);
1283 #define    _PR_MD_LSEEK _MD_LSEEK
1284 
1285 extern PROffset64 _PR_MD_LSEEK64(PRFileDesc *fd, PROffset64 offset, PRSeekWhence whence);
1286 #define    _PR_MD_LSEEK64 _MD_LSEEK64
1287 
1288 extern PRInt32 _PR_MD_GETFILEINFO(const char *fn, PRFileInfo *info);
1289 #define _PR_MD_GETFILEINFO _MD_GETFILEINFO
1290 
1291 extern PRInt32 _PR_MD_GETFILEINFO64(const char *fn, PRFileInfo64 *info);
1292 #define _PR_MD_GETFILEINFO64 _MD_GETFILEINFO64
1293 
1294 extern PRInt32 _PR_MD_GETOPENFILEINFO(const PRFileDesc *fd, PRFileInfo *info);
1295 #define _PR_MD_GETOPENFILEINFO _MD_GETOPENFILEINFO
1296 
1297 extern PRInt32 _PR_MD_GETOPENFILEINFO64(const PRFileDesc *fd, PRFileInfo64 *info);
1298 #define _PR_MD_GETOPENFILEINFO64 _MD_GETOPENFILEINFO64
1299 
1300 
1301 /*****************************************************************************/
1302 /************************** File descriptor caching **************************/
1303 /*****************************************************************************/
1304 extern void _PR_InitFdCache(void);
1305 extern void _PR_CleanupFdCache(void);
1306 extern PRFileDesc *_PR_Getfd(void);
1307 extern void _PR_Putfd(PRFileDesc *fd);
1308 
1309 /*
1310  * These flags are used by NSPR temporarily in the poll
1311  * descriptor's out_flags field to record the mapping of
1312  * NSPR's poll flags to the system poll flags.
1313  *
1314  * If _PR_POLL_READ_SYS_WRITE bit is set, it means the
1315  * PR_POLL_READ flag specified by the topmost layer is
1316  * mapped to the WRITE flag at the system layer.  Similarly
1317  * for the other three _PR_POLL_XXX_SYS_YYY flags.  It is
1318  * assumed that the PR_POLL_EXCEPT flag doesn't get mapped
1319  * to other flags.
1320  */
1321 #define _PR_POLL_READ_SYS_READ     0x1
1322 #define _PR_POLL_READ_SYS_WRITE    0x2
1323 #define _PR_POLL_WRITE_SYS_READ    0x4
1324 #define _PR_POLL_WRITE_SYS_WRITE   0x8
1325 
1326 /*
1327 ** These methods are coerced into file descriptor methods table
1328 ** when the intended service is inappropriate for the particular
1329 ** type of file descriptor.
1330 */
1331 extern PRIntn _PR_InvalidInt(void);
1332 extern PRInt16 _PR_InvalidInt16(void);
1333 extern PRInt64 _PR_InvalidInt64(void);
1334 extern PRStatus _PR_InvalidStatus(void);
1335 extern PRFileDesc *_PR_InvalidDesc(void);
1336 
1337 extern PRIOMethods _pr_faulty_methods;
1338 
1339 /*
1340 ** The PR_NETADDR_SIZE macro can only be called on a PRNetAddr union
1341 ** whose 'family' field is set.  It returns the size of the union
1342 ** member corresponding to the specified address family.
1343 */
1344 
1345 extern PRUintn _PR_NetAddrSize(const PRNetAddr* addr);
1346 
1347 #if defined(_PR_INET6)
1348 
1349 #define PR_NETADDR_SIZE(_addr) _PR_NetAddrSize(_addr)
1350 
1351 #elif defined(_PR_HAVE_MD_SOCKADDR_IN6)
1352 
1353 /*
1354 ** Under the following conditions:
1355 ** 1. _PR_INET6 is not defined;
1356 ** 2. _PR_INET6_PROBE is defined;
1357 ** 3. struct sockaddr_in6 has nonstandard fields at the end
1358 **    (e.g., on Solaris 8),
1359 ** (_addr)->ipv6 is smaller than struct sockaddr_in6, and
1360 ** hence we can't pass sizeof((_addr)->ipv6) to socket
1361 ** functions such as connect because they would fail with
1362 ** EINVAL.
1363 **
1364 ** To pass the correct socket address length to socket
1365 ** functions, define the macro _PR_HAVE_MD_SOCKADDR_IN6 and
1366 ** define struct _md_sockaddr_in6 to be isomorphic to
1367 ** struct sockaddr_in6.
1368 */
1369 
1370 #if defined(XP_UNIX) || defined(XP_OS2)
1371 #define PR_NETADDR_SIZE(_addr) 					\
1372         ((_addr)->raw.family == PR_AF_INET		\
1373         ? sizeof((_addr)->inet)					\
1374         : ((_addr)->raw.family == PR_AF_INET6	\
1375         ? sizeof(struct _md_sockaddr_in6)		\
1376         : sizeof((_addr)->local)))
1377 #else
1378 #define PR_NETADDR_SIZE(_addr) 					\
1379         ((_addr)->raw.family == PR_AF_INET		\
1380         ? sizeof((_addr)->inet)					\
1381         : sizeof(struct _md_sockaddr_in6))
1382 #endif /* defined(XP_UNIX) */
1383 
1384 #else
1385 
1386 #if defined(XP_UNIX) || defined(XP_OS2)
1387 #define PR_NETADDR_SIZE(_addr) 					\
1388         ((_addr)->raw.family == PR_AF_INET		\
1389         ? sizeof((_addr)->inet)					\
1390         : ((_addr)->raw.family == PR_AF_INET6	\
1391         ? sizeof((_addr)->ipv6)					\
1392         : sizeof((_addr)->local)))
1393 #else
1394 #define PR_NETADDR_SIZE(_addr) 					\
1395         ((_addr)->raw.family == PR_AF_INET		\
1396         ? sizeof((_addr)->inet)					\
1397         : sizeof((_addr)->ipv6))
1398 #endif /* defined(XP_UNIX) */
1399 
1400 #endif /* defined(_PR_INET6) */
1401 
1402 extern PRStatus _PR_MapOptionName(
1403     PRSockOption optname, PRInt32 *level, PRInt32 *name);
1404 extern void _PR_InitThreads(
1405     PRThreadType type, PRThreadPriority priority, PRUintn maxPTDs);
1406 
1407 struct PRLock {
1408 #if defined(_PR_PTHREADS)
1409     pthread_mutex_t mutex;          /* the underlying lock */
1410     _PT_Notified notified;          /* array of conditions notified */
1411     PRBool locked;                  /* whether the mutex is locked */
1412     pthread_t owner;                /* if locked, current lock owner */
1413 #elif defined(_PR_BTHREADS)
1414     sem_id	semaphoreID;	    /* the underlying lock */
1415     int32	benaphoreCount;	    /* number of people in lock */
1416     thread_id	owner;		    /* current lock owner */
1417 #else /* not pthreads or Be threads */
1418     PRCList links;                  /* linkage for PRThread.lockList */
1419     struct PRThread *owner;         /* current lock owner */
1420     PRCList waitQ;                  /* list of threads waiting for lock */
1421     PRThreadPriority priority;      /* priority of lock */
1422     PRThreadPriority boostPriority; /* boosted priority of lock owner */
1423     _MDLock ilock;                  /* Internal Lock to protect user-level fields */
1424 #endif
1425 };
1426 
1427 struct PRCondVar {
1428     PRLock *lock;               /* associated lock that protects the condition */
1429 #if defined(_PR_PTHREADS)
1430     pthread_cond_t cv;          /* underlying pthreads condition */
1431     PRInt32 notify_pending;     /* CV has destroy pending notification */
1432 #elif defined(_PR_BTHREADS)
1433     sem_id    sem;              /* the underlying lock */
1434     sem_id    handshakeSem;     /* the lock for 'notify'-threads waiting for confirmation */
1435     sem_id    signalSem;        /* the lock for threads waiting for someone to notify */
1436     volatile int32    nw;       /* the number waiting */
1437     volatile int32    ns;       /* the number signalling */
1438     long signalBenCount;        /* the number waiting on the underlying sem */
1439 #else /* not pthreads or Be threads */
1440     PRCList condQ;              /* Condition variable wait Q */
1441     _MDLock ilock;              /* Internal Lock to protect condQ */
1442     _MDCVar md;
1443 #endif
1444 };
1445 
1446 /************************************************************************/
1447 
1448 struct PRMonitor {
1449     const char* name;           /* monitor name for debugging */
1450 #if defined(_PR_PTHREADS)
1451     pthread_mutex_t lock;       /* lock is only held when accessing fields
1452                                  * of the PRMonitor, instead of being held
1453                                  * while the monitor is entered. The only
1454                                  * exception is notifyTimes, which is
1455                                  * protected by the monitor. */
1456     pthread_t owner;            /* the owner of the monitor or invalid */
1457     pthread_cond_t entryCV;     /* for threads waiting to enter the monitor */
1458 
1459     pthread_cond_t waitCV;      /* for threads waiting on the monitor */
1460     PRInt32 refCount;           /* reference count, an atomic variable.
1461                                  * PR_NewMonitor adds a reference to the
1462                                  * newly created PRMonitor, and
1463                                  * PR_DestroyMonitor releases that reference.
1464                                  * PR_ExitMonitor adds a reference before
1465                                  * unlocking the internal lock if it needs to
1466                                  * signal entryCV, and releases the reference
1467                                  * after signaling entryCV. */
1468 #else  /* defined(_PR_PTHREADS) */
1469     PRLock lock;                /* lock is only held when accessing fields
1470                                  * of the PRMonitor, instead of being held
1471                                  * while the monitor is entered. The only
1472                                  * exception is notifyTimes, which is
1473                                  * protected by the monitor. */
1474     PRThread *owner;            /* the owner of the monitor or invalid */
1475     PRCondVar entryCV;          /* for threads waiting to enter the monitor */
1476 
1477     PRCondVar waitCV;           /* for threads waiting on the monitor */
1478 #endif /* defined(_PR_PTHREADS) */
1479     PRUint32 entryCount;        /* # of times re-entered */
1480     PRIntn notifyTimes;         /* number of pending notifies for waitCV.
1481                                  * The special value -1 means a broadcast
1482                                  * (PR_NotifyAll). */
1483 };
1484 
1485 /************************************************************************/
1486 
1487 struct PRSemaphore {
1488 #if defined(_PR_BTHREADS)
1489     sem_id  sem;
1490     int32   benaphoreCount;
1491 #else
1492     PRCondVar *cvar;        /* associated lock and condition variable queue */
1493     PRUintn count;            /* the value of the counting semaphore */
1494     PRUint32 waiters;            /* threads waiting on the semaphore */
1495 #if defined(_PR_PTHREADS)
1496 #else  /* defined(_PR_PTHREADS) */
1497     _MDSemaphore md;
1498 #endif /* defined(_PR_PTHREADS) */
1499 #endif /* defined(_PR_BTHREADS) */
1500 };
1501 
1502 /*************************************************************************/
1503 
1504 struct PRSem {
1505 #ifdef _PR_HAVE_POSIX_SEMAPHORES
1506     sem_t *sem;
1507 #elif defined(_PR_HAVE_SYSV_SEMAPHORES)
1508     int semid;
1509 #elif defined(WIN32)
1510     HANDLE sem;
1511 #else
1512     PRInt8 notused;
1513 #endif
1514 };
1515 
1516 /*************************************************************************/
1517 
1518 struct PRStackStr {
1519     /* head MUST be at offset 0; assembly language code relies on this */
1520 #if defined(AIX)
1521     volatile PRStackElem prstk_head;
1522 #else
1523     PRStackElem prstk_head;
1524 #endif
1525 
1526     PRLock *prstk_lock;
1527     char *prstk_name;
1528 };
1529 
1530 /************************************************************************/
1531 
1532 /* XXX this needs to be exported (sigh) */
1533 struct PRThreadStack {
1534     PRCList links;
1535     PRUintn flags;
1536 
1537     char *allocBase;            /* base of stack's allocated memory */
1538     PRUint32 allocSize;         /* size of stack's allocated memory */
1539     char *stackBottom;          /* bottom of stack from C's point of view */
1540     char *stackTop;             /* top of stack from C's point of view */
1541     PRUint32 stackSize;         /* size of usable portion of the stack */
1542 
1543     PRSegment *seg;
1544         PRThread* thr;          /* back pointer to thread owning this stack */
1545 
1546 #if defined(_PR_PTHREADS)
1547 #else /* defined(_PR_PTHREADS) */
1548     _MDThreadStack md;
1549 #endif /* defined(_PR_PTHREADS) */
1550 };
1551 
1552 extern void _PR_DestroyThreadPrivate(PRThread*);
1553 
1554 typedef void (PR_CALLBACK *_PRStartFn)(void *);
1555 
1556 struct PRThread {
1557     PRUint32 state;                 /* thread's creation state */
1558     PRThreadPriority priority;      /* apparent priority, loosly defined */
1559 
1560     void *arg;                      /* argument to the client's entry point */
1561     _PRStartFn startFunc;           /* the root of the client's thread */
1562 
1563     PRThreadStack *stack;           /* info about thread's stack (for GC) */
1564     void *environment;              /* pointer to execution environment */
1565 
1566     PRThreadDumpProc dump;          /* dump thread info out */
1567     void *dumpArg;                  /* argument for the dump function */
1568 
1569     /*
1570     ** Per thread private data
1571     */
1572     PRUint32 tpdLength;             /* thread's current vector length */
1573     void **privateData;             /* private data vector or NULL */
1574     PRErrorCode errorCode;          /* current NSPR error code | zero */
1575     PRInt32 osErrorCode;            /* mapping of errorCode | zero */
1576     PRIntn  errorStringLength;      /* textLength from last call to PR_SetErrorText() */
1577     PRInt32 errorStringSize;        /* malloc()'d size of buffer | zero */
1578     char *errorString;              /* current error string | NULL */
1579     char *name;                     /* thread's name */
1580 
1581 #if defined(_PR_PTHREADS)
1582     pthread_t id;                   /* pthread identifier for the thread */
1583     PRBool idSet;                   /* whether 'id' has been set. Protected by
1584                                      * pt_book.ml. */
1585 #ifdef _PR_NICE_PRIORITY_SCHEDULING
1586     pid_t tid;                      /* Linux-specific kernel thread ID */
1587 #endif
1588     PRBool okToDelete;              /* ok to delete the PRThread struct? */
1589     PRCondVar *waiting;             /* where the thread is waiting | NULL */
1590     void *sp;                       /* recorded sp for garbage collection */
1591     PRThread *next, *prev;          /* simple linked list of all threads */
1592     PRUint32 suspend;               /* used to store suspend and resume flags */
1593 #ifdef PT_NO_SIGTIMEDWAIT
1594     pthread_mutex_t suspendResumeMutex;
1595     pthread_cond_t suspendResumeCV;
1596 #endif
1597     PRUint32 interrupt_blocked;     /* interrupt blocked */
1598     struct pollfd *syspoll_list;    /* Unix polling list used by PR_Poll */
1599     PRUint32 syspoll_count;         /* number of elements in syspoll_list */
1600 #if defined(_PR_POLL_WITH_SELECT)
1601     int *selectfd_list;             /* Unix fd's that PR_Poll selects on */
1602     PRUint32 selectfd_count;        /* number of elements in selectfd_list */
1603 #endif
1604 #elif defined(_PR_BTHREADS)
1605     PRUint32 flags;
1606     _MDThread md;
1607     PRBool io_pending;
1608     PRInt32 io_fd;
1609     PRBool io_suspended;
1610 #else /* not pthreads or Be threads */
1611     _MDLock threadLock;             /* Lock to protect thread state variables.
1612                                      * Protects the following fields:
1613                                      *     state
1614                                      *     priority
1615                                      *     links
1616                                      *     wait
1617                                      *     cpu
1618                                      */
1619     PRUint32 queueCount;
1620     PRUint32 waitCount;
1621 
1622     PRCList active;                 /* on list of all active threads        */
1623     PRCList links;
1624     PRCList waitQLinks;             /* when thread is PR_Wait'ing */
1625     PRCList lockList;               /* list of locks currently holding */
1626     PRIntervalTime sleep;           /* sleep time when thread is sleeping */
1627     struct _wait {
1628         struct PRLock *lock;
1629         struct PRCondVar *cvar;
1630     } wait;
1631 
1632     PRUint32 id;
1633     PRUint32 flags;
1634     PRUint32 no_sched;              /* Don't schedule the thread to run.
1635                                      * This flag has relevance only when
1636                                      * multiple NSPR CPUs are created.
1637                                      * When a thread is de-scheduled, there
1638                                      * is a narrow window of time in which
1639                                      * the thread is put on the run queue
1640                                      * but the scheduler is actually using
1641                                      * the stack of this thread.  It is safe
1642                                      * to run this thread on a different CPU
1643                                      * only when its stack is not in use on
1644                                      * any other CPU.  The no_sched flag is
1645                                      * set during this interval to prevent
1646                                      * the thread from being scheduled on a
1647                                      * different CPU.
1648                                      */
1649 
1650     /* thread termination condition variable for join */
1651     PRCondVar *term;
1652 
1653     _PRCPU *cpu;                    /* cpu to which this thread is bound    */
1654     PRUint32 threadAllocatedOnStack;/* boolean */
1655 
1656     /* When an async IO is in progress and a second async IO cannot be
1657      * initiated, the io_pending flag is set to true.  Some platforms will
1658      * not use the io_pending flag.  If the io_pending flag is true, then
1659      * io_fd is the OS-file descriptor on which IO is pending.
1660      */
1661     PRBool io_pending;
1662     PRInt32 io_fd;
1663 
1664     /* If a timeout occurs or if an outstanding IO is interrupted and the
1665      * OS doesn't support a real cancellation (NT or MAC), then the
1666      * io_suspended flag will be set to true.  The thread will be resumed
1667      * but may run into trouble issuing additional IOs until the io_pending
1668      * flag can be cleared
1669      */
1670     PRBool io_suspended;
1671 
1672     _MDThread md;
1673 #endif
1674 };
1675 
1676 struct PRProcessAttr {
1677     PRFileDesc *stdinFd;
1678     PRFileDesc *stdoutFd;
1679     PRFileDesc *stderrFd;
1680     char *currentDirectory;
1681     char *fdInheritBuffer;
1682     PRSize fdInheritBufferSize;
1683     PRSize fdInheritBufferUsed;
1684 };
1685 
1686 struct PRProcess {
1687     _MDProcess md;
1688 };
1689 
1690 struct PRFileMap {
1691     PRFileDesc *fd;
1692     PRFileMapProtect prot;
1693     _MDFileMap md;
1694 };
1695 
1696 /************************************************************************/
1697 
1698 /*
1699 ** File descriptors of the NSPR layer can be in one of the
1700 ** following states (stored in the 'state' field of struct
1701 ** PRFilePrivate):
1702 ** - _PR_FILEDESC_OPEN: The OS fd is open.
1703 ** - _PR_FILEDESC_CLOSED: The OS fd is closed.  The PRFileDesc
1704 **   is still open but is unusable.  The only operation allowed
1705 **   on the PRFileDesc is PR_Close().
1706 ** - _PR_FILEDESC_FREED: The OS fd is closed and the PRFileDesc
1707 **   structure is freed.
1708 */
1709 
1710 #define _PR_FILEDESC_OPEN       0xaaaaaaaa    /* 1010101... */
1711 #define _PR_FILEDESC_CLOSED     0x55555555    /* 0101010... */
1712 #define _PR_FILEDESC_FREED      0x11111111
1713 
1714 /*
1715 ** A boolean type with an additional "unknown" state
1716 */
1717 
1718 typedef enum {
1719     _PR_TRI_TRUE = 1,
1720     _PR_TRI_FALSE = 0,
1721     _PR_TRI_UNKNOWN = -1
1722 } _PRTriStateBool;
1723 
1724 struct PRFilePrivate {
1725     PRInt32 state;
1726     PRBool nonblocking;
1727     _PRTriStateBool inheritable;
1728     PRFileDesc *next;
1729     PRIntn lockCount;   /*   0: not locked
1730                          *  -1: a native lockfile call is in progress
1731                          * > 0: # times the file is locked */
1732 #ifdef _PR_HAVE_PEEK_BUFFER
1733     char *peekBuffer;
1734     PRInt32 peekBufSize;
1735     PRInt32 peekBytes;
1736 #endif
1737 #if !defined(_PR_HAVE_O_APPEND)
1738     PRBool  appendMode; /* Some platforms don't have O_APPEND or its
1739                          * equivalent, so they have to seek to end of
1740                          * file on write if the file was opened in
1741                          * append mode.  See Bugzilla 4090, 276330. */
1742 #endif
1743     _MDFileDesc md;
1744 #ifdef _PR_NEED_SECRET_AF
1745     PRUint16 af;        /* If the platform's implementation of accept()
1746                          * requires knowing the address family of the
1747 			 * socket, we save the address family here. */
1748 #endif
1749 
1750 #if defined(_WIN64)
1751     /* This is necessary for TCP Fast Open. TCP Fast Open in windows must
1752      * use ConnectEx function which uses OVERLAPPED. TCPSendTo will call
1753      * ConnectEx to send fast open data. If ConnectEx returns
1754      * ERROR_IO_PENDING we need to save OVERLAPPED structure and we will
1755      * use it in ConnectContinue to get the final result of ConnectEx.
1756      */
1757     PRBool alreadyConnected;
1758     PRBool overlappedActive;
1759     OVERLAPPED ol;
1760 #endif
1761 };
1762 
1763 #ifdef _WIN64
1764 #define PR_PRIdOSFD "lld"       /* for printing PROsfd */
1765 #define PR_PRIxOSFD "llx"
1766 #define PR_SCNdOSFD "lld"       /* for scanning PROsfd */
1767 #define PR_SCNxOSFD "llx"
1768 #else
1769 #define PR_PRIdOSFD "ld"        /* for printing PROsfd */
1770 #define PR_PRIxOSFD "lx"
1771 #define PR_SCNdOSFD "ld"        /* for scanning PROsfd */
1772 #define PR_SCNxOSFD "lx"
1773 #endif
1774 
1775 struct PRDir {
1776     PRDirEntry d;
1777     _MDDir md;
1778 };
1779 
1780 #ifdef MOZ_UNICODE
1781 struct PRDirUTF16 {
1782     PRDirEntry d;
1783     _MDDirUTF16 md;
1784 };
1785 #endif /* MOZ_UNICODE */
1786 
1787 extern void _PR_InitLocks(void);
1788 extern void _PR_InitSegs(void);
1789 extern void _PR_InitStacks(void);
1790 extern void _PR_InitTPD(void);
1791 extern void _PR_InitMem(void);
1792 extern void _PR_InitEnv(void);
1793 extern void _PR_InitCMon(void);
1794 extern void _PR_InitIO(void);
1795 extern void _PR_InitLog(void);
1796 extern void _PR_InitNet(void);
1797 extern void _PR_InitClock(void);
1798 extern void _PR_InitLinker(void);
1799 extern void _PR_InitAtomic(void);
1800 extern void _PR_InitCPUs(void);
1801 extern void _PR_InitDtoa(void);
1802 extern void _PR_InitTime(void);
1803 extern void _PR_InitMW(void);
1804 extern void _PR_InitRWLocks(void);
1805 extern void _PR_CleanupThread(PRThread *thread);
1806 extern void _PR_CleanupCallOnce(void);
1807 extern void _PR_CleanupMW(void);
1808 extern void _PR_CleanupTime(void);
1809 extern void _PR_CleanupDtoa(void);
1810 extern void _PR_ShutdownLinker(void);
1811 extern void _PR_CleanupEnv(void);
1812 extern void _PR_CleanupIO(void);
1813 extern void _PR_CleanupCMon(void);
1814 extern void _PR_CleanupNet(void);
1815 extern void _PR_CleanupLayerCache(void);
1816 extern void _PR_CleanupStacks(void);
1817 #ifdef WINNT
1818 extern void _PR_CleanupCPUs(void);
1819 #endif
1820 extern void _PR_CleanupThreads(void);
1821 extern void _PR_CleanupTPD(void);
1822 extern void _PR_Cleanup(void);
1823 extern void _PR_LogCleanup(void);
1824 extern void _PR_InitLayerCache(void);
1825 
1826 extern PRBool _pr_initialized;
1827 extern void _PR_ImplicitInitialization(void);
1828 extern PRBool _PR_Obsolete(const char *obsolete, const char *preferred);
1829 
1830 /************************************************************************/
1831 
1832 struct PRSegment {
1833     void *vaddr;
1834     PRUint32 size;
1835     PRUintn flags;
1836 #if defined(_PR_PTHREADS)
1837 #else  /* defined(_PR_PTHREADS) */
1838     _MDSegment md;
1839 #endif /* defined(_PR_PTHREADS) */
1840 };
1841 
1842 /* PRSegment.flags */
1843 #define _PR_SEG_VM    0x1
1844 
1845 /************************************************************************/
1846 
1847 extern PRInt32 _pr_pageSize;
1848 extern PRInt32 _pr_pageShift;
1849 
1850 extern PRLogModuleInfo *_pr_clock_lm;
1851 extern PRLogModuleInfo *_pr_cmon_lm;
1852 extern PRLogModuleInfo *_pr_io_lm;
1853 extern PRLogModuleInfo *_pr_cvar_lm;
1854 extern PRLogModuleInfo *_pr_mon_lm;
1855 extern PRLogModuleInfo *_pr_linker_lm;
1856 extern PRLogModuleInfo *_pr_sched_lm;
1857 extern PRLogModuleInfo *_pr_thread_lm;
1858 extern PRLogModuleInfo *_pr_gc_lm;
1859 
1860 extern PRFileDesc *_pr_stdin;
1861 extern PRFileDesc *_pr_stdout;
1862 extern PRFileDesc *_pr_stderr;
1863 
1864 /* Zone allocator */
1865 /*
1866 ** The zone allocator code has hardcoded pthread types and
1867 ** functions, so it can only be used in the pthreads version.
1868 ** This can be fixed by replacing the hardcoded pthread types
1869 ** and functions with macros that expand to the native thread
1870 ** types and functions on each platform.
1871 */
1872 #if defined(_PR_PTHREADS)
1873 #define _PR_ZONE_ALLOCATOR
1874 #endif
1875 
1876 #ifdef _PR_ZONE_ALLOCATOR
1877 extern void _PR_InitZones(void);
1878 extern void _PR_DestroyZones(void);
1879 #endif
1880 
1881 /* Overriding malloc, free, etc. */
1882 #if !defined(_PR_NO_PREEMPT) && defined(XP_UNIX) \
1883         && !defined(_PR_PTHREADS) && !defined(_PR_GLOBAL_THREADS_ONLY) \
1884         && !defined(PURIFY) \
1885         && !defined(DARWIN) \
1886         && !defined(QNX) \
1887         && !(defined (UNIXWARE) && defined (USE_SVR4_THREADS))
1888 #define _PR_OVERRIDE_MALLOC
1889 #endif
1890 
1891 /*************************************************************************
1892 * External machine-dependent code provided by each OS.                     *                                                                     *
1893 *************************************************************************/
1894 
1895 /* Initialization related */
1896 extern void _PR_MD_EARLY_INIT(void);
1897 #define    _PR_MD_EARLY_INIT _MD_EARLY_INIT
1898 
1899 extern void _PR_MD_INTERVAL_INIT(void);
1900 #define    _PR_MD_INTERVAL_INIT _MD_INTERVAL_INIT
1901 
1902 NSPR_API(void) _PR_MD_FINAL_INIT(void);
1903 #define    _PR_MD_FINAL_INIT _MD_FINAL_INIT
1904 
1905 extern void _PR_MD_EARLY_CLEANUP(void);
1906 #define    _PR_MD_EARLY_CLEANUP _MD_EARLY_CLEANUP
1907 
1908 /* Process control */
1909 
1910 extern PRProcess * _PR_MD_CREATE_PROCESS(
1911     const char *path,
1912     char *const *argv,
1913     char *const *envp,
1914     const PRProcessAttr *attr);
1915 #define    _PR_MD_CREATE_PROCESS _MD_CREATE_PROCESS
1916 
1917 extern PRStatus _PR_MD_DETACH_PROCESS(PRProcess *process);
1918 #define    _PR_MD_DETACH_PROCESS _MD_DETACH_PROCESS
1919 
1920 extern PRStatus _PR_MD_WAIT_PROCESS(PRProcess *process, PRInt32 *exitCode);
1921 #define    _PR_MD_WAIT_PROCESS _MD_WAIT_PROCESS
1922 
1923 extern PRStatus _PR_MD_KILL_PROCESS(PRProcess *process);
1924 #define    _PR_MD_KILL_PROCESS _MD_KILL_PROCESS
1925 
1926 /* Current Time */
1927 NSPR_API(PRTime) _PR_MD_NOW(void);
1928 #define    _PR_MD_NOW _MD_NOW
1929 
1930 /* Environment related */
1931 extern char* _PR_MD_GET_ENV(const char *name);
1932 #define    _PR_MD_GET_ENV _MD_GET_ENV
1933 
1934 extern PRIntn _PR_MD_PUT_ENV(const char *name);
1935 #define    _PR_MD_PUT_ENV _MD_PUT_ENV
1936 
1937 /* Atomic operations */
1938 
1939 extern void _PR_MD_INIT_ATOMIC(void);
1940 #define    _PR_MD_INIT_ATOMIC _MD_INIT_ATOMIC
1941 
1942 extern PRInt32 _PR_MD_ATOMIC_INCREMENT(PRInt32 *);
1943 #define    _PR_MD_ATOMIC_INCREMENT _MD_ATOMIC_INCREMENT
1944 
1945 extern PRInt32 _PR_MD_ATOMIC_ADD(PRInt32 *, PRInt32);
1946 #define    _PR_MD_ATOMIC_ADD _MD_ATOMIC_ADD
1947 
1948 extern PRInt32 _PR_MD_ATOMIC_DECREMENT(PRInt32 *);
1949 #define    _PR_MD_ATOMIC_DECREMENT _MD_ATOMIC_DECREMENT
1950 
1951 extern PRInt32 _PR_MD_ATOMIC_SET(PRInt32 *, PRInt32);
1952 #define    _PR_MD_ATOMIC_SET _MD_ATOMIC_SET
1953 
1954 /* Garbage collection */
1955 
1956 /*
1957 ** Save the registers that the GC would find interesting into the thread
1958 ** "t". isCurrent will be non-zero if the thread state that is being
1959 ** saved is the currently executing thread. Return the address of the
1960 ** first register to be scanned as well as the number of registers to
1961 ** scan in "np".
1962 **
1963 ** If "isCurrent" is non-zero then it is allowed for the thread context
1964 ** area to be used as scratch storage to hold just the registers
1965 ** necessary for scanning.
1966 */
1967 extern PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np);
1968 
1969 /* Time intervals */
1970 
1971 extern PRIntervalTime _PR_MD_GET_INTERVAL(void);
1972 #define _PR_MD_GET_INTERVAL _MD_GET_INTERVAL
1973 
1974 extern PRIntervalTime _PR_MD_INTERVAL_PER_SEC(void);
1975 #define _PR_MD_INTERVAL_PER_SEC _MD_INTERVAL_PER_SEC
1976 
1977 /* Affinity masks */
1978 
1979 extern PRInt32 _PR_MD_SETTHREADAFFINITYMASK(PRThread *thread, PRUint32 mask );
1980 #define _PR_MD_SETTHREADAFFINITYMASK _MD_SETTHREADAFFINITYMASK
1981 
1982 extern PRInt32 _PR_MD_GETTHREADAFFINITYMASK(PRThread *thread, PRUint32 *mask);
1983 #define _PR_MD_GETTHREADAFFINITYMASK _MD_GETTHREADAFFINITYMASK
1984 
1985 /* File locking */
1986 
1987 extern PRStatus _PR_MD_LOCKFILE(PROsfd osfd);
1988 #define    _PR_MD_LOCKFILE _MD_LOCKFILE
1989 
1990 extern PRStatus _PR_MD_TLOCKFILE(PROsfd osfd);
1991 #define    _PR_MD_TLOCKFILE _MD_TLOCKFILE
1992 
1993 extern PRStatus _PR_MD_UNLOCKFILE(PROsfd osfd);
1994 #define    _PR_MD_UNLOCKFILE _MD_UNLOCKFILE
1995 
1996 /* Memory-mapped files */
1997 
1998 extern PRStatus _PR_MD_CREATE_FILE_MAP(PRFileMap *fmap, PRInt64 size);
1999 #define _PR_MD_CREATE_FILE_MAP _MD_CREATE_FILE_MAP
2000 
2001 extern PRInt32 _PR_MD_GET_MEM_MAP_ALIGNMENT(void);
2002 #define _PR_MD_GET_MEM_MAP_ALIGNMENT _MD_GET_MEM_MAP_ALIGNMENT
2003 
2004 extern void * _PR_MD_MEM_MAP(
2005     PRFileMap *fmap,
2006     PROffset64 offset,
2007     PRUint32 len);
2008 #define _PR_MD_MEM_MAP _MD_MEM_MAP
2009 
2010 extern PRStatus _PR_MD_MEM_UNMAP(void *addr, PRUint32 size);
2011 #define _PR_MD_MEM_UNMAP _MD_MEM_UNMAP
2012 
2013 extern PRStatus _PR_MD_CLOSE_FILE_MAP(PRFileMap *fmap);
2014 #define _PR_MD_CLOSE_FILE_MAP _MD_CLOSE_FILE_MAP
2015 
2016 extern PRStatus _PR_MD_SYNC_MEM_MAP(
2017     PRFileDesc *fd,
2018     void *addr,
2019     PRUint32 len);
2020 #define _PR_MD_SYNC_MEM_MAP _MD_SYNC_MEM_MAP
2021 
2022 /* Named Shared Memory */
2023 
2024 /*
2025 ** Declare PRSharedMemory.
2026 */
2027 struct PRSharedMemory
2028 {
2029     char        *ipcname; /* after conversion to native */
2030     PRSize      size;  /* from open */
2031     PRIntn      mode;  /* from open */
2032     PRIntn      flags; /* from open */
2033 #if defined(PR_HAVE_POSIX_NAMED_SHARED_MEMORY)
2034     int         id;
2035 #elif defined(PR_HAVE_SYSV_NAMED_SHARED_MEMORY)
2036     int         id;
2037 #elif defined(PR_HAVE_WIN32_NAMED_SHARED_MEMORY)
2038     HANDLE      handle;
2039 #else
2040     PRUint32    nothing; /* placeholder, nothing behind here */
2041 #endif
2042     PRUint32    ident; /* guard word at end of struct */
2043 #define _PR_SHM_IDENT 0xdeadbad
2044 };
2045 
2046 extern PRSharedMemory * _MD_OpenSharedMemory(
2047     const char *name,
2048     PRSize      size,
2049     PRIntn      flags,
2050     PRIntn      mode
2051 );
2052 #define _PR_MD_OPEN_SHARED_MEMORY _MD_OpenSharedMemory
2053 
2054 extern void * _MD_AttachSharedMemory( PRSharedMemory *shm, PRIntn flags );
2055 #define _PR_MD_ATTACH_SHARED_MEMORY _MD_AttachSharedMemory
2056 
2057 extern PRStatus _MD_DetachSharedMemory( PRSharedMemory *shm, void *addr );
2058 #define _PR_MD_DETACH_SHARED_MEMORY _MD_DetachSharedMemory
2059 
2060 extern PRStatus _MD_CloseSharedMemory( PRSharedMemory *shm );
2061 #define _PR_MD_CLOSE_SHARED_MEMORY _MD_CloseSharedMemory
2062 
2063 extern PRStatus _MD_DeleteSharedMemory( const char *name );
2064 #define _PR_MD_DELETE_SHARED_MEMORY  _MD_DeleteSharedMemory
2065 
2066 extern PRFileMap* _md_OpenAnonFileMap(
2067     const char *dirName,
2068     PRSize      size,
2069     PRFileMapProtect prot
2070 );
2071 #define _PR_MD_OPEN_ANON_FILE_MAP _md_OpenAnonFileMap
2072 
2073 extern PRStatus _md_ExportFileMapAsString(
2074     PRFileMap *fm,
2075     PRSize    bufSize,
2076     char      *buf
2077 );
2078 #define _PR_MD_EXPORT_FILE_MAP_AS_STRING _md_ExportFileMapAsString
2079 
2080 extern PRFileMap * _md_ImportFileMapFromString(
2081     const char *fmstring
2082 );
2083 #define _PR_MD_IMPORT_FILE_MAP_FROM_STRING _md_ImportFileMapFromString
2084 
2085 
2086 
2087 /* Interprocess communications (IPC) */
2088 
2089 /*
2090  * The maximum length of an NSPR IPC name, including the
2091  * terminating null byte.
2092  */
2093 #define PR_IPC_NAME_SIZE 1024
2094 
2095 /*
2096  * Types of NSPR IPC objects
2097  */
2098 typedef enum {
2099     _PRIPCSem,  /* semaphores */
2100     _PRIPCShm   /* shared memory segments */
2101 } _PRIPCType;
2102 
2103 /*
2104  * Make a native IPC name from an NSPR IPC name.
2105  */
2106 extern PRStatus _PR_MakeNativeIPCName(
2107     const char *name,  /* NSPR IPC name */
2108     char *result,      /* result buffer */
2109     PRIntn size,       /* size of result buffer */
2110     _PRIPCType type    /* type of IPC object */
2111 );
2112 
2113 /* Socket call error code */
2114 
2115 NSPR_API(PRInt32) _PR_MD_GET_SOCKET_ERROR(void);
2116 #define    _PR_MD_GET_SOCKET_ERROR _MD_GET_SOCKET_ERROR
2117 
2118 /* Get name of current host */
2119 extern PRStatus _PR_MD_GETHOSTNAME(char *name, PRUint32 namelen);
2120 #define    _PR_MD_GETHOSTNAME _MD_GETHOSTNAME
2121 
2122 extern PRStatus _PR_MD_GETSYSINFO(PRSysInfo cmd, char *name, PRUint32 namelen);
2123 #define    _PR_MD_GETSYSINFO _MD_GETSYSINFO
2124 
2125 /* File descriptor inheritance */
2126 
2127 /*
2128  * If fd->secret->inheritable is _PR_TRI_UNKNOWN and we need to
2129  * know the inheritable attribute of the fd, call this function
2130  * to find that out.  This typically requires a system call.
2131  */
2132 extern void _PR_MD_QUERY_FD_INHERITABLE(PRFileDesc *fd);
2133 #define    _PR_MD_QUERY_FD_INHERITABLE _MD_QUERY_FD_INHERITABLE
2134 
2135 /* --- PR_GetRandomNoise() related things --- */
2136 
2137 extern PRSize _PR_MD_GetRandomNoise( void *buf, PRSize size );
2138 #define _PR_MD_GET_RANDOM_NOISE(buf,size) _PR_MD_GetRandomNoise((buf),(size))
2139 extern PRSize _pr_CopyLowBits( void *dest, PRSize dstlen, void *src, PRSize srclen );
2140 
2141 /* end PR_GetRandomNoise() related */
2142 
2143 #ifdef XP_BEOS
2144 
2145 extern PRLock *_connectLock;
2146 
2147 typedef struct _ConnectListNode {
2148 	PRInt32		osfd;
2149 	PRNetAddr	addr;
2150 	PRUint32	addrlen;
2151 	PRIntervalTime	timeout;
2152 } ConnectListNode;
2153 
2154 extern ConnectListNode connectList[64];
2155 
2156 extern PRUint32 connectCount;
2157 
2158 #endif /* XP_BEOS */
2159 
2160 #if defined(_WIN64) && defined(WIN95)
2161 typedef struct _PRFileDescList {
2162   PRFileDesc *fd;
2163   struct _PRFileDescList *next;
2164 } PRFileDescList;
2165 
2166 extern PRLock *_fd_waiting_for_overlapped_done_lock;
2167 extern PRFileDescList *_fd_waiting_for_overlapped_done;
2168 extern void CheckOverlappedPendingSocketsAreDone();
2169 #endif
2170 
2171 
2172 PR_END_EXTERN_C
2173 
2174 #endif /* primpl_h___ */
2175