home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-e.zip / nspr30-e / include / md / _nspr_pthread.h < prev    next >
C/C++ Source or Header  |  1998-11-02  |  8KB  |  267 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. /*
  123.  * md-specific cpu structure field
  124.  */
  125. #define _PR_MD_MAX_OSFD FD_SETSIZE
  126.  
  127. struct _MDCPU_Unix {
  128.     PRCList ioQ;
  129.     PRUint32 ioq_timeout;
  130.     PRInt32 ioq_max_osfd;
  131.     PRInt32 ioq_osfd_cnt;
  132. #ifndef _PR_USE_POLL
  133.     fd_set fd_read_set, fd_write_set, fd_exception_set;
  134.     PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
  135.                 fd_exception_cnt[_PR_MD_MAX_OSFD];
  136. #else
  137.     struct pollfd *ioq_pollfds;
  138.     int ioq_pollfds_size;
  139. #endif    /* _PR_USE_POLL */
  140. };
  141.  
  142. #define _PR_IOQ(_cpu)            ((_cpu)->md.md_unix.ioQ)
  143. #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
  144. #define _PR_FD_READ_SET(_cpu)        ((_cpu)->md.md_unix.fd_read_set)
  145. #define _PR_FD_READ_CNT(_cpu)        ((_cpu)->md.md_unix.fd_read_cnt)
  146. #define _PR_FD_WRITE_SET(_cpu)        ((_cpu)->md.md_unix.fd_write_set)
  147. #define _PR_FD_WRITE_CNT(_cpu)        ((_cpu)->md.md_unix.fd_write_cnt)
  148. #define _PR_FD_EXCEPTION_SET(_cpu)    ((_cpu)->md.md_unix.fd_exception_set)
  149. #define _PR_FD_EXCEPTION_CNT(_cpu)    ((_cpu)->md.md_unix.fd_exception_cnt)
  150. #define _PR_IOQ_TIMEOUT(_cpu)        ((_cpu)->md.md_unix.ioq_timeout)
  151. #define _PR_IOQ_MAX_OSFD(_cpu)        ((_cpu)->md.md_unix.ioq_max_osfd)
  152. #define _PR_IOQ_OSFD_CNT(_cpu)        ((_cpu)->md.md_unix.ioq_osfd_cnt)
  153. #define _PR_IOQ_POLLFDS(_cpu)        ((_cpu)->md.md_unix.ioq_pollfds)
  154. #define _PR_IOQ_POLLFDS_SIZE(_cpu)    ((_cpu)->md.md_unix.ioq_pollfds_size)
  155.  
  156. #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu)    32
  157.  
  158. struct _MDCPU {
  159.     jmp_buf             jb;
  160.     pthread_t             pthread;
  161.     struct _MDCPU_Unix     md_unix;
  162. };
  163.  
  164. /*
  165. #define _MD_NEW_LOCK(lock) PR_SUCCESS
  166. #define _MD_FREE_LOCK(lock)
  167. #define _MD_LOCK(lock)
  168. #define _MD_UNLOCK(lock)
  169. */
  170.  
  171. extern pthread_mutex_t _pr_heapLock;
  172.  
  173. #define _PR_LOCK(lock) pthread_mutex_lock(lock)
  174.  
  175. #define _PR_UNLOCK(lock) pthread_mutex_unlock(lock)
  176.  
  177.  
  178. #define _PR_LOCK_HEAP()    {                                    \
  179.                 if (_pr_primordialCPU) {                    \
  180.                     _PR_LOCK(_pr_heapLock);                    \
  181.                 }
  182.  
  183. #define _PR_UNLOCK_HEAP()     if (_pr_primordialCPU)    {        \
  184.                     _PR_UNLOCK(_pr_heapLock);                \
  185.                 }                                            \
  186.               }
  187.  
  188. PR_EXTERN(PRStatus) _MD_NEW_LOCK(struct _MDLock *md);
  189. PR_EXTERN(void) _MD_FREE_LOCK(struct _MDLock *lockp);
  190.  
  191. #define _MD_LOCK(_lockp) _PR_LOCK(&(_lockp)->mutex)
  192. #define _MD_UNLOCK(_lockp) _PR_UNLOCK(&(_lockp)->mutex)
  193.  
  194. #define _MD_INIT_IO()
  195. #define _MD_IOQ_LOCK()
  196. #define _MD_IOQ_UNLOCK()
  197. #define _MD_CHECK_FOR_EXIT()
  198.  
  199. PR_EXTERN(PRStatus) _MD_InitThread(struct PRThread *thread);
  200. #define _MD_INIT_THREAD _MD_InitThread
  201. #define _MD_INIT_ATTACHED_THREAD _MD_InitThread
  202.  
  203. PR_EXTERN(void) _MD_ExitThread(struct PRThread *thread);
  204. #define _MD_EXIT_THREAD _MD_ExitThread
  205.  
  206. PR_EXTERN(void) _MD_SuspendThread(struct PRThread *thread);
  207. #define _MD_SUSPEND_THREAD _MD_SuspendThread
  208.  
  209. PR_EXTERN(void) _MD_ResumeThread(struct PRThread *thread);
  210. #define _MD_RESUME_THREAD _MD_ResumeThread
  211.  
  212. PR_EXTERN(void) _MD_SuspendCPU(struct _PRCPU *thread);
  213. #define _MD_SUSPEND_CPU _MD_SuspendCPU
  214.  
  215. PR_EXTERN(void) _MD_ResumeCPU(struct _PRCPU *thread);
  216. #define _MD_RESUME_CPU _MD_ResumeCPU
  217.  
  218. #define _MD_BEGIN_SUSPEND_ALL()
  219. #define _MD_END_SUSPEND_ALL()
  220. #define _MD_BEGIN_RESUME_ALL()
  221. #define _MD_END_RESUME_ALL()
  222.  
  223. PR_EXTERN(void) _MD_EarlyInit(void);
  224. #define _MD_EARLY_INIT _MD_EarlyInit
  225.  
  226. #define _MD_FINAL_INIT _PR_UnixInit
  227.  
  228. PR_EXTERN(void) _MD_InitLocks(void);
  229. #define _MD_INIT_LOCKS _MD_InitLocks
  230.  
  231. PR_EXTERN(void) _MD_CleanThread(struct PRThread *thread);
  232. #define _MD_CLEAN_THREAD _MD_CleanThread
  233.  
  234. #define _MD_INIT_PRIMORDIAL_THREAD(threadp)
  235.  
  236. PR_EXTERN(PRStatus) _MD_CreateThread(
  237.                         struct PRThread *thread,
  238.                         void (*start) (void *),
  239.                         PRThreadPriority priority,
  240.                         PRThreadScope scope,
  241.                         PRThreadState state,
  242.                         PRUint32 stackSize);
  243. #define _MD_CREATE_THREAD _MD_CreateThread
  244.  
  245. extern void _MD_CleanupBeforeExit(void);
  246. #define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit
  247.  
  248. PR_EXTERN(void) _MD_InitRunningCPU(struct _PRCPU *cpu);
  249. #define    _MD_INIT_RUNNING_CPU _MD_InitRunningCPU
  250.  
  251. /* The _PR_MD_WAIT_LOCK and _PR_MD_WAKEUP_WAITER functions put to sleep and
  252.  * awaken a thread which is waiting on a lock or cvar.
  253.  */
  254. PR_EXTERN(PRStatus) _MD_wait(struct PRThread *, PRIntervalTime timeout);
  255. #define _MD_WAIT _MD_wait
  256.  
  257. PR_EXTERN(PRStatus) _MD_WakeupWaiter(struct PRThread *);
  258. #define _MD_WAKEUP_WAITER _MD_WakeupWaiter
  259.  
  260. PR_EXTERN(void) _MD_SetPriority(struct _MDThread *thread,
  261.     PRThreadPriority newPri);
  262. #define _MD_SET_PRIORITY _MD_SetPriority
  263.  
  264. #endif /* PTHREADS_USER */
  265.  
  266. #endif /* nspr_pthread_defs_h___ */
  267.