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