home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-e.zip / nspr30-e / include / private / primpl.h < prev    next >
C/C++ Source or Header  |  1998-11-21  |  60KB  |  1,685 lines

  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 Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef primpl_h___
  20. #define primpl_h___
  21.  
  22. /*
  23.  * HP-UX 10.10's pthread.h (DCE threads) includes dce/cma.h, which
  24.  * has:
  25.  *     #define sigaction _sigaction_sys
  26.  * This macro causes chaos if signal.h gets included before pthread.h.
  27.  * To be safe, we include pthread.h first.
  28.  */
  29.  
  30. #if defined(_PR_PTHREADS)
  31. #include <pthread.h>
  32. #endif
  33.  
  34. #ifdef WINNT
  35. /* Need to force service-pack 3 extensions to be defined by
  36. ** setting _WIN32_WINNT to NT 4.0 for winsock.h, winbase.h, winnt.h.
  37. */
  38. #ifndef  _WIN32_WINNT
  39.     #define _WIN32_WINNT 0x0400
  40. #elif   (_WIN32_WINNT < 0x0400)
  41.     #undef  _WIN32_WINNT
  42.     #define _WIN32_WINNT 0x0400
  43. #endif /* _WIN32_WINNT */
  44. #endif /* WINNT */
  45.  
  46. #include "nspr.h"
  47. #include "prpriv.h"
  48.  
  49. typedef struct PRSegment PRSegment;
  50.  
  51. #ifdef XP_MAC
  52. #include "prosdep.h"
  53. #include "probslet.h"
  54. #else
  55. #include "md/prosdep.h"
  56. #include "obsolete/probslet.h"
  57. #endif  /* XP_MAC */
  58.  
  59. /*************************************************************************
  60. *****  A Word about Model Dependent Function Naming Convention ***********
  61. *************************************************************************/
  62.  
  63. /*
  64. NSPR 2.0 must implement its function across a range of platforms 
  65. including: MAC, Windows/16, Windows/95, Windows/NT, and several
  66. variants of Unix. Each implementation shares common code as well 
  67. as having platform dependent portions. This standard describes how
  68. the model dependent portions are to be implemented.
  69.  
  70. In header file pr/include/primpl.h, each publicly declared 
  71. platform dependent function is declared as:
  72.  
  73. PR_EXTERN void _PR_MD_FUNCTION( long arg1, long arg2 );
  74. #define _PR_MD_FUNCTION _MD_FUNCTION
  75.  
  76. In header file pr/include/md/<platform>/_<platform>.h, 
  77. each #define'd macro is redefined as one of:
  78.  
  79. #define _MD_FUNCTION <blanks>
  80. #define _MD_FUNCTION <expanded macro>
  81. #define _MD_FUNCTION <osFunction>
  82. #define _MD_FUNCTION <_MD_Function>
  83.  
  84. Where:
  85.  
  86. <blanks> is no definition at all. In this case, the function is not implemented 
  87. and is never called for this platform. 
  88. For example: 
  89. #define _MD_INIT_CPUS()
  90.  
  91. <expanded macro> is a C language macro expansion. 
  92. For example: 
  93. #define        _MD_CLEAN_THREAD(_thread) \
  94.     PR_BEGIN_MACRO \
  95.         PR_DestroyCondVar(_thread->md.asyncIOCVar); \
  96.         PR_DestroyLock(_thread->md.asyncIOLock); \
  97.     PR_END_MACRO
  98.  
  99. <osFunction> is some function implemented by the host operating system. 
  100. For example: 
  101. #define _MD_EXIT        exit
  102.  
  103. <_MD_function> is the name of a function implemented for this platform in 
  104. pr/src/md/<platform>/<soruce>.c file. 
  105. For example: 
  106. #define        _MD_GETFILEINFO         _MD_GetFileInfo
  107.  
  108. In <source>.c, the implementation is:
  109. PR_IMPLEMENT(PRInt32) _MD_GetFileInfo(const char *fn, PRFileInfo *info);
  110. */
  111.  
  112. PR_BEGIN_EXTERN_C
  113.  
  114. typedef struct _MDLock _MDLock;
  115. typedef struct _MDCVar _MDCVar;
  116. typedef struct _MDSegment _MDSegment;
  117. typedef struct _MDThread _MDThread;
  118. typedef struct _MDThreadStack _MDThreadStack;
  119. typedef struct _MDSemaphore _MDSemaphore;
  120. typedef struct _MDDir _MDDir;
  121. typedef struct _MDFileDesc _MDFileDesc;
  122. typedef struct _MDProcess _MDProcess;
  123. typedef struct _MDFileMap _MDFileMap;
  124.  
  125. #if defined(_PR_PTHREADS)
  126.  
  127. /*
  128. ** The following definitions are unique to implementing NSPR using pthreads.
  129. ** Since pthreads defines most of the thread and thread synchronization
  130. ** stuff, this is a pretty small set.
  131. */
  132.  
  133. #define PT_CV_NOTIFIED_LENGTH 6
  134. typedef struct _PT_Notified _PT_Notified;
  135. struct _PT_Notified
  136. {
  137.     PRIntn length;              /* # of used entries in this structure */
  138.     struct
  139.     {
  140.         PRCondVar *cv;          /* the condition variable notified */
  141.         PRIntn times;           /* and the number of times notified */
  142.     } cv[PT_CV_NOTIFIED_LENGTH];
  143.     _PT_Notified *link;         /* link to another of these | NULL */
  144. };
  145.  
  146. /*
  147.  * bits defined for pthreads 'state' field 
  148.  */
  149. #define PT_THREAD_DETACHED  0x01    /* thread can't be joined */
  150. #define PT_THREAD_GLOBAL    0x02    /* a global thread (unlikely) */
  151. #define PT_THREAD_SYSTEM    0x04    /* system (not user) thread */
  152. #define PT_THREAD_PRIMORD   0x08    /* this is the primordial thread */
  153. #define PT_THREAD_ABORTED   0x10    /* thread has been interrupted */
  154. #define PT_THREAD_GCABLE    0x20    /* thread is garbage collectible */
  155. #define PT_THREAD_SUSPENDED 0x40    /* thread has been suspended */
  156. #define PT_THREAD_FOREIGN   0x80    /* thread is not one of ours */
  157.  
  158. /* 
  159. ** Possible values for thread's suspend field
  160. ** Note that the first two can be the same as they are really mutually exclusive,
  161. ** i.e. both cannot be happening at the same time. We have two symbolic names
  162. ** just as a mnemonic.
  163. **/
  164. #define PT_THREAD_RESUMED   0x80    /* thread has been resumed */
  165. #define PT_THREAD_SETGCABLE 0x100   /* set the GCAble flag */
  166.  
  167. #if defined(DEBUG)
  168.  
  169. typedef struct PTDebug
  170. {
  171.     PRTime timeStarted;
  172.     PRUintn predictionsFoiled;
  173.     PRUintn pollingListMax;
  174.     PRUintn continuationsServed;
  175.     PRUintn recyclesNeeded;
  176.     PRUintn quiescentIO;
  177.     PRUintn locks_created, locks_destroyed;
  178.     PRUintn locks_acquired, locks_released;
  179.     PRUintn cvars_created, cvars_destroyed;
  180.     PRUintn cvars_notified, delayed_cv_deletes;
  181. } PTDebug;
  182.  
  183. PR_EXTERN(void) PT_GetStats(PTDebug* here);
  184. PR_EXTERN(void) PT_FPrintStats(PRFileDesc *fd, const char *msg);
  185.  
  186. #else
  187.  
  188. typedef PRUintn PTDebug;
  189. #define PT_GetStats(_p)
  190. #define PT_FPrintStats(_fd, _msg)
  191.  
  192. #endif /* defined(DEBUG) */
  193.  
  194. #else /* defined(_PR_PTHREADS) */
  195.  
  196. typedef PRUintn PTDebug;
  197. #define PT_GetStats(_p)
  198. #define PT_FPrintStats(_fd, _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)
  278. #define _MD_GET_ATTACHED_THREAD()        (_PR_MD_CURRENT_THREAD())
  279. #endif
  280.  
  281. #ifdef _PR_LOCAL_THREADS_ONLY 
  282.  
  283. PR_EXTERN(struct _PRCPU *)              _pr_currentCPU;
  284. PR_EXTERN(PRThread *)                   _pr_currentThread;
  285. PR_EXTERN(PRThread *)                   _pr_lastThread;
  286. PR_EXTERN(PRInt32)                      _pr_intsOff;
  287.  
  288. #define _MD_CURRENT_CPU()               (_pr_currentCPU)
  289. #define _MD_SET_CURRENT_CPU(_cpu)       (_pr_currentCPU = (_cpu))
  290. #define _MD_CURRENT_THREAD()            (_pr_currentThread)
  291. #define _MD_SET_CURRENT_THREAD(_thread) (_pr_currentThread = (_thread))
  292. #define _MD_LAST_THREAD()               (_pr_lastThread)
  293. #define _MD_SET_LAST_THREAD(t)          (_pr_lastThread = t)
  294.  
  295. #define _MD_GET_INTSOFF()               (_pr_intsOff)
  296. #define _MD_SET_INTSOFF(_val)           (_pr_intsOff = _val)
  297.  
  298.  
  299. /* The unbalanced curly braces in these two macros are intentional */
  300. #define _PR_LOCK_HEAP() { PRIntn _is; if (_pr_currentCPU) _PR_INTSOFF(_is);
  301. #define _PR_UNLOCK_HEAP() if (_pr_currentCPU) _PR_INTSON(_is); }
  302.  
  303. #endif /* _PR_LOCAL_THREADS_ONLY */
  304.  
  305. #if defined(_PR_GLOBAL_THREADS_ONLY)
  306.  
  307. #define _MD_GET_INTSOFF() 0
  308. #define _MD_SET_INTSOFF(_val)
  309. #define _PR_INTSOFF(_is)
  310. #define _PR_FAST_INTSON(_is)
  311. #define _PR_INTSON(_is)
  312. #define _PR_THREAD_LOCK(_thread)
  313. #define _PR_THREAD_UNLOCK(_thread)
  314. #define _PR_RUNQ_LOCK(cpu)
  315. #define _PR_RUNQ_UNLOCK(cpu)
  316. #define _PR_SLEEPQ_LOCK(thread)
  317. #define _PR_SLEEPQ_UNLOCK(thread)
  318. #define _PR_MISCQ_LOCK(thread)
  319. #define _PR_MISCQ_UNLOCK(thread)
  320. #define _PR_CPU_LIST_LOCK()
  321. #define _PR_CPU_LIST_UNLOCK()
  322.  
  323. #define _PR_ADD_RUNQ(_thread, _cpu, _pri)
  324. #define _PR_DEL_RUNQ(_thread)
  325. #define _PR_ADD_SLEEPQ(_thread, _timeout)
  326. #define _PR_DEL_SLEEPQ(_thread, _propogate)
  327. #define _PR_ADD_JOINQ(_thread, _cpu)
  328. #define _PR_DEL_JOINQ(_thread)
  329. #define _PR_ADD_SUSPENDQ(_thread, _cpu)
  330. #define _PR_DEL_SUSPENDQ(_thread)
  331.  
  332. #define _PR_THREAD_SWITCH_CPU(_thread, _newCPU)
  333.  
  334. #define _PR_IS_NATIVE_THREAD(thread) 1
  335. #define _PR_IS_NATIVE_THREAD_SUPPORTED() 1
  336.  
  337. #else
  338.  
  339. #define _PR_INTSOFF(_is) \
  340.     PR_BEGIN_MACRO \
  341.         (_is) = _PR_MD_GET_INTSOFF(); \
  342.         _PR_MD_SET_INTSOFF(1); \
  343.     PR_END_MACRO
  344.  
  345. #define _PR_FAST_INTSON(_is) \
  346.     PR_BEGIN_MACRO \
  347.         _PR_MD_SET_INTSOFF(_is); \
  348.     PR_END_MACRO
  349.  
  350. #define _PR_INTSON(_is) \
  351.     PR_BEGIN_MACRO \
  352.         if ((_is == 0) && (_PR_MD_CURRENT_CPU())->u.bits) \
  353.                 _PR_IntsOn((_PR_MD_CURRENT_CPU())); \
  354.         _PR_MD_SET_INTSOFF(_is); \
  355.     PR_END_MACRO
  356.  
  357. #ifdef _PR_LOCAL_THREADS_ONLY 
  358.  
  359. #define _PR_IS_NATIVE_THREAD(thread) 0
  360. #define _PR_THREAD_LOCK(_thread)
  361. #define _PR_THREAD_UNLOCK(_thread)
  362. #define _PR_RUNQ_LOCK(cpu)
  363. #define _PR_RUNQ_UNLOCK(cpu)
  364. #define _PR_SLEEPQ_LOCK(thread)
  365. #define _PR_SLEEPQ_UNLOCK(thread)
  366. #define _PR_MISCQ_LOCK(thread)
  367. #define _PR_MISCQ_UNLOCK(thread)
  368. #define _PR_CPU_LIST_LOCK()
  369. #define _PR_CPU_LIST_UNLOCK()
  370.  
  371. #define _PR_ADD_RUNQ(_thread, _cpu, _pri) \
  372.     PR_BEGIN_MACRO \
  373.     PR_APPEND_LINK(&(_thread)->links, &_PR_RUNQ(_cpu)[_pri]); \
  374.     _PR_RUNQREADYMASK(_cpu) |= (1L << _pri); \
  375.     PR_END_MACRO
  376.  
  377. #define _PR_DEL_RUNQ(_thread) \
  378.     PR_BEGIN_MACRO \
  379.     _PRCPU *_cpu = _thread->cpu; \
  380.     PRInt32 _pri = _thread->priority; \
  381.     PR_REMOVE_LINK(&(_thread)->links); \
  382.     if (PR_CLIST_IS_EMPTY(&_PR_RUNQ(_cpu)[_pri])) \
  383.         _PR_RUNQREADYMASK(_cpu) &= ~(1L << _pri); \
  384.     PR_END_MACRO
  385.  
  386. #define _PR_ADD_SLEEPQ(_thread, _timeout) \
  387.     _PR_AddSleepQ(_thread, _timeout);   
  388.  
  389. #define _PR_DEL_SLEEPQ(_thread, _propogate) \
  390.     _PR_DelSleepQ(_thread, _propogate);  
  391.  
  392. #define _PR_ADD_JOINQ(_thread, _cpu) \
  393.     PR_APPEND_LINK(&(_thread)->links, &_PR_WAITINGTOJOINQ(_cpu));
  394.  
  395. #define _PR_DEL_JOINQ(_thread) \
  396.     PR_REMOVE_LINK(&(_thread)->links);
  397.  
  398. #define _PR_ADD_SUSPENDQ(_thread, _cpu) \
  399.     PR_APPEND_LINK(&(_thread)->links, &_PR_SUSPENDQ(_cpu));
  400.  
  401. #define _PR_DEL_SUSPENDQ(_thread) \
  402.     PR_REMOVE_LINK(&(_thread)->links);
  403.  
  404. #define _PR_THREAD_SWITCH_CPU(_thread, _newCPU)
  405.  
  406. #define _PR_IS_NATIVE_THREAD_SUPPORTED() 0
  407.  
  408. #else        /* _PR_LOCAL_THREADS_ONLY */
  409.  
  410. /* These are for the "combined" thread model */
  411.  
  412. #define _PR_THREAD_LOCK(_thread) \
  413.     _PR_MD_LOCK(&(_thread)->threadLock);
  414.  
  415. #define _PR_THREAD_UNLOCK(_thread) \
  416.     _PR_MD_UNLOCK(&(_thread)->threadLock);
  417.  
  418. #define _PR_RUNQ_LOCK(_cpu) \
  419.     PR_BEGIN_MACRO \
  420.     _PR_MD_LOCK(&(_cpu)->queue->runQLock );\
  421.     PR_END_MACRO
  422.     
  423. #define _PR_RUNQ_UNLOCK(_cpu) \
  424.     PR_BEGIN_MACRO \
  425.     _PR_MD_UNLOCK(&(_cpu)->queue->runQLock );\
  426.     PR_END_MACRO
  427.  
  428. #define _PR_SLEEPQ_LOCK(_cpu) \
  429.     _PR_MD_LOCK(&(_cpu)->queue->sleepQLock );
  430.  
  431. #define _PR_SLEEPQ_UNLOCK(_cpu) \
  432.     _PR_MD_UNLOCK(&(_cpu)->queue->sleepQLock );
  433.  
  434. #define _PR_MISCQ_LOCK(_cpu) \
  435.     _PR_MD_LOCK(&(_cpu)->queue->miscQLock );
  436.  
  437. #define _PR_MISCQ_UNLOCK(_cpu) \
  438.     _PR_MD_UNLOCK(&(_cpu)->queue->miscQLock );
  439.  
  440. #define _PR_CPU_LIST_LOCK()                 _PR_MD_LOCK(&_pr_cpuLock)
  441. #define _PR_CPU_LIST_UNLOCK()               _PR_MD_UNLOCK(&_pr_cpuLock)
  442.  
  443. #define QUEUE_RUN           0x1
  444. #define QUEUE_SLEEP         0x2
  445. #define QUEUE_JOIN          0x4
  446. #define QUEUE_SUSPEND       0x8
  447. #define QUEUE_LOCK          0x10
  448.  
  449. #define _PR_ADD_RUNQ(_thread, _cpu, _pri) \
  450.     PR_BEGIN_MACRO \
  451.     PR_APPEND_LINK(&(_thread)->links, &_PR_RUNQ(_cpu)[_pri]); \
  452.     _PR_RUNQREADYMASK(_cpu) |= (1L << _pri); \
  453.     PR_ASSERT((_thread)->queueCount == 0); \
  454.     (_thread)->queueCount = QUEUE_RUN; \
  455.     PR_END_MACRO
  456.  
  457. #define _PR_DEL_RUNQ(_thread) \
  458.     PR_BEGIN_MACRO \
  459.     _PRCPU *_cpu = _thread->cpu; \
  460.     PRInt32 _pri = _thread->priority; \
  461.     PR_REMOVE_LINK(&(_thread)->links); \
  462.     if (PR_CLIST_IS_EMPTY(&_PR_RUNQ(_cpu)[_pri])) \
  463.         _PR_RUNQREADYMASK(_cpu) &= ~(1L << _pri); \
  464.     PR_ASSERT((_thread)->queueCount == QUEUE_RUN);\
  465.     (_thread)->queueCount = 0; \
  466.     PR_END_MACRO
  467.  
  468. #define _PR_ADD_SLEEPQ(_thread, _timeout) \
  469.     PR_ASSERT((_thread)->queueCount == 0); \
  470.     (_thread)->queueCount = QUEUE_SLEEP; \
  471.     _PR_AddSleepQ(_thread, _timeout);  
  472.  
  473. #define _PR_DEL_SLEEPQ(_thread, _propogate) \
  474.     PR_ASSERT((_thread)->queueCount == QUEUE_SLEEP);\
  475.     (_thread)->queueCount = 0; \
  476.     _PR_DelSleepQ(_thread, _propogate);  
  477.  
  478. #define _PR_ADD_JOINQ(_thread, _cpu) \
  479.     PR_ASSERT((_thread)->queueCount == 0); \
  480.     (_thread)->queueCount = QUEUE_JOIN; \
  481.     PR_APPEND_LINK(&(_thread)->links, &_PR_WAITINGTOJOINQ(_cpu));
  482.  
  483. #define _PR_DEL_JOINQ(_thread) \
  484.     PR_ASSERT((_thread)->queueCount == QUEUE_JOIN);\
  485.     (_thread)->queueCount = 0; \
  486.     PR_REMOVE_LINK(&(_thread)->links);
  487.  
  488. #define _PR_ADD_SUSPENDQ(_thread, _cpu) \
  489.     PR_ASSERT((_thread)->queueCount == 0); \
  490.     (_thread)->queueCount = QUEUE_SUSPEND; \
  491.     PR_APPEND_LINK(&(_thread)->links, &_PR_SUSPENDQ(_cpu));
  492.  
  493. #define _PR_DEL_SUSPENDQ(_thread) \
  494.     PR_ASSERT((_thread)->queueCount == QUEUE_SUSPEND);\
  495.     (_thread)->queueCount = 0; \
  496.     PR_REMOVE_LINK(&(_thread)->links);
  497.  
  498. #define _PR_THREAD_SWITCH_CPU(_thread, _newCPU) \
  499.     (_thread)->cpu = (_newCPU);
  500.  
  501. #define _PR_IS_NATIVE_THREAD(thread) (thread->flags & _PR_GLOBAL_SCOPE)
  502. #define _PR_IS_NATIVE_THREAD_SUPPORTED() 1
  503.  
  504. #endif /* _PR_LOCAL_THREADS_ONLY */
  505.  
  506. #endif /* _PR_GLOBAL_THREADS_ONLY */
  507.  
  508. #define _PR_SET_RESCHED_FLAG() _PR_MD_CURRENT_CPU()->u.missed[3] = 1
  509. #define _PR_CLEAR_RESCHED_FLAG() _PR_MD_CURRENT_CPU()->u.missed[3] = 0
  510.  
  511. extern _PRInterruptTable _pr_interruptTable[];
  512.  
  513. /* Bits for _pr_interruptState.u.missed[0,1] */
  514. #define _PR_MISSED_CLOCK    0x1
  515. #define _PR_MISSED_IO        0x2
  516. #define _PR_MISSED_CHILD    0x4
  517.  
  518. extern void _PR_IntsOn(_PRCPU *cpu);
  519.  
  520. PR_EXTERN(void) _PR_WakeupCPU(void);
  521. PR_EXTERN(void) _PR_PauseCPU(void);
  522.  
  523. /************************************************************************/
  524.  
  525. #define _PR_LOCK_LOCK(_lock) \
  526.     _PR_MD_LOCK(&(_lock)->ilock);
  527. #define _PR_LOCK_UNLOCK(_lock) \
  528.     _PR_MD_UNLOCK(&(_lock)->ilock);
  529.     
  530. extern PRThread * _PR_AssignLock(PRLock *lock);
  531.  
  532. #define _PR_LOCK_PTR(_qp) \
  533.     ((PRLock*) ((char*) (_qp) - offsetof(PRLock,links)))
  534.  
  535. /************************************************************************/
  536.  
  537. #define _PR_CVAR_LOCK(_cvar) \
  538.     _PR_MD_LOCK(&(_cvar)->ilock); 
  539. #define _PR_CVAR_UNLOCK(_cvar) \
  540.     _PR_MD_UNLOCK(&(_cvar)->ilock);
  541.  
  542. extern PRStatus _PR_WaitCondVar(
  543.     PRThread *thread, PRCondVar *cvar, PRLock *lock, PRIntervalTime timeout);
  544. extern PRUint32 _PR_CondVarToString(PRCondVar *cvar, char *buf, PRUint32 buflen);
  545.  
  546. PR_EXTERN(void) _PR_Notify(PRMonitor *mon, PRBool all, PRBool sticky);
  547.  
  548. typedef struct _PRPerThreadExit {
  549.     PRThreadExit func;
  550.     void *arg;
  551. } _PRPerThreadExit;
  552.  
  553. /* PRThread.flags */
  554. #define _PR_SYSTEM          0x01
  555. #define _PR_INTERRUPT       0x02
  556. #define _PR_ATTACHED        0x04        /* created via PR_AttachThread */
  557. #define _PR_PRIMORDIAL      0x08        /* the thread that called PR_Init */
  558. #define _PR_ON_SLEEPQ       0x10        /* thread is on the sleepQ */
  559. #define _PR_ON_PAUSEQ       0x20        /* thread is on the pauseQ */
  560. #define _PR_SUSPENDING      0x40        /* thread wants to suspend */
  561. #define _PR_GLOBAL_SCOPE    0x80        /* thread is global scope */
  562. #define _PR_IDLE_THREAD     0x200       /* this is an idle thread        */
  563. #define _PR_GCABLE_THREAD   0x400       /* this is a collectable thread */
  564. #define _PR_BOUND_THREAD    0x800       /* a bound thread */
  565.  
  566. /* PRThread.state */
  567. #define _PR_UNBORN       0
  568. #define _PR_RUNNABLE     1
  569. #define _PR_RUNNING      2
  570. #define _PR_LOCK_WAIT    3
  571. #define _PR_COND_WAIT    4
  572. #define _PR_JOIN_WAIT    5
  573. #define _PR_IO_WAIT      6
  574. #define _PR_SUSPENDED    7
  575. #define _PR_DEAD_STATE   8  /* for debugging */
  576.  
  577. /* PRThreadStack.flags */
  578. #define _PR_STACK_VM            0x1    /* using vm instead of malloc */
  579. #define _PR_STACK_MAPPED        0x2    /* vm is mapped */
  580. #define _PR_STACK_PRIMORDIAL    0x4    /* stack for primordial thread */
  581.  
  582. /* 
  583. ** If the default stcksize from the client is zero, we need to pick a machine
  584. ** dependent value.  This is only for standard user threads.  For custom threads,
  585. ** 0 has a special meaning.
  586. ** Adjust stackSize. Round up to a page boundary.
  587. */
  588. #if (!defined(HAVE_CUSTOM_USER_THREADS))
  589. #define        _PR_ADJUST_STACKSIZE(stackSize) \
  590.         PR_BEGIN_MACRO \
  591.     if (stackSize == 0) \
  592.                 stackSize = _MD_DEFAULT_STACK_SIZE; \
  593.     stackSize = (stackSize + (1 << _pr_pageShift) - 1) >> _pr_pageShift; \
  594.     stackSize <<= _pr_pageShift; \
  595.         PR_END_MACRO
  596. #else
  597. #define        _PR_ADJUST_STACKSIZE(stackSize)
  598. #endif
  599.  
  600. #define _PR_PENDING_INTERRUPT(_thread) ((_thread)->flags & _PR_INTERRUPT)
  601.  
  602. #define _PR_THREAD_PTR(_qp) \
  603.     ((PRThread*) ((char*) (_qp) - offsetof(PRThread,links)))
  604.  
  605. #define _PR_ACTIVE_THREAD_PTR(_qp) \
  606.     ((PRThread*) ((char*) (_qp) - offsetof(PRThread,active)))
  607.  
  608. #define _PR_THREAD_CONDQ_PTR(_qp) \
  609.     ((PRThread*) ((char*) (_qp) - offsetof(PRThread,waitQLinks)))
  610.  
  611. #define _PR_THREAD_MD_TO_PTR(_md) \
  612.     ((PRThread*) ((char*) (_md) - offsetof(PRThread,md)))
  613.  
  614. #define _PR_THREAD_STACK_TO_PTR(_stack) \
  615.     ((PRThread*) (_stack->thr))
  616.  
  617. extern PRCList _pr_active_local_threadQ;
  618. extern PRCList _pr_active_global_threadQ;
  619. extern PRCList _pr_cpuQ;
  620. extern _MDLock  _pr_cpuLock;
  621. extern PRInt32 _pr_md_idle_cpus;
  622.  
  623. #define _PR_ACTIVE_LOCAL_THREADQ()          _pr_active_local_threadQ
  624. #define _PR_ACTIVE_GLOBAL_THREADQ()         _pr_active_global_threadQ
  625. #define _PR_CPUQ()                          _pr_cpuQ
  626. #define _PR_RUNQ(_cpu)                      ((_cpu)->queue->runQ)
  627. #define _PR_RUNQREADYMASK(_cpu)             ((_cpu)->queue->runQReadyMask)
  628. #define _PR_SLEEPQ(_cpu)                    ((_cpu)->queue->sleepQ)
  629. #define _PR_SLEEPQMAX(_cpu)                 ((_cpu)->queue->sleepQmax)
  630. #define _PR_PAUSEQ(_cpu)                    ((_cpu)->queue->pauseQ)
  631. #define _PR_SUSPENDQ(_cpu)                  ((_cpu)->queue->suspendQ)
  632. #define _PR_WAITINGTOJOINQ(_cpu)            ((_cpu)->queue->waitingToJoinQ)
  633.  
  634. extern PRUint32 _pr_recycleThreads;   /* Flag for behavior on thread cleanup */
  635. extern PRLock *_pr_deadQLock;
  636. extern PRUint32 _pr_numNativeDead;
  637. extern PRUint32 _pr_numUserDead;
  638. extern PRCList _pr_deadNativeQ;
  639. extern PRCList _pr_deadUserQ;
  640. #define _PR_DEADNATIVEQ     _pr_deadNativeQ
  641. #define _PR_DEADUSERQ       _pr_deadUserQ
  642. #define _PR_DEADQ_LOCK      PR_Lock(_pr_deadQLock);
  643. #define _PR_DEADQ_UNLOCK    PR_Unlock(_pr_deadQLock);
  644. #define _PR_INC_DEADNATIVE  (_pr_numNativeDead++)
  645. #define _PR_DEC_DEADNATIVE  (_pr_numNativeDead--)
  646. #define _PR_NUM_DEADNATIVE  (_pr_numNativeDead)
  647. #define _PR_INC_DEADUSER    (_pr_numUserDead++)
  648. #define _PR_DEC_DEADUSER    (_pr_numUserDead--)
  649. #define _PR_NUM_DEADUSER    (_pr_numUserDead)
  650.  
  651. extern PRUint32 _pr_utid;
  652.  
  653. extern struct _PRCPU  *_pr_primordialCPU;
  654.  
  655. extern PRLock *_pr_activeLock;          /* lock for userActive and systemActive */
  656. extern PRInt32 _pr_userActive;          /* number of active user threads */
  657. extern PRInt32 _pr_systemActive;        /* number of active system threads */
  658. extern PRInt32 _pr_primordialExitCount; /* number of user threads left
  659.                                          * before the primordial thread
  660.                                          * can exit.  */
  661. extern PRCondVar *_pr_primordialExitCVar; /* the condition variable for
  662.                                            * notifying the primordial thread
  663.                                            * when all other user threads
  664.                                            * have terminated.  */
  665.  
  666. extern PRUintn _pr_maxPTDs;
  667.  
  668. extern PRLock *_pr_terminationCVLock;
  669.  
  670. /*************************************************************************
  671. * Internal routines either called by PR itself or from machine-dependent *
  672. * code.                                                                  *
  673. *************************************************************************/
  674.  
  675. extern void _PR_ClockInterrupt(void);
  676.  
  677. extern void _PR_Schedule(void);
  678. extern void _PR_SetThreadPriority(
  679.     PRThread* thread, PRThreadPriority priority);
  680. PR_EXTERN(void) _PR_Unlock(PRLock *lock);
  681.  
  682. PR_EXTERN(void) _PR_SuspendThread(PRThread *t);
  683. PR_EXTERN(void) _PR_ResumeThread(PRThread *t);
  684.  
  685. extern PRThreadStack * _PR_NewStack(PRUint32 stackSize);
  686. extern void _PR_FreeStack(PRThreadStack *stack);
  687. extern PRBool _PR_NotifyThread (PRThread *thread, PRThread *me);
  688. extern void _PR_NotifyLockedThread (PRThread *thread);
  689.  
  690. PR_EXTERN(void) _PR_AddSleepQ(PRThread *thread, PRIntervalTime timeout);
  691. PR_EXTERN(void) _PR_DelSleepQ(PRThread *thread, PRBool propogate_time);
  692.  
  693. extern void _PR_AddThreadToRunQ(PRThread *me, PRThread *thread);
  694.  
  695. PR_EXTERN(PRThread*) _PR_CreateThread(PRThreadType type,
  696.                                      void (*start)(void *arg),
  697.                                      void *arg,
  698.                                      PRThreadPriority priority,
  699.                                      PRThreadScope scope,
  700.                                      PRThreadState state,
  701.                                      PRUint32 stackSize,
  702.                      PRUint32 flags);
  703.  
  704. extern void _PR_NativeDestroyThread(PRThread *thread);
  705. extern void _PR_UserDestroyThread(PRThread *thread);
  706.  
  707. extern PRThread* _PRI_AttachThread(
  708.     PRThreadType type, PRThreadPriority priority,
  709.     PRThreadStack *stack, PRUint32 flags);
  710.  
  711. extern void _PRI_DetachThread(void);
  712.  
  713.  
  714. #define _PR_IO_PENDING(_thread) ((_thread)->io_pending)
  715.  
  716. PR_EXTERN(void) _PR_MD_INIT_CPUS();
  717. #define    _PR_MD_INIT_CPUS _MD_INIT_CPUS
  718.  
  719. PR_EXTERN(void) _PR_MD_WAKEUP_CPUS();
  720. #define    _PR_MD_WAKEUP_CPUS _MD_WAKEUP_CPUS
  721.  
  722. /* Interrupts related */
  723.  
  724. PR_EXTERN(void) _PR_MD_STOP_INTERRUPTS(void);
  725. #define    _PR_MD_STOP_INTERRUPTS _MD_STOP_INTERRUPTS
  726.  
  727. PR_EXTERN(void) _PR_MD_ENABLE_CLOCK_INTERRUPTS(void);
  728. #define    _PR_MD_ENABLE_CLOCK_INTERRUPTS _MD_ENABLE_CLOCK_INTERRUPTS
  729.  
  730. PR_EXTERN(void) _PR_MD_DISABLE_CLOCK_INTERRUPTS(void);
  731. #define    _PR_MD_DISABLE_CLOCK_INTERRUPTS _MD_DISABLE_CLOCK_INTERRUPTS
  732.  
  733. PR_EXTERN(void) _PR_MD_BLOCK_CLOCK_INTERRUPTS(void);
  734. #define    _PR_MD_BLOCK_CLOCK_INTERRUPTS _MD_BLOCK_CLOCK_INTERRUPTS
  735.  
  736. PR_EXTERN(void) _PR_MD_UNBLOCK_CLOCK_INTERRUPTS(void);
  737. #define    _PR_MD_UNBLOCK_CLOCK_INTERRUPTS _MD_UNBLOCK_CLOCK_INTERRUPTS
  738.  
  739. /* The _PR_MD_WAIT_LOCK and _PR_MD_WAKEUP_WAITER functions put to sleep and
  740.  * awaken a thread which is waiting on a lock or cvar.
  741.  */
  742. extern PRStatus _PR_MD_WAIT(PRThread *, PRIntervalTime timeout);
  743. #define    _PR_MD_WAIT _MD_WAIT
  744.  
  745. extern PRStatus _PR_MD_WAKEUP_WAITER(PRThread *);
  746. #define    _PR_MD_WAKEUP_WAITER _MD_WAKEUP_WAITER
  747.  
  748. #ifndef _PR_LOCAL_THREADS_ONLY /* not if only local threads supported */
  749. PR_EXTERN(void) _PR_MD_CLOCK_INTERRUPT(void);
  750. #define    _PR_MD_CLOCK_INTERRUPT _MD_CLOCK_INTERRUPT
  751. #endif
  752.  
  753. /* Stack debugging */
  754. PR_EXTERN(void) _PR_MD_INIT_STACK(PRThreadStack *ts, PRIntn redzone);
  755. #define    _PR_MD_INIT_STACK _MD_INIT_STACK
  756.  
  757. PR_EXTERN(void) _PR_MD_CLEAR_STACK(PRThreadStack* ts);
  758. #define    _PR_MD_CLEAR_STACK _MD_CLEAR_STACK
  759.  
  760. /* CPU related */
  761. PR_EXTERN(PRInt32) _PR_MD_GET_INTSOFF(void);
  762. #define    _PR_MD_GET_INTSOFF _MD_GET_INTSOFF
  763.  
  764. PR_EXTERN(void) _PR_MD_SET_INTSOFF(PRInt32 _val);
  765. #define    _PR_MD_SET_INTSOFF _MD_SET_INTSOFF
  766.  
  767. PR_EXTERN(_PRCPU*) _PR_MD_CURRENT_CPU(void);
  768. #define    _PR_MD_CURRENT_CPU _MD_CURRENT_CPU
  769.  
  770. PR_EXTERN(void) _PR_MD_SET_CURRENT_CPU(_PRCPU *cpu);
  771. #define    _PR_MD_SET_CURRENT_CPU _MD_SET_CURRENT_CPU
  772.  
  773. PR_EXTERN(void) _PR_MD_INIT_RUNNING_CPU(_PRCPU *cpu);
  774. #define    _PR_MD_INIT_RUNNING_CPU _MD_INIT_RUNNING_CPU
  775.  
  776. /*
  777.  * Returns the number of threads awoken or 0 if a timeout occurred;
  778.  */
  779. extern PRInt32 _PR_MD_PAUSE_CPU(PRIntervalTime timeout);
  780. #define    _PR_MD_PAUSE_CPU _MD_PAUSE_CPU
  781.  
  782. extern void _PR_MD_CLEANUP_BEFORE_EXIT(void);
  783. #define _PR_MD_CLEANUP_BEFORE_EXIT _MD_CLEANUP_BEFORE_EXIT
  784.  
  785. extern void _PR_MD_EXIT(PRIntn status);
  786. #define    _PR_MD_EXIT _MD_EXIT
  787.  
  788. /* Locks related */
  789.  
  790. PR_EXTERN(PRStatus) _PR_MD_NEW_LOCK(_MDLock *md);
  791. #define    _PR_MD_NEW_LOCK _MD_NEW_LOCK
  792.  
  793. PR_EXTERN(void) _PR_MD_FREE_LOCK(_MDLock *md);
  794. #define    _PR_MD_FREE_LOCK _MD_FREE_LOCK
  795.  
  796. PR_EXTERN(void) _PR_MD_LOCK(_MDLock *md);
  797. #define    _PR_MD_LOCK _MD_LOCK
  798.  
  799. PR_EXTERN(PRBool) _PR_MD_TEST_AND_LOCK(_MDLock *md);
  800. #define    _PR_MD_TEST_AND_LOCK _MD_TEST_AND_LOCK
  801.  
  802. PR_EXTERN(void) _PR_MD_UNLOCK(_MDLock *md);
  803. #define    _PR_MD_UNLOCK _MD_UNLOCK
  804.  
  805. PR_EXTERN(void) _PR_MD_IOQ_LOCK(void);
  806. #define    _PR_MD_IOQ_LOCK _MD_IOQ_LOCK
  807.  
  808. PR_EXTERN(void) _PR_MD_IOQ_UNLOCK(void);
  809. #define    _PR_MD_IOQ_UNLOCK _MD_IOQ_UNLOCK
  810.  
  811. #ifndef _PR_LOCAL_THREADS_ONLY /* not if only local threads supported */
  812. /* Semaphore related -- only for native threads */
  813. #ifdef HAVE_CVAR_BUILT_ON_SEM
  814. PR_EXTERN(void) _PR_MD_NEW_SEM(_MDSemaphore *md, PRUintn value);
  815. #define _PR_MD_NEW_SEM _MD_NEW_SEM
  816.  
  817. PR_EXTERN(void) _PR_MD_DESTROY_SEM(_MDSemaphore *md);
  818. #define _PR_MD_DESTROY_SEM _MD_DESTROY_SEM
  819.  
  820. PR_EXTERN(PRStatus) _PR_MD_TIMED_WAIT_SEM(
  821.     _MDSemaphore *md, PRIntervalTime timeout);
  822. #define _PR_MD_TIMED_WAIT_SEM _MD_TIMED_WAIT_SEM
  823.  
  824. PR_EXTERN(PRStatus) _PR_MD_WAIT_SEM(_MDSemaphore *md);
  825. #define _PR_MD_WAIT_SEM _MD_WAIT_SEM
  826.  
  827. PR_EXTERN(void) _PR_MD_POST_SEM(_MDSemaphore *md);
  828. #define _PR_MD_POST_SEM _MD_POST_SEM
  829. #endif /* HAVE_CVAR_BUILT_ON_SEM */
  830.  
  831. #endif
  832.  
  833. /* Condition Variables related -- only for native threads */
  834.  
  835. #ifndef _PR_LOCAL_THREADS_ONLY /* not if only local threads supported */
  836. PR_EXTERN(PRInt32) _PR_MD_NEW_CV(_MDCVar *md);
  837. #define    _PR_MD_NEW_CV _MD_NEW_CV
  838.  
  839. PR_EXTERN(void) _PR_MD_FREE_CV(_MDCVar *md);
  840. #define    _PR_MD_FREE_CV _MD_FREE_CV
  841.  
  842. PR_EXTERN(void) _PR_MD_WAIT_CV(
  843.     _MDCVar *mdCVar,_MDLock *mdLock,PRIntervalTime timeout);
  844. #define    _PR_MD_WAIT_CV _MD_WAIT_CV
  845.  
  846. PR_EXTERN(void) _PR_MD_NOTIFY_CV(_MDCVar *md, _MDLock *lock);
  847. #define    _PR_MD_NOTIFY_CV _MD_NOTIFY_CV
  848.  
  849. PR_EXTERN(void) _PR_MD_NOTIFYALL_CV(_MDCVar *md, _MDLock *lock);
  850. #define    _PR_MD_NOTIFYALL_CV _MD_NOTIFYALL_CV
  851. #endif /* _PR_LOCAL_THREADS_ONLY */
  852.  
  853. /* Threads related */
  854. PR_EXTERN(PRThread*) _PR_MD_CURRENT_THREAD(void);
  855. #define    _PR_MD_CURRENT_THREAD _MD_CURRENT_THREAD
  856.  
  857. PR_EXTERN(PRThread*) _PR_MD_GET_ATTACHED_THREAD(void);
  858. #define    _PR_MD_GET_ATTACHED_THREAD _MD_GET_ATTACHED_THREAD
  859.  
  860. PR_EXTERN(PRThread*) _PR_MD_LAST_THREAD(void);
  861. #define    _PR_MD_LAST_THREAD _MD_LAST_THREAD
  862.  
  863. PR_EXTERN(void) _PR_MD_SET_CURRENT_THREAD(PRThread *thread);
  864. #define    _PR_MD_SET_CURRENT_THREAD _MD_SET_CURRENT_THREAD
  865.  
  866. PR_EXTERN(void) _PR_MD_SET_LAST_THREAD(PRThread *thread);
  867. #define    _PR_MD_SET_LAST_THREAD _MD_SET_LAST_THREAD
  868.  
  869. extern PRStatus _PR_MD_INIT_THREAD(PRThread *thread);
  870. #define    _PR_MD_INIT_THREAD _MD_INIT_THREAD
  871.  
  872. extern void _PR_MD_EXIT_THREAD(PRThread *thread);
  873. #define    _PR_MD_EXIT_THREAD _MD_EXIT_THREAD
  874.  
  875. #ifndef _PR_LOCAL_THREADS_ONLY /* not if only local threads supported */
  876.  
  877. PR_EXTERN(PRStatus) _PR_MD_INIT_ATTACHED_THREAD(PRThread *thread);
  878. #define    _PR_MD_INIT_ATTACHED_THREAD _MD_INIT_ATTACHED_THREAD
  879.  
  880. extern void _PR_MD_SUSPEND_THREAD(PRThread *thread);
  881. #define    _PR_MD_SUSPEND_THREAD _MD_SUSPEND_THREAD
  882.  
  883. extern void _PR_MD_RESUME_THREAD(PRThread *thread);
  884. #define    _PR_MD_RESUME_THREAD _MD_RESUME_THREAD
  885.  
  886. extern void _PR_MD_SUSPEND_CPU(_PRCPU  *cpu);
  887. #define    _PR_MD_SUSPEND_CPU _MD_SUSPEND_CPU
  888.  
  889. extern void _PR_MD_RESUME_CPU(_PRCPU  *cpu);
  890. #define    _PR_MD_RESUME_CPU _MD_RESUME_CPU
  891.  
  892. extern void _PR_MD_BEGIN_SUSPEND_ALL(void);
  893. #define    _PR_MD_BEGIN_SUSPEND_ALL _MD_BEGIN_SUSPEND_ALL
  894.  
  895. extern void _PR_MD_END_SUSPEND_ALL(void);
  896. #define    _PR_MD_END_SUSPEND_ALL _MD_END_SUSPEND_ALL
  897.  
  898. extern void _PR_MD_BEGIN_RESUME_ALL(void);
  899. #define    _PR_MD_BEGIN_RESUME_ALL _MD_BEGIN_RESUME_ALL
  900.  
  901. extern void _PR_MD_END_RESUME_ALL(void);
  902. #define    _PR_MD_END_RESUME_ALL _MD_END_RESUME_ALL
  903.  
  904. #if defined(IRIX) 
  905. PR_EXTERN(void) _PR_IRIX_CHILD_PROCESS(void);
  906. #endif        /* IRIX */
  907.  
  908. #endif        /* !_PR_LOCAL_THREADS_ONLY */
  909.  
  910. extern void _PR_MD_CLEAN_THREAD(PRThread *thread);
  911. #define    _PR_MD_CLEAN_THREAD _MD_CLEAN_THREAD
  912.  
  913. #ifdef HAVE_CUSTOM_USER_THREADS
  914. extern void _PR_MD_CREATE_PRIMORDIAL_USER_THREAD(PRThread *);
  915. #define    _PR_MD_CREATE_PRIMORDIAL_USER_THREAD _MD_CREATE_PRIMORDIAL_USER_THREAD
  916.  
  917. extern PRThread* _PR_MD_CREATE_USER_THREAD(
  918.                         PRUint32 stacksize,
  919.                         void (*start)(void *),
  920.                         void *arg);
  921. #define    _PR_MD_CREATE_USER_THREAD _MD_CREATE_USER_THREAD
  922. #endif
  923.  
  924. extern void _PR_MD_INIT_PRIMORDIAL_THREAD(PRThread *thread);
  925. #define _PR_MD_INIT_PRIMORDIAL_THREAD _MD_INIT_PRIMORDIAL_THREAD
  926.  
  927. extern PRStatus _PR_MD_CREATE_THREAD(
  928.                         PRThread *thread, 
  929.                         void (*start) (void *), 
  930.                         PRThreadPriority priority,                      
  931.                         PRThreadScope scope,
  932.                         PRThreadState state,
  933.                         PRUint32 stackSize);
  934. #define    _PR_MD_CREATE_THREAD _MD_CREATE_THREAD
  935.  
  936. extern void _PR_MD_YIELD(void);
  937. #define    _PR_MD_YIELD _MD_YIELD
  938.  
  939. extern void _PR_MD_SET_PRIORITY(_MDThread *md, PRThreadPriority newPri);
  940. #define    _PR_MD_SET_PRIORITY _MD_SET_PRIORITY
  941.  
  942. PR_EXTERN(void) _PR_MD_SUSPENDALL(void);
  943. #define    _PR_MD_SUSPENDALL _MD_SUSPENDALL
  944.  
  945. PR_EXTERN(void) _PR_MD_RESUMEALL(void);
  946. #define    _PR_MD_RESUMEALL _MD_RESUMEALL
  947.  
  948. extern void _PR_MD_INIT_CONTEXT(
  949.     PRThread *thread, char *top, void (*start) (void), PRBool *status);
  950. #define    _PR_MD_INIT_CONTEXT _MD_INIT_CONTEXT
  951.  
  952. extern void _PR_MD_SWITCH_CONTEXT(PRThread *thread);
  953. #define    _PR_MD_SWITCH_CONTEXT _MD_SWITCH_CONTEXT
  954.  
  955. extern void _PR_MD_RESTORE_CONTEXT(PRThread *thread);
  956. #define    _PR_MD_RESTORE_CONTEXT _MD_RESTORE_CONTEXT
  957.  
  958. /* Directory enumeration related */
  959. extern PRStatus _PR_MD_OPEN_DIR(_MDDir *md,const char *name);
  960. #define    _PR_MD_OPEN_DIR _MD_OPEN_DIR
  961.  
  962. extern char * _PR_MD_READ_DIR(_MDDir *md, PRIntn flags);
  963. #define    _PR_MD_READ_DIR _MD_READ_DIR
  964.  
  965. extern PRInt32 _PR_MD_CLOSE_DIR(_MDDir *md);
  966. #define    _PR_MD_CLOSE_DIR _MD_CLOSE_DIR
  967.  
  968. /* I/O related */
  969. extern void _PR_MD_MAKE_NONBLOCK(PRFileDesc *fd);
  970. #define    _PR_MD_MAKE_NONBLOCK _MD_MAKE_NONBLOCK
  971.  
  972. /* File I/O related */
  973. extern PRInt32 _PR_MD_OPEN(const char *name, PRIntn osflags, PRIntn mode);
  974. #define    _PR_MD_OPEN _MD_OPEN
  975.  
  976. extern PRInt32 _PR_MD_CLOSE_FILE(PRInt32 osfd);
  977. #define    _PR_MD_CLOSE_FILE _MD_CLOSE_FILE
  978.  
  979. extern PRInt32 _PR_MD_READ(PRFileDesc *fd, void *buf, PRInt32 amount);
  980. #define    _PR_MD_READ _MD_READ
  981.  
  982. extern PRInt32 _PR_MD_WRITE(PRFileDesc *fd, const void *buf, PRInt32 amount);
  983. #define    _PR_MD_WRITE _MD_WRITE
  984.  
  985. extern PRInt32 _PR_MD_WRITEV(
  986.     PRFileDesc *fd, struct PRIOVec *iov,
  987.     PRInt32 iov_size, PRIntervalTime timeout);
  988. #define    _PR_MD_WRITEV _MD_WRITEV
  989.  
  990. extern PRInt32 _PR_MD_FSYNC(PRFileDesc *fd);
  991. #define    _PR_MD_FSYNC _MD_FSYNC
  992.  
  993. extern PRInt32 _PR_MD_DELETE(const char *name);
  994. #define        _PR_MD_DELETE _MD_DELETE
  995.  
  996. extern PRInt32 _PR_MD_RENAME(const char *from, const char *to);
  997. #define _PR_MD_RENAME _MD_RENAME
  998.  
  999. extern PRInt32 _PR_MD_ACCESS(const char *name, PRAccessHow how);
  1000. #define _PR_MD_ACCESS _MD_ACCESS
  1001.  
  1002. extern PRInt32 _PR_MD_STAT(const char *name, struct stat *buf);
  1003. #define _PR_MD_STAT _MD_STAT
  1004.  
  1005. extern PRInt32 _PR_MD_MKDIR(const char *name, PRIntn mode);
  1006. #define _PR_MD_MKDIR _MD_MKDIR
  1007.  
  1008. extern PRInt32 _PR_MD_RMDIR(const char *name);
  1009. #define _PR_MD_RMDIR _MD_RMDIR
  1010.  
  1011. /* Socket I/O related */
  1012. extern void _PR_MD_INIT_IO(void);
  1013. #define    _PR_MD_INIT_IO _MD_INIT_IO
  1014.  
  1015. extern PRInt32 _PR_MD_CLOSE_SOCKET(PRInt32 osfd);
  1016. #define    _PR_MD_CLOSE_SOCKET _MD_CLOSE_SOCKET
  1017.  
  1018. extern PRInt32 _PR_MD_CONNECT(
  1019.     PRFileDesc *fd, const PRNetAddr *addr,
  1020.     PRUint32 addrlen, PRIntervalTime timeout);
  1021. #define    _PR_MD_CONNECT _MD_CONNECT
  1022.  
  1023. extern PRInt32 _PR_MD_ACCEPT(
  1024.     PRFileDesc *fd, PRNetAddr *addr,
  1025.     PRUint32 *addrlen, PRIntervalTime timeout);
  1026. #define    _PR_MD_ACCEPT _MD_ACCEPT
  1027.  
  1028. extern PRInt32 _PR_MD_BIND(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen);
  1029. #define    _PR_MD_BIND _MD_BIND
  1030.  
  1031. extern PRInt32 _PR_MD_LISTEN(PRFileDesc *fd, PRIntn backlog);
  1032. #define    _PR_MD_LISTEN _MD_LISTEN
  1033.  
  1034. extern PRInt32 _PR_MD_SHUTDOWN(PRFileDesc *fd, PRIntn how);
  1035. #define    _PR_MD_SHUTDOWN _MD_SHUTDOWN
  1036.  
  1037. extern PRInt32 _PR_MD_RECV(PRFileDesc *fd, void *buf, PRInt32 amount, 
  1038.                                PRIntn flags, PRIntervalTime timeout);
  1039. #define    _PR_MD_RECV _MD_RECV
  1040.  
  1041. extern PRInt32 _PR_MD_SEND(
  1042.     PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, 
  1043.     PRIntervalTime timeout);
  1044. #define    _PR_MD_SEND _MD_SEND
  1045.  
  1046. extern PRInt32 _PR_MD_ACCEPT_READ(PRFileDesc *sd, PRInt32 *newSock, 
  1047.                                 PRNetAddr **raddr, void *buf, PRInt32 amount,
  1048.                                 PRIntervalTime timeout);
  1049. #define _PR_MD_ACCEPT_READ _MD_ACCEPT_READ
  1050. extern PRInt32 _PR_EmulateAcceptRead(PRFileDesc *sd, PRFileDesc **nd,
  1051.               PRNetAddr **raddr, void *buf, PRInt32 amount,
  1052.               PRIntervalTime timeout);
  1053.  
  1054. #ifdef WIN32
  1055. extern PRInt32 _PR_MD_FAST_ACCEPT(PRFileDesc *fd, PRNetAddr *addr, 
  1056.                                 PRUint32 *addrlen, PRIntervalTime timeout,
  1057.                                 PRBool fast,
  1058.                                 _PR_AcceptTimeoutCallback callback,
  1059.                                 void *callbackArg);
  1060.  
  1061. extern PRInt32 _PR_MD_FAST_ACCEPT_READ(PRFileDesc *sd, PRInt32 *newSock, 
  1062.                                 PRNetAddr **raddr, void *buf, PRInt32 amount,
  1063.                                 PRIntervalTime timeout, PRBool fast,
  1064.                                 _PR_AcceptTimeoutCallback callback,
  1065.                                 void *callbackArg);
  1066.  
  1067. extern void _PR_MD_UPDATE_ACCEPT_CONTEXT(PRInt32 s, PRInt32 ls);
  1068. #define _PR_MD_UPDATE_ACCEPT_CONTEXT _MD_UPDATE_ACCEPT_CONTEXT
  1069. #endif /* WIN32 */
  1070.  
  1071. extern PRInt32 _PR_MD_TRANSMITFILE(
  1072.     PRFileDesc *sock, PRFileDesc *file, 
  1073.     const void *headers, PRInt32 hlen, PRInt32 flags,
  1074.     PRIntervalTime timeout);
  1075. #define _PR_MD_TRANSMITFILE _MD_TRANSMITFILE
  1076. extern PRInt32 _PR_EmulateTransmitFile(PRFileDesc *sd, PRFileDesc *fd,
  1077.               const void *headers, PRInt32 hlen, PRTransmitFileFlags flags,
  1078.               PRIntervalTime timeout);
  1079.  
  1080. extern PRStatus _PR_MD_GETSOCKNAME(
  1081.     PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen);
  1082. #define    _PR_MD_GETSOCKNAME _MD_GETSOCKNAME
  1083.  
  1084. extern PRStatus _PR_MD_GETPEERNAME(
  1085.     PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen);
  1086. #define    _PR_MD_GETPEERNAME _MD_GETPEERNAME
  1087.  
  1088. extern PRStatus _PR_MD_GETSOCKOPT(
  1089.     PRFileDesc *fd, PRInt32 level, PRInt32 optname, char* optval, PRInt32* optlen);
  1090. #define    _PR_MD_GETSOCKOPT _MD_GETSOCKOPT
  1091.  
  1092. extern PRStatus _PR_MD_SETSOCKOPT(
  1093.     PRFileDesc *fd, PRInt32 level, PRInt32 optname,
  1094.     const char* optval, PRInt32 optlen);
  1095. #define    _PR_MD_SETSOCKOPT _MD_SETSOCKOPT
  1096.  
  1097. extern PRStatus PR_CALLBACK _PR_SocketGetSocketOption(
  1098.     PRFileDesc *fd, PRSocketOptionData *data);
  1099.  
  1100. extern PRStatus PR_CALLBACK _PR_SocketSetSocketOption(
  1101.     PRFileDesc *fd, const PRSocketOptionData *data);
  1102.  
  1103. extern PRInt32 _PR_MD_RECVFROM(
  1104.     PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,
  1105.     PRNetAddr *addr, PRUint32 *addrlen, PRIntervalTime timeout);
  1106. #define    _PR_MD_RECVFROM _MD_RECVFROM
  1107.  
  1108. extern PRInt32 _PR_MD_SENDTO(
  1109.     PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,
  1110.     const PRNetAddr *addr, PRUint32 addrlen, PRIntervalTime timeout);
  1111. #define    _PR_MD_SENDTO _MD_SENDTO
  1112.  
  1113. extern PRInt32 _PR_MD_SOCKETPAIR(int af, int type, int flags, PRInt32 *osfd);
  1114. #define    _PR_MD_SOCKETPAIR _MD_SOCKETPAIR
  1115.  
  1116. extern PRInt32 _PR_MD_SOCKET(int af, int type, int flags);
  1117. #define    _PR_MD_SOCKET _MD_SOCKET
  1118.  
  1119. extern PRInt32 _PR_MD_SOCKETAVAILABLE(PRFileDesc *fd);
  1120. #define    _PR_MD_SOCKETAVAILABLE _MD_SOCKETAVAILABLE
  1121.  
  1122. extern PRInt32 _PR_MD_PR_POLL(PRPollDesc *pds, PRIntn npds,
  1123.                                                                                         PRIntervalTime timeout);
  1124. #define    _PR_MD_PR_POLL _MD_PR_POLL
  1125.  
  1126.  
  1127. #define _PR_PROCESS_TIMEOUT_INTERRUPT_ERRORS(me) \
  1128.         if (_PR_PENDING_INTERRUPT(me)) { \
  1129.                 me->flags &= ~_PR_INTERRUPT; \
  1130.                 PR_SetError( PR_PENDING_INTERRUPT_ERROR, 0); \
  1131.         } else { \
  1132.                 PR_SetError(PR_IO_TIMEOUT_ERROR, 0); \
  1133.         }                                                        
  1134.                 
  1135. #ifndef NO_NSPR_10_SUPPORT
  1136.  
  1137. PR_EXTERN(void *) _PR_MD_GET_SP(PRThread *thread);
  1138. #define    _PR_MD_GET_SP _MD_GET_SP
  1139.  
  1140. #endif /* NO_NSPR_10_SUPPORT */
  1141.  
  1142. #endif /* defined(_PR_PTHREADS) */
  1143.  
  1144. /************************************************************************/
  1145. /*************************************************************************
  1146. ** The remainder of the definitions are shared by pthreads and the classic
  1147. ** NSPR code. These too may be conditionalized.
  1148. *************************************************************************/
  1149. /************************************************************************/
  1150.  
  1151. extern PRInt32 _PR_MD_LSEEK(PRFileDesc *fd, PRInt32 offset, PRSeekWhence whence);
  1152. #define    _PR_MD_LSEEK _MD_LSEEK
  1153.  
  1154. extern PRInt64 _PR_MD_LSEEK64(PRFileDesc *fd, PRInt64 offset, PRSeekWhence whence);
  1155. #define    _PR_MD_LSEEK64 _MD_LSEEK64
  1156.  
  1157. extern PRInt32 _PR_MD_GETFILEINFO(const char *fn, PRFileInfo *info);
  1158. #define _PR_MD_GETFILEINFO _MD_GETFILEINFO
  1159.  
  1160. extern PRInt32 _PR_MD_GETFILEINFO64(const char *fn, PRFileInfo64 *info);
  1161. #define _PR_MD_GETFILEINFO64 _MD_GETFILEINFO64
  1162.  
  1163. extern PRInt32 _PR_MD_GETOPENFILEINFO(const PRFileDesc *fd, PRFileInfo *info);
  1164. #define _PR_MD_GETOPENFILEINFO _MD_GETOPENFILEINFO
  1165.  
  1166. extern PRInt32 _PR_MD_GETOPENFILEINFO64(const PRFileDesc *fd, PRFileInfo64 *info);
  1167. #define _PR_MD_GETOPENFILEINFO64 _MD_GETOPENFILEINFO64
  1168.  
  1169.  
  1170. /*****************************************************************************/
  1171. /************************** File descriptor caching **************************/
  1172. /*****************************************************************************/
  1173. extern void _PR_InitFdCache(void);
  1174. extern void _PR_CleanupFdCache(void);
  1175. extern PRFileDesc *_PR_Getfd(void);
  1176. extern void _PR_Putfd(PRFileDesc *fd);
  1177.  
  1178. /*
  1179.  * These flags are used by NSPR temporarily in the poll
  1180.  * descriptor's out_flags field to record the mapping of
  1181.  * NSPR's poll flags to the system poll flags.
  1182.  *
  1183.  * If _PR_POLL_READ_SYS_WRITE bit is set, it means the
  1184.  * PR_POLL_READ flag specified by the topmost layer is
  1185.  * mapped to the WRITE flag at the system layer.  Similarly
  1186.  * for the other three _PR_POLL_XXX_SYS_YYY flags.  It is
  1187.  * assumed that the PR_POLL_EXCEPT flag doesn't get mapped
  1188.  * to other flags.
  1189.  */
  1190. #define _PR_POLL_READ_SYS_READ     0x1
  1191. #define _PR_POLL_READ_SYS_WRITE    0x2
  1192. #define _PR_POLL_WRITE_SYS_READ    0x4
  1193. #define _PR_POLL_WRITE_SYS_WRITE   0x8
  1194.  
  1195. /*
  1196. ** These methods are coerced into file descriptor methods table
  1197. ** when the intended service is inappropriate for the particular
  1198. ** type of file descriptor.
  1199. */
  1200. extern PRIntn _PR_InvalidInt(void);
  1201. extern PRInt16 _PR_InvalidInt16(void);
  1202. extern PRInt64 _PR_InvalidInt64(void);
  1203. extern PRStatus _PR_InvalidStatus(void);
  1204. extern PRFileDesc *_PR_InvalidDesc(void);
  1205.  
  1206. extern PRIOMethods _pr_faulty_methods;
  1207.  
  1208. extern PRStatus _PR_MapOptionName(
  1209.     PRSockOption optname, PRInt32 *level, PRInt32 *name);
  1210. extern void _PR_InitThreads(
  1211.     PRThreadType type, PRThreadPriority priority, PRUintn maxPTDs);
  1212.  
  1213. PR_EXTERN(void) _PR_MD_START_INTERRUPTS(void);
  1214. #define    _PR_MD_START_INTERRUPTS _MD_START_INTERRUPTS
  1215.  
  1216. PR_EXTERN(void) _PR_MD_INIT_LOCKS(void);
  1217. #define    _PR_MD_INIT_LOCKS _MD_INIT_LOCKS
  1218.  
  1219. struct PRLock {
  1220. #if defined(_PR_PTHREADS)
  1221.     pthread_mutex_t mutex;          /* the underlying lock */
  1222.     _PT_Notified notified;          /* array of conditions notified */
  1223.     pthread_t owner;                /* current lock owner */
  1224. #else  /* defined(_PR_PTHREADS) */
  1225.     PRCList links;                  /* linkage for PRThread.lockList */
  1226.     struct PRThread *owner;         /* current lock owner */
  1227.     PRCList waitQ;                  /* list of threads waiting for lock */
  1228.     PRThreadPriority priority;      /* priority of lock */ 
  1229.     PRThreadPriority boostPriority; /* boosted priority of lock owner */
  1230.     _MDLock ilock;                  /* Internal Lock to protect user-level fields */
  1231. #endif /* defined(_PR_PTHREADS) */
  1232. };
  1233.  
  1234. extern void _PR_InitLocks(void);
  1235.  
  1236. struct PRCondVar {
  1237.     PRLock *lock;               /* associated lock that protects the condition */
  1238. #if defined(_PR_PTHREADS)
  1239.     pthread_cond_t cv;          /* underlying pthreads condition */
  1240.     PRInt32 notify_pending;     /* CV has destroy pending notification */
  1241. #else  /* defined(_PR_PTHREADS) */
  1242.     PRCList condQ;              /* Condition variable wait Q */
  1243.     _MDLock ilock;              /* Internal Lock to protect condQ */
  1244.     _MDCVar md;
  1245. #endif /* defined(_PR_PTHREADS) */
  1246. };
  1247.  
  1248. /************************************************************************/
  1249.  
  1250. struct PRMonitor {
  1251.     const char* name;           /* monitor name for debugging */
  1252. #if defined(_PR_PTHREADS)
  1253.     PRLock lock;                /* the lock struture structure */
  1254.     pthread_t owner;            /* the owner of the lock or zero */
  1255.     PRCondVar *cvar;            /* condition variable queue */
  1256. #else  /* defined(_PR_PTHREADS) */
  1257.     PRCondVar *cvar;            /* associated lock and condition variable queue */
  1258. #endif /* defined(_PR_PTHREADS) */
  1259.     PRUint32 entryCount;        /* # of times re-entered */
  1260. };
  1261.  
  1262. /************************************************************************/
  1263.  
  1264. struct PRSemaphore {
  1265.     PRCondVar *cvar;        /* associated lock and condition variable queue */
  1266.     PRUintn count;            /* the value of the counting semaphore */
  1267.     PRUint32 waiters;            /* threads waiting on the semaphore */
  1268. #if defined(_PR_PTHREADS)
  1269. #else  /* defined(_PR_PTHREADS) */
  1270.     _MDSemaphore md;
  1271. #endif /* defined(_PR_PTHREADS) */
  1272. };
  1273.  
  1274. PR_EXTERN(void) _PR_InitSem(void);
  1275.  
  1276. /************************************************************************/
  1277.  
  1278. /* XXX this needs to be exported (sigh) */
  1279. struct PRThreadStack {
  1280.     PRCList links;
  1281.     PRUintn flags;
  1282.  
  1283.     char *allocBase;            /* base of stack's allocated memory */
  1284.     PRUint32 allocSize;         /* size of stack's allocated memory */
  1285.     char *stackBottom;          /* bottom of stack from C's point of view */
  1286.     char *stackTop;             /* top of stack from C's point of view */
  1287.     PRUint32 stackSize;         /* size of usable portion of the stack */
  1288.  
  1289.     PRSegment *seg;
  1290.         PRThread* thr;          /* back pointer to thread owning this stack */
  1291.  
  1292. #if defined(_PR_PTHREADS)
  1293. #else /* defined(_PR_PTHREADS) */
  1294.     _MDThreadStack md;
  1295. #endif /* defined(_PR_PTHREADS) */
  1296. };
  1297.  
  1298. /*
  1299.  * Thread private data destructor array
  1300.  * There is a destructor (or NULL) associated with each key and
  1301.  *    applied to all threads known to the system.
  1302.  *  Storage allocated in prtpd.c.
  1303.  */
  1304. extern PRThreadPrivateDTOR *_pr_tpd_destructors;
  1305. extern void _PR_DestroyThreadPrivate(PRThread*);
  1306.  
  1307. typedef void (PR_CALLBACK *_PRStartFn)(void *);
  1308.  
  1309. struct PRThread {
  1310.     PRUint32 state;                 /* thread's creation state */
  1311.     PRThreadPriority priority;      /* apparent priority, loosly defined */
  1312.  
  1313.     void *arg;                      /* argument to the client's entry point */
  1314.     _PRStartFn startFunc;           /* the root of the client's thread */
  1315.  
  1316.     PRThreadStack *stack;           /* info about thread's stack (for GC) */
  1317.     void *environment;              /* pointer to execution environment */
  1318.  
  1319.     PRThreadDumpProc dump;          /* dump thread info out */
  1320.     void *dumpArg;                  /* argument for the dump function */
  1321.  
  1322.     /*
  1323.     ** Per thread private data
  1324.     */
  1325.     PRUint32 tpdLength;             /* thread's current vector length */
  1326.     void **privateData;             /* private data vector or NULL */
  1327.     PRInt32 errorStringSize;        /* byte length of current error string | zero */
  1328.     PRErrorCode errorCode;          /* current NSPR error code | zero */
  1329.     PRInt32 osErrorCode;            /* mapping of errorCode | zero */
  1330.     char *errorString;              /* current error string | NULL */
  1331.  
  1332. #if defined(_PR_PTHREADS)
  1333.     pthread_t id;                   /* pthread identifier for the thread */
  1334.     PRBool okToDelete;              /* ok to delete the PRThread struct? */
  1335.     PRCondVar *io_cv;               /* a condition used to run i/o */
  1336.     PRCondVar *waiting;             /* where the thread is waiting | NULL */
  1337.     void *sp;                       /* recorded sp for garbage collection */
  1338.     PRThread *next, *prev;          /* simple linked list of all threads */
  1339.     PRUint32 suspend;               /* used to store suspend and resume flags */
  1340. #ifdef PT_NO_SIGTIMEDWAIT
  1341.     pthread_mutex_t suspendResumeMutex;
  1342.     pthread_cond_t suspendResumeCV;
  1343. #endif
  1344. #else /* defined(_PR_PTHREADS) */
  1345.     _MDLock threadLock;             /* Lock to protect thread state variables.
  1346.                                      * Protects the following fields:
  1347.                                      *     state
  1348.                                      *     priority
  1349.                                      *     links
  1350.                                      *     wait
  1351.                                      *     cpu
  1352.                                      */
  1353.     PRUint32 queueCount;
  1354.     PRUint32 waitCount;
  1355.  
  1356.     PRCList active;                 /* on list of all active threads        */
  1357.     PRCList links;
  1358.     PRCList waitQLinks;             /* when thread is PR_Wait'ing */
  1359.     PRCList lockList;               /* list of locks currently holding */
  1360.     PRIntervalTime sleep;           /* sleep time when thread is sleeping */
  1361.     struct _wait {
  1362.         struct PRLock *lock;
  1363.         struct PRCondVar *cvar;
  1364.     } wait;
  1365.  
  1366.     PRUint32 id;
  1367.     PRUint32 flags;
  1368.     PRUint32 no_sched;              /* Don't schedule the thread to run.
  1369.                                      * This flag has relevance only when
  1370.                                      * multiple NSPR CPUs are created.
  1371.                                      * When a thread is de-scheduled, there
  1372.                                      * is a narrow window of time in which
  1373.                                      * the thread is put on the run queue
  1374.                                      * but the scheduler is actually using
  1375.                                      * the stack of this thread.  It is safe
  1376.                                      * to run this thread on a different CPU
  1377.                                      * only when its stack is not in use on
  1378.                                      * any other CPU.  The no_sched flag is
  1379.                                      * set during this interval to prevent
  1380.                                      * the thread from being scheduled on a
  1381.                                      * different CPU.
  1382.                                      */
  1383.     PRUint32 numExits;
  1384.     _PRPerThreadExit *ptes;
  1385.  
  1386.     /* thread termination condition variable for join */
  1387.     PRCondVar *term;
  1388.  
  1389.     _PRCPU *cpu;                    /* cpu to which this thread is bound    */
  1390.     PRUint32 threadAllocatedOnStack;/* boolean */
  1391.  
  1392.     /* When an async IO is in progress and a second async IO cannot be 
  1393.      * initiated, the io_pending flag is set to true.  Some platforms will
  1394.      * not use the io_pending flag.  If the io_pending flag is true, then
  1395.      * io_fd is the OS-file descriptor on which IO is pending.
  1396.      */
  1397.     PRBool io_pending;
  1398.     PRInt32 io_fd;
  1399.  
  1400.     /* If a timeout occurs or if an outstanding IO is interrupted and the
  1401.      * OS doesn't support a real cancellation (NT or MAC), then the 
  1402.      * io_suspended flag will be set to true.  The thread will be resumed
  1403.      * but may run into trouble issuing additional IOs until the io_pending
  1404.      * flag can be cleared 
  1405.      */
  1406.     PRBool io_suspended;
  1407.  
  1408.     _MDThread md;
  1409. #endif /* defined(_PR_PTHREADS) */
  1410. };
  1411.  
  1412. struct PRProcessAttr {
  1413.     PRFileDesc *stdinFd;
  1414.     PRFileDesc *stdoutFd;
  1415.     PRFileDesc *stderrFd;
  1416.     char *currentDirectory;
  1417. };
  1418.  
  1419. struct PRProcess {
  1420.     _MDProcess md;
  1421. };
  1422.  
  1423. struct PRFileMap {
  1424.     PRFileDesc *fd;
  1425.     PRFileMapProtect prot;
  1426.     _MDFileMap md;
  1427. };
  1428.  
  1429. /************************************************************************/
  1430.  
  1431. struct PRFilePrivate {
  1432.     PRInt32 state;
  1433.     PRBool nonblocking;
  1434.     PRFileDesc *next;
  1435.     PRIntn lockCount;
  1436.     _MDFileDesc md;
  1437. };
  1438.  
  1439. struct PRDir {
  1440.     PRDirEntry d;
  1441.     _MDDir md;
  1442. };
  1443.  
  1444. extern void _PR_InitSegs(void);
  1445. extern void _PR_InitStacks(void);
  1446. extern void _PR_InitTPD(void);
  1447. extern void _PR_InitMem(void);
  1448. extern void _PR_InitEnv(void);
  1449. extern void _PR_InitCMon(void);
  1450. extern void _PR_InitIO(void);
  1451. extern void _PR_InitLog(void);
  1452. extern void _PR_InitNet(void);
  1453. extern void _PR_InitClock(void);
  1454. extern void _PR_InitLinker(void);
  1455. extern void _PR_InitAtomic(void);
  1456. extern void _PR_InitCPUs(void);
  1457. extern void _PR_InitDtoa(void);
  1458. extern void _PR_InitMW(void);
  1459. extern void _PR_NotifyCondVar(PRCondVar *cvar, PRThread *me);
  1460. extern void _PR_CleanupThread(PRThread *thread);
  1461. extern void _PR_CleanupTPD(void);
  1462. extern void _PR_Cleanup(void);
  1463. extern void _PR_LogCleanup(void);
  1464. extern void _PR_InitLayerCache(void);
  1465.  
  1466. extern PRBool _pr_initialized;
  1467. extern void _PR_ImplicitInitialization(void);
  1468. extern PRBool _PR_Obsolete(const char *obsolete, const char *preferred);
  1469.  
  1470. /************************************************************************/
  1471.  
  1472. struct PRSegment {
  1473.     void *vaddr;
  1474.     PRUint32 size;
  1475.     PRUintn flags;
  1476. #if defined(_PR_PTHREADS)
  1477. #else  /* defined(_PR_PTHREADS) */
  1478.     _MDSegment md;
  1479. #endif /* defined(_PR_PTHREADS) */
  1480. };
  1481.  
  1482. /* PRSegment.flags */
  1483. #define _PR_SEG_VM    0x1
  1484.  
  1485. /***********************************************************************
  1486. ** FUNCTION:    _PR_NewSegment()
  1487. ** DESCRIPTION:
  1488. **   Allocate a memory segment. The "size" value is rounded up to the
  1489. **   native system page size and a page aligned portion of memory is
  1490. **   returned.  This memory is not part of the malloc heap. If "vaddr" is
  1491. **   not NULL then PR tries to allocate the segment at the desired virtual
  1492. **   address.
  1493. ** INPUTS:    size:  size of the desired memory segment
  1494. **          vaddr:  address at which the newly aquired segment is to be
  1495. **                  mapped into memory.
  1496. ** OUTPUTS:    a memory segment is allocated, a PRSegment is allocated
  1497. ** RETURN:    pointer to PRSegment
  1498. ***********************************************************************/
  1499. extern PRSegment* _PR_NewSegment(PRUint32 size, void *vaddr);
  1500.  
  1501. /***********************************************************************
  1502. ** FUNCTION:    _PR_DestroySegment()
  1503. ** DESCRIPTION:
  1504. **   The memory segment and the PRSegment are freed
  1505. ** INPUTS:    seg:  pointer to PRSegment to be freed
  1506. ** OUTPUTS:    the the PRSegment and its associated memory segment are freed
  1507. ** RETURN:    void
  1508. ***********************************************************************/
  1509. extern void _PR_DestroySegment(PRSegment *seg);
  1510.  
  1511. /************************************************************************/
  1512.  
  1513. extern PRInt32 _pr_pageSize;
  1514. extern PRInt32 _pr_pageShift;
  1515.  
  1516. extern PRLogModuleInfo *_pr_clock_lm;
  1517. extern PRLogModuleInfo *_pr_cmon_lm;
  1518. extern PRLogModuleInfo *_pr_io_lm;
  1519. extern PRLogModuleInfo *_pr_cvar_lm;
  1520. extern PRLogModuleInfo *_pr_mon_lm;
  1521. extern PRLogModuleInfo *_pr_linker_lm;
  1522. extern PRLogModuleInfo *_pr_sched_lm;
  1523. extern PRLogModuleInfo *_pr_thread_lm;
  1524. extern PRLogModuleInfo *_pr_gc_lm;
  1525.  
  1526. extern PRFileDesc *_pr_stdin;
  1527. extern PRFileDesc *_pr_stdout;
  1528. extern PRFileDesc *_pr_stderr;
  1529.  
  1530. #if defined(_PR_INET6)
  1531. extern PRBool _pr_ipv6_enabled;  /* defined in prnetdb.c */
  1532. #endif
  1533.  
  1534. /* Overriding malloc, free, etc. */
  1535. #if !defined(_PR_NO_PREEMPT) && defined(XP_UNIX) \
  1536.         && !defined(_PR_PTHREADS) && !defined(_PR_GLOBAL_THREADS_ONLY) \
  1537.         && !defined(PURIFY) \
  1538.         && !defined(RHAPSODY) \
  1539.         && !defined(NEXTSTEP) \
  1540.         && !(defined (UNIXWARE) && defined (USE_SVR4_THREADS))
  1541. #define _PR_OVERRIDE_MALLOC
  1542. #endif
  1543.  
  1544. /*************************************************************************
  1545. * External machine-dependent code provided by each OS.                     *                                                                     *
  1546. *************************************************************************/
  1547.  
  1548. /* Initialization related */
  1549. extern void _PR_MD_EARLY_INIT(void);
  1550. #define    _PR_MD_EARLY_INIT _MD_EARLY_INIT
  1551.  
  1552. extern void _PR_MD_INTERVAL_INIT(void);
  1553. #define    _PR_MD_INTERVAL_INIT _MD_INTERVAL_INIT
  1554.  
  1555. PR_EXTERN(void) _PR_MD_INIT_SEGS(void);
  1556. #define    _PR_MD_INIT_SEGS _MD_INIT_SEGS
  1557.  
  1558. PR_EXTERN(void) _PR_MD_FINAL_INIT(void);
  1559. #define    _PR_MD_FINAL_INIT _MD_FINAL_INIT
  1560.  
  1561. /* Process control */
  1562.  
  1563. extern PRProcess * _PR_MD_CREATE_PROCESS(
  1564.     const char *path,
  1565.     char *const *argv,
  1566.     char *const *envp,
  1567.     const PRProcessAttr *attr);
  1568. #define    _PR_MD_CREATE_PROCESS _MD_CREATE_PROCESS
  1569.  
  1570. extern PRStatus _PR_MD_DETACH_PROCESS(PRProcess *process);
  1571. #define    _PR_MD_DETACH_PROCESS _MD_DETACH_PROCESS
  1572.  
  1573. extern PRStatus _PR_MD_WAIT_PROCESS(PRProcess *process, PRInt32 *exitCode);
  1574. #define    _PR_MD_WAIT_PROCESS _MD_WAIT_PROCESS
  1575.  
  1576. extern PRStatus _PR_MD_KILL_PROCESS(PRProcess *process);
  1577. #define    _PR_MD_KILL_PROCESS _MD_KILL_PROCESS        
  1578.  
  1579. /* Current Time */
  1580. PR_EXTERN(PRTime) _PR_MD_NOW(void);
  1581. #define    _PR_MD_NOW _MD_NOW
  1582.  
  1583. /* Environment related */
  1584. extern char* _PR_MD_GET_ENV(const char *name);
  1585. #define    _PR_MD_GET_ENV _MD_GET_ENV
  1586.  
  1587. extern PRIntn _PR_MD_PUT_ENV(const char *name);
  1588. #define    _PR_MD_PUT_ENV _MD_PUT_ENV
  1589.  
  1590. /* Atomic operations */
  1591.  
  1592. extern void _PR_MD_INIT_ATOMIC(void);
  1593. #define    _PR_MD_INIT_ATOMIC _MD_INIT_ATOMIC
  1594.  
  1595. extern PRInt32 _PR_MD_ATOMIC_INCREMENT(PRInt32 *);
  1596. #define    _PR_MD_ATOMIC_INCREMENT _MD_ATOMIC_INCREMENT
  1597.  
  1598. extern PRInt32 _PR_MD_ATOMIC_ADD(PRInt32 *, PRInt32);
  1599. #define    _PR_MD_ATOMIC_ADD _MD_ATOMIC_ADD
  1600.  
  1601. extern PRInt32 _PR_MD_ATOMIC_DECREMENT(PRInt32 *);
  1602. #define    _PR_MD_ATOMIC_DECREMENT _MD_ATOMIC_DECREMENT
  1603.  
  1604. extern PRInt32 _PR_MD_ATOMIC_SET(PRInt32 *, PRInt32);
  1605. #define    _PR_MD_ATOMIC_SET _MD_ATOMIC_SET
  1606.  
  1607. /* Segment related */
  1608. PR_EXTERN(PRStatus) _PR_MD_ALLOC_SEGMENT(PRSegment *seg, PRUint32 size, void *vaddr);
  1609. #define    _PR_MD_ALLOC_SEGMENT _MD_ALLOC_SEGMENT
  1610.  
  1611. PR_EXTERN(void) _PR_MD_FREE_SEGMENT(PRSegment *seg);
  1612. #define    _PR_MD_FREE_SEGMENT _MD_FREE_SEGMENT
  1613.  
  1614. /* Garbage collection */
  1615.  
  1616. /*
  1617. ** Save the registers that the GC would find interesting into the thread
  1618. ** "t". isCurrent will be non-zero if the thread state that is being
  1619. ** saved is the currently executing thread. Return the address of the
  1620. ** first register to be scanned as well as the number of registers to
  1621. ** scan in "np".
  1622. **
  1623. ** If "isCurrent" is non-zero then it is allowed for the thread context
  1624. ** area to be used as scratch storage to hold just the registers
  1625. ** necessary for scanning.
  1626. */
  1627. extern PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np);
  1628.  
  1629. /* Time intervals */
  1630.  
  1631. extern PRIntervalTime _PR_MD_GET_INTERVAL(void);
  1632. #define _PR_MD_GET_INTERVAL _MD_GET_INTERVAL
  1633.  
  1634. extern PRIntervalTime _PR_MD_INTERVAL_PER_SEC(void);
  1635. #define _PR_MD_INTERVAL_PER_SEC _MD_INTERVAL_PER_SEC
  1636.  
  1637. /* Affinity masks */
  1638.  
  1639. extern PRInt32 _PR_MD_SETTHREADAFFINITYMASK(PRThread *thread, PRUint32 mask );
  1640. #define _PR_MD_SETTHREADAFFINITYMASK _MD_SETTHREADAFFINITYMASK
  1641.  
  1642. extern PRInt32 _PR_MD_GETTHREADAFFINITYMASK(PRThread *thread, PRUint32 *mask);
  1643. #define _PR_MD_GETTHREADAFFINITYMASK _MD_GETTHREADAFFINITYMASK
  1644.  
  1645. /* File locking */
  1646.  
  1647. extern PRStatus _PR_MD_LOCKFILE(PRInt32 osfd);
  1648. #define    _PR_MD_LOCKFILE _MD_LOCKFILE
  1649.  
  1650. extern PRStatus _PR_MD_TLOCKFILE(PRInt32 osfd);
  1651. #define    _PR_MD_TLOCKFILE _MD_TLOCKFILE
  1652.  
  1653. extern PRStatus _PR_MD_UNLOCKFILE(PRInt32 osfd);
  1654. #define    _PR_MD_UNLOCKFILE _MD_UNLOCKFILE
  1655.  
  1656. /* Memory-mapped files */
  1657.  
  1658. extern PRStatus _PR_MD_CREATE_FILE_MAP(PRFileMap *fmap, PRInt64 size);
  1659. #define _PR_MD_CREATE_FILE_MAP _MD_CREATE_FILE_MAP
  1660.  
  1661. extern void * _PR_MD_MEM_MAP(
  1662.     PRFileMap *fmap,
  1663.     PRInt64 offset,
  1664.     PRUint32 len);
  1665. #define _PR_MD_MEM_MAP _MD_MEM_MAP
  1666.  
  1667. extern PRStatus _PR_MD_MEM_UNMAP(void *addr, PRUint32 size);
  1668. #define _PR_MD_MEM_UNMAP _MD_MEM_UNMAP
  1669.  
  1670. extern PRStatus _PR_MD_CLOSE_FILE_MAP(PRFileMap *fmap);
  1671. #define _PR_MD_CLOSE_FILE_MAP _MD_CLOSE_FILE_MAP
  1672.  
  1673. /* Socket call error code */
  1674.  
  1675. PR_EXTERN(PRInt32) _PR_MD_GET_SOCKET_ERROR(void);
  1676. #define    _PR_MD_GET_SOCKET_ERROR _MD_GET_SOCKET_ERROR
  1677.  
  1678. /* Get name of current host */
  1679. extern PRStatus _PR_MD_GETHOSTNAME(char *name, PRUint32 namelen);
  1680. #define    _PR_MD_GETHOSTNAME _MD_GETHOSTNAME
  1681.  
  1682. PR_END_EXTERN_C
  1683.  
  1684. #endif /* primpl_h___ */
  1685.