home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / include / md / _nspr_pthread.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.5 KB  |  231 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 nspr_pthread_defs_h___
  20. #define nspr_pthread_defs_h___
  21.  
  22. #include <pthread.h>
  23. #include "prthread.h"
  24.  
  25. #if defined(PTHREADS_USER)
  26. /*
  27. ** Thread Local Storage 
  28. */
  29. extern pthread_key_t current_thread_key;
  30. extern pthread_key_t current_cpu_key;
  31. extern pthread_key_t last_thread_key;
  32. extern pthread_key_t intsoff_key;
  33.  
  34. #define _MD_CURRENT_THREAD()             \
  35.             ((struct PRThread *) pthread_getspecific(current_thread_key))
  36. #define _MD_CURRENT_CPU()                 \
  37.             ((struct _PRCPU *) pthread_getspecific(current_cpu_key))
  38. #define _MD_LAST_THREAD()                \
  39.             ((struct PRThread *) pthread_getspecific(last_thread_key))
  40.     
  41. #define _MD_SET_CURRENT_THREAD(newval)             \
  42.     pthread_setspecific(current_thread_key, (void *)newval)
  43.  
  44. #define _MD_SET_CURRENT_CPU(newval)             \
  45.     pthread_setspecific(current_cpu_key, (void *)newval)
  46.  
  47. #define _MD_SET_LAST_THREAD(newval)                 \
  48.     pthread_setspecific(last_thread_key, (void *)newval)
  49.  
  50. #define _MD_SET_INTSOFF(_val)
  51. #define _MD_GET_INTSOFF()    1
  52.     
  53. /*
  54. ** Initialize the thread context preparing it to execute _main.
  55. */
  56. #define _MD_INIT_CONTEXT(_thread, _sp, _main, status)            \
  57.     PR_BEGIN_MACRO                                                  \
  58.         *status = PR_TRUE;                                      \
  59.         if (SAVE_CONTEXT(_thread)) {                            \
  60.             (*_main)();                                            \
  61.         }                                                        \
  62.         _MD_SET_THR_SP(_thread, _sp);                            \
  63.         _thread->no_sched = 0;                                     \
  64.     PR_END_MACRO
  65.  
  66. #define _MD_SWITCH_CONTEXT(_thread)                                  \
  67.     PR_BEGIN_MACRO                                                     \
  68.     PR_ASSERT(_thread->no_sched);                                    \
  69.     if (!SAVE_CONTEXT(_thread)) {                                    \
  70.         (_thread)->md.errcode = errno;                              \
  71.         _MD_SET_LAST_THREAD(_thread);                                \
  72.         _PR_Schedule();                                                 \
  73.     } else {                                                        \
  74.          (_MD_LAST_THREAD())->no_sched = 0;                            \
  75.     }                                                                \
  76.     PR_END_MACRO
  77.  
  78. /*
  79. ** Restore a thread context, saved by _MD_SWITCH_CONTEXT
  80. */
  81. #define _MD_RESTORE_CONTEXT(_thread)                                \
  82.     PR_BEGIN_MACRO                                                     \
  83.     errno = (_thread)->md.errcode;                                     \
  84.     _MD_SET_CURRENT_THREAD(_thread);                                 \
  85.     _thread->no_sched = 1;                                            \
  86.     GOTO_CONTEXT(_thread);                                             \
  87.     PR_END_MACRO
  88.  
  89.  
  90. /* Machine-dependent (MD) data structures */
  91.  
  92. struct _MDThread {
  93.     jmp_buf         jb;
  94.     int                id;
  95.     int                errcode;
  96.     pthread_t        pthread;
  97.     pthread_mutex_t    pthread_mutex;
  98.     pthread_cond_t    pthread_cond;
  99.     int                wait;
  100. };
  101.  
  102. struct _MDThreadStack {
  103.     PRInt8 notused;
  104. };
  105.  
  106. struct _MDLock {
  107.     pthread_mutex_t mutex;
  108. };
  109.  
  110. struct _MDSemaphore {
  111.     PRInt8 notused;
  112. };
  113.  
  114. struct _MDCVar {
  115.     pthread_mutex_t mutex;
  116. };
  117.  
  118. struct _MDSegment {
  119.     PRInt8 notused;
  120. };
  121.  
  122. struct _MDCPU {
  123.     jmp_buf             jb;
  124.     pthread_t             pthread;
  125.     struct _MDCPU_Unix     md_unix;
  126. };
  127.  
  128. /*
  129. #define _MD_NEW_LOCK(lock) PR_SUCCESS
  130. #define _MD_FREE_LOCK(lock)
  131. #define _MD_LOCK(lock)
  132. #define _MD_UNLOCK(lock)
  133. */
  134.  
  135. extern pthread_mutex_t _pr_heapLock;
  136.  
  137. #define _PR_LOCK(lock) pthread_mutex_lock(lock)
  138.  
  139. #define _PR_UNLOCK(lock) pthread_mutex_unlock(lock)
  140.  
  141.  
  142. #define _PR_LOCK_HEAP()    {                                    \
  143.                 if (_pr_primordialCPU) {                    \
  144.                     _PR_LOCK(_pr_heapLock);                    \
  145.                 }
  146.  
  147. #define _PR_UNLOCK_HEAP()     if (_pr_primordialCPU)    {        \
  148.                     _PR_UNLOCK(_pr_heapLock);                \
  149.                 }                                            \
  150.               }
  151.  
  152. PR_EXTERN(PRStatus) _MD_NEW_LOCK(struct _MDLock *md);
  153. PR_EXTERN(void) _MD_FREE_LOCK(struct _MDLock *lockp);
  154.  
  155. #define _MD_LOCK(_lockp) _PR_LOCK(&(_lockp)->mutex)
  156. #define _MD_UNLOCK(_lockp) _PR_UNLOCK(&(_lockp)->mutex)
  157.  
  158. #define _MD_INIT_IO()
  159. #define _MD_IOQ_LOCK()
  160. #define _MD_IOQ_UNLOCK()
  161. #define _MD_CHECK_FOR_EXIT()
  162.  
  163. PR_EXTERN(PRStatus) _MD_InitThread(struct PRThread *thread);
  164. #define _MD_INIT_THREAD _MD_InitThread
  165. #define _MD_INIT_ATTACHED_THREAD _MD_InitThread
  166.  
  167. PR_EXTERN(void) _MD_ExitThread(struct PRThread *thread);
  168. #define _MD_EXIT_THREAD _MD_ExitThread
  169.  
  170. PR_EXTERN(void) _MD_SuspendThread(struct PRThread *thread);
  171. #define _MD_SUSPEND_THREAD _MD_SuspendThread
  172.  
  173. PR_EXTERN(void) _MD_ResumeThread(struct PRThread *thread);
  174. #define _MD_RESUME_THREAD _MD_ResumeThread
  175.  
  176. PR_EXTERN(void) _MD_SuspendCPU(struct _PRCPU *thread);
  177. #define _MD_SUSPEND_CPU _MD_SuspendCPU
  178.  
  179. PR_EXTERN(void) _MD_ResumeCPU(struct _PRCPU *thread);
  180. #define _MD_RESUME_CPU _MD_ResumeCPU
  181.  
  182. #define _MD_BEGIN_SUSPEND_ALL()
  183. #define _MD_END_SUSPEND_ALL()
  184. #define _MD_BEGIN_RESUME_ALL()
  185. #define _MD_END_RESUME_ALL()
  186.  
  187. PR_EXTERN(void) _MD_EarlyInit(void);
  188. #define _MD_EARLY_INIT _MD_EarlyInit
  189.  
  190. #define _MD_FINAL_INIT _PR_UnixInit
  191.  
  192. PR_EXTERN(void) _MD_InitLocks(void);
  193. #define _MD_INIT_LOCKS _MD_InitLocks
  194.  
  195. PR_EXTERN(void) _MD_CleanThread(struct PRThread *thread);
  196. #define _MD_CLEAN_THREAD _MD_CleanThread
  197.  
  198. #define _MD_INIT_PRIMORDIAL_THREAD(threadp)
  199.  
  200. PR_EXTERN(PRStatus) _MD_CreateThread(
  201.                         struct PRThread *thread,
  202.                         void (*start) (void *),
  203.                         PRThreadPriority priority,
  204.                         PRThreadScope scope,
  205.                         PRThreadState state,
  206.                         PRUint32 stackSize);
  207. #define _MD_CREATE_THREAD _MD_CreateThread
  208.  
  209. extern void _MD_CleanupBeforeExit(void);
  210. #define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit
  211.  
  212. PR_EXTERN(void) _MD_InitRunningCPU(struct _PRCPU *cpu);
  213. #define    _MD_INIT_RUNNING_CPU _MD_InitRunningCPU
  214.  
  215. /* The _PR_MD_WAIT_LOCK and _PR_MD_WAKEUP_WAITER functions put to sleep and
  216.  * awaken a thread which is waiting on a lock or cvar.
  217.  */
  218. PR_EXTERN(PRStatus) _MD_wait(struct PRThread *, PRIntervalTime timeout);
  219. #define _MD_WAIT _MD_wait
  220.  
  221. PR_EXTERN(PRStatus) _MD_WakeupWaiter(struct PRThread *);
  222. #define _MD_WAKEUP_WAITER _MD_WakeupWaiter
  223.  
  224. PR_EXTERN(void) _MD_SetPriority(struct _MDThread *thread,
  225.     PRThreadPriority newPri);
  226. #define _MD_SET_PRIORITY _MD_SetPriority
  227.  
  228. #endif /* PTHREADS_USER */
  229.  
  230. #endif /* nspr_pthread_defs_h___ */
  231.