_nspr_pthread.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nspr_pthread_defs_h___
  6. #define nspr_pthread_defs_h___
  7. #include <pthread.h>
  8. #include "prthread.h"
  9. #if defined(PTHREADS_USER)
  10. /*
  11. ** Thread Local Storage
  12. */
  13. extern pthread_key_t current_thread_key;
  14. extern pthread_key_t current_cpu_key;
  15. extern pthread_key_t last_thread_key;
  16. extern pthread_key_t intsoff_key;
  17. #define _MD_CURRENT_THREAD() \
  18. ((struct PRThread *) pthread_getspecific(current_thread_key))
  19. #define _MD_CURRENT_CPU() \
  20. ((struct _PRCPU *) pthread_getspecific(current_cpu_key))
  21. #define _MD_LAST_THREAD() \
  22. ((struct PRThread *) pthread_getspecific(last_thread_key))
  23. #define _MD_SET_CURRENT_THREAD(newval) \
  24. pthread_setspecific(current_thread_key, (void *)newval)
  25. #define _MD_SET_CURRENT_CPU(newval) \
  26. pthread_setspecific(current_cpu_key, (void *)newval)
  27. #define _MD_SET_LAST_THREAD(newval) \
  28. pthread_setspecific(last_thread_key, (void *)newval)
  29. #define _MD_SET_INTSOFF(_val)
  30. #define _MD_GET_INTSOFF() 1
  31. /*
  32. ** Initialize the thread context preparing it to execute _main.
  33. */
  34. #define _MD_INIT_CONTEXT(_thread, _sp, _main, status) \
  35. PR_BEGIN_MACRO \
  36. *status = PR_TRUE; \
  37. if (SAVE_CONTEXT(_thread)) { \
  38. (*_main)(); \
  39. } \
  40. _MD_SET_THR_SP(_thread, _sp); \
  41. _thread->no_sched = 0; \
  42. PR_END_MACRO
  43. #define _MD_SWITCH_CONTEXT(_thread) \
  44. PR_BEGIN_MACRO \
  45. PR_ASSERT(_thread->no_sched); \
  46. if (!SAVE_CONTEXT(_thread)) { \
  47. (_thread)->md.errcode = errno; \
  48. _MD_SET_LAST_THREAD(_thread); \
  49. _PR_Schedule(); \
  50. } else { \
  51. (_MD_LAST_THREAD())->no_sched = 0; \
  52. } \
  53. PR_END_MACRO
  54. /*
  55. ** Restore a thread context, saved by _MD_SWITCH_CONTEXT
  56. */
  57. #define _MD_RESTORE_CONTEXT(_thread) \
  58. PR_BEGIN_MACRO \
  59. errno = (_thread)->md.errcode; \
  60. _MD_SET_CURRENT_THREAD(_thread); \
  61. _thread->no_sched = 1; \
  62. GOTO_CONTEXT(_thread); \
  63. PR_END_MACRO
  64. /* Machine-dependent (MD) data structures */
  65. struct _MDThread {
  66. jmp_buf jb;
  67. int id;
  68. int errcode;
  69. pthread_t pthread;
  70. pthread_mutex_t pthread_mutex;
  71. pthread_cond_t pthread_cond;
  72. int wait;
  73. };
  74. struct _MDThreadStack {
  75. PRInt8 notused;
  76. };
  77. struct _MDLock {
  78. pthread_mutex_t mutex;
  79. };
  80. struct _MDSemaphore {
  81. PRInt8 notused;
  82. };
  83. struct _MDCVar {
  84. pthread_mutex_t mutex;
  85. };
  86. struct _MDSegment {
  87. PRInt8 notused;
  88. };
  89. /*
  90. * md-specific cpu structure field
  91. */
  92. #define _PR_MD_MAX_OSFD FD_SETSIZE
  93. struct _MDCPU_Unix {
  94. PRCList ioQ;
  95. PRUint32 ioq_timeout;
  96. PRInt32 ioq_max_osfd;
  97. PRInt32 ioq_osfd_cnt;
  98. #ifndef _PR_USE_POLL
  99. fd_set fd_read_set, fd_write_set, fd_exception_set;
  100. PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
  101. fd_exception_cnt[_PR_MD_MAX_OSFD];
  102. #else
  103. struct pollfd *ioq_pollfds;
  104. int ioq_pollfds_size;
  105. #endif /* _PR_USE_POLL */
  106. };
  107. #define _PR_IOQ(_cpu) ((_cpu)->md.md_unix.ioQ)
  108. #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
  109. #define _PR_FD_READ_SET(_cpu) ((_cpu)->md.md_unix.fd_read_set)
  110. #define _PR_FD_READ_CNT(_cpu) ((_cpu)->md.md_unix.fd_read_cnt)
  111. #define _PR_FD_WRITE_SET(_cpu) ((_cpu)->md.md_unix.fd_write_set)
  112. #define _PR_FD_WRITE_CNT(_cpu) ((_cpu)->md.md_unix.fd_write_cnt)
  113. #define _PR_FD_EXCEPTION_SET(_cpu) ((_cpu)->md.md_unix.fd_exception_set)
  114. #define _PR_FD_EXCEPTION_CNT(_cpu) ((_cpu)->md.md_unix.fd_exception_cnt)
  115. #define _PR_IOQ_TIMEOUT(_cpu) ((_cpu)->md.md_unix.ioq_timeout)
  116. #define _PR_IOQ_MAX_OSFD(_cpu) ((_cpu)->md.md_unix.ioq_max_osfd)
  117. #define _PR_IOQ_OSFD_CNT(_cpu) ((_cpu)->md.md_unix.ioq_osfd_cnt)
  118. #define _PR_IOQ_POLLFDS(_cpu) ((_cpu)->md.md_unix.ioq_pollfds)
  119. #define _PR_IOQ_POLLFDS_SIZE(_cpu) ((_cpu)->md.md_unix.ioq_pollfds_size)
  120. #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu) 32
  121. struct _MDCPU {
  122. jmp_buf jb;
  123. pthread_t pthread;
  124. struct _MDCPU_Unix md_unix;
  125. };
  126. /*
  127. #define _MD_NEW_LOCK(lock) PR_SUCCESS
  128. #define _MD_FREE_LOCK(lock)
  129. #define _MD_LOCK(lock)
  130. #define _MD_UNLOCK(lock)
  131. */
  132. extern pthread_mutex_t _pr_heapLock;
  133. #define _PR_LOCK(lock) pthread_mutex_lock(lock)
  134. #define _PR_UNLOCK(lock) pthread_mutex_unlock(lock)
  135. #define _PR_LOCK_HEAP() { \
  136. if (_pr_primordialCPU) { \
  137. _PR_LOCK(_pr_heapLock); \
  138. }
  139. #define _PR_UNLOCK_HEAP() if (_pr_primordialCPU) { \
  140. _PR_UNLOCK(_pr_heapLock); \
  141. } \
  142. }
  143. NSPR_API(PRStatus) _MD_NEW_LOCK(struct _MDLock *md);
  144. NSPR_API(void) _MD_FREE_LOCK(struct _MDLock *lockp);
  145. #define _MD_LOCK(_lockp) _PR_LOCK(&(_lockp)->mutex)
  146. #define _MD_UNLOCK(_lockp) _PR_UNLOCK(&(_lockp)->mutex)
  147. #define _MD_INIT_IO()
  148. #define _MD_IOQ_LOCK()
  149. #define _MD_IOQ_UNLOCK()
  150. #define _MD_CHECK_FOR_EXIT()
  151. NSPR_API(PRStatus) _MD_InitThread(struct PRThread *thread);
  152. #define _MD_INIT_THREAD _MD_InitThread
  153. #define _MD_INIT_ATTACHED_THREAD _MD_InitThread
  154. NSPR_API(void) _MD_ExitThread(struct PRThread *thread);
  155. #define _MD_EXIT_THREAD _MD_ExitThread
  156. NSPR_API(void) _MD_SuspendThread(struct PRThread *thread);
  157. #define _MD_SUSPEND_THREAD _MD_SuspendThread
  158. NSPR_API(void) _MD_ResumeThread(struct PRThread *thread);
  159. #define _MD_RESUME_THREAD _MD_ResumeThread
  160. NSPR_API(void) _MD_SuspendCPU(struct _PRCPU *thread);
  161. #define _MD_SUSPEND_CPU _MD_SuspendCPU
  162. NSPR_API(void) _MD_ResumeCPU(struct _PRCPU *thread);
  163. #define _MD_RESUME_CPU _MD_ResumeCPU
  164. #define _MD_BEGIN_SUSPEND_ALL()
  165. #define _MD_END_SUSPEND_ALL()
  166. #define _MD_BEGIN_RESUME_ALL()
  167. #define _MD_END_RESUME_ALL()
  168. NSPR_API(void) _MD_EarlyInit(void);
  169. #define _MD_EARLY_INIT _MD_EarlyInit
  170. #define _MD_FINAL_INIT _PR_UnixInit
  171. NSPR_API(void) _MD_InitLocks(void);
  172. #define _MD_INIT_LOCKS _MD_InitLocks
  173. NSPR_API(void) _MD_CleanThread(struct PRThread *thread);
  174. #define _MD_CLEAN_THREAD _MD_CleanThread
  175. NSPR_API(PRStatus) _MD_CreateThread(
  176. struct PRThread *thread,
  177. void (*start) (void *),
  178. PRThreadPriority priority,
  179. PRThreadScope scope,
  180. PRThreadState state,
  181. PRUint32 stackSize);
  182. #define _MD_CREATE_THREAD _MD_CreateThread
  183. extern void _MD_CleanupBeforeExit(void);
  184. #define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit
  185. NSPR_API(void) _MD_InitRunningCPU(struct _PRCPU *cpu);
  186. #define _MD_INIT_RUNNING_CPU _MD_InitRunningCPU
  187. /* The _PR_MD_WAIT_LOCK and _PR_MD_WAKEUP_WAITER functions put to sleep and
  188. * awaken a thread which is waiting on a lock or cvar.
  189. */
  190. NSPR_API(PRStatus) _MD_wait(struct PRThread *, PRIntervalTime timeout);
  191. #define _MD_WAIT _MD_wait
  192. NSPR_API(PRStatus) _MD_WakeupWaiter(struct PRThread *);
  193. #define _MD_WAKEUP_WAITER _MD_WakeupWaiter
  194. NSPR_API(void) _MD_SetPriority(struct _MDThread *thread,
  195. PRThreadPriority newPri);
  196. #define _MD_SET_PRIORITY _MD_SetPriority
  197. #endif /* PTHREADS_USER */
  198. #endif /* nspr_pthread_defs_h___ */