nsprpub/pr/include/md/_nspr_pthread.h

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     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/. */
     6 #ifndef nspr_pthread_defs_h___
     7 #define nspr_pthread_defs_h___
     9 #include <pthread.h>
    10 #include "prthread.h"
    12 #if defined(PTHREADS_USER)
    13 /*
    14 ** Thread Local Storage 
    15 */
    16 extern pthread_key_t current_thread_key;
    17 extern pthread_key_t current_cpu_key;
    18 extern pthread_key_t last_thread_key;
    19 extern pthread_key_t intsoff_key;
    21 #define _MD_CURRENT_THREAD() 			\
    22 			((struct PRThread *) pthread_getspecific(current_thread_key))
    23 #define _MD_CURRENT_CPU() 				\
    24 			((struct _PRCPU *) pthread_getspecific(current_cpu_key))
    25 #define _MD_LAST_THREAD()				\
    26 			((struct PRThread *) pthread_getspecific(last_thread_key))
    28 #define _MD_SET_CURRENT_THREAD(newval) 			\
    29 	pthread_setspecific(current_thread_key, (void *)newval)
    31 #define _MD_SET_CURRENT_CPU(newval) 			\
    32 	pthread_setspecific(current_cpu_key, (void *)newval)
    34 #define _MD_SET_LAST_THREAD(newval)	 			\
    35 	pthread_setspecific(last_thread_key, (void *)newval)
    37 #define _MD_SET_INTSOFF(_val)
    38 #define _MD_GET_INTSOFF()	1
    40 /*
    41 ** Initialize the thread context preparing it to execute _main.
    42 */
    43 #define _MD_INIT_CONTEXT(_thread, _sp, _main, status)			\
    44     PR_BEGIN_MACRO				      							\
    45         *status = PR_TRUE;              						\
    46 		if (SAVE_CONTEXT(_thread)) {							\
    47 	    	(*_main)();											\
    48 		}														\
    49 		_MD_SET_THR_SP(_thread, _sp);							\
    50 		_thread->no_sched = 0; 									\
    51     PR_END_MACRO
    53 #define _MD_SWITCH_CONTEXT(_thread)  								\
    54     PR_BEGIN_MACRO 													\
    55 	PR_ASSERT(_thread->no_sched);									\
    56 	if (!SAVE_CONTEXT(_thread)) {									\
    57 		(_thread)->md.errcode = errno;  							\
    58 		_MD_SET_LAST_THREAD(_thread);								\
    59 		_PR_Schedule();		     									\
    60     } else {														\
    61 		 (_MD_LAST_THREAD())->no_sched = 0;							\
    62 	}																\
    63     PR_END_MACRO
    65 /*
    66 ** Restore a thread context, saved by _MD_SWITCH_CONTEXT
    67 */
    68 #define _MD_RESTORE_CONTEXT(_thread)								\
    69     PR_BEGIN_MACRO 													\
    70     errno = (_thread)->md.errcode; 									\
    71     _MD_SET_CURRENT_THREAD(_thread); 								\
    72 	_thread->no_sched = 1;											\
    73     GOTO_CONTEXT(_thread); 											\
    74     PR_END_MACRO
    77 /* Machine-dependent (MD) data structures */
    79 struct _MDThread {
    80     jmp_buf 		jb;
    81     int				id;
    82     int				errcode;
    83 	pthread_t		pthread;
    84 	pthread_mutex_t	pthread_mutex;
    85 	pthread_cond_t	pthread_cond;
    86 	int				wait;
    87 };
    89 struct _MDThreadStack {
    90     PRInt8 notused;
    91 };
    93 struct _MDLock {
    94 	pthread_mutex_t mutex;
    95 };
    97 struct _MDSemaphore {
    98     PRInt8 notused;
    99 };
   101 struct _MDCVar {
   102 	pthread_mutex_t mutex;
   103 };
   105 struct _MDSegment {
   106     PRInt8 notused;
   107 };
   109 /*
   110  * md-specific cpu structure field
   111  */
   112 #define _PR_MD_MAX_OSFD FD_SETSIZE
   114 struct _MDCPU_Unix {
   115     PRCList ioQ;
   116     PRUint32 ioq_timeout;
   117     PRInt32 ioq_max_osfd;
   118     PRInt32 ioq_osfd_cnt;
   119 #ifndef _PR_USE_POLL
   120     fd_set fd_read_set, fd_write_set, fd_exception_set;
   121     PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
   122 				fd_exception_cnt[_PR_MD_MAX_OSFD];
   123 #else
   124 	struct pollfd *ioq_pollfds;
   125 	int ioq_pollfds_size;
   126 #endif	/* _PR_USE_POLL */
   127 };
   129 #define _PR_IOQ(_cpu)			((_cpu)->md.md_unix.ioQ)
   130 #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
   131 #define _PR_FD_READ_SET(_cpu)		((_cpu)->md.md_unix.fd_read_set)
   132 #define _PR_FD_READ_CNT(_cpu)		((_cpu)->md.md_unix.fd_read_cnt)
   133 #define _PR_FD_WRITE_SET(_cpu)		((_cpu)->md.md_unix.fd_write_set)
   134 #define _PR_FD_WRITE_CNT(_cpu)		((_cpu)->md.md_unix.fd_write_cnt)
   135 #define _PR_FD_EXCEPTION_SET(_cpu)	((_cpu)->md.md_unix.fd_exception_set)
   136 #define _PR_FD_EXCEPTION_CNT(_cpu)	((_cpu)->md.md_unix.fd_exception_cnt)
   137 #define _PR_IOQ_TIMEOUT(_cpu)		((_cpu)->md.md_unix.ioq_timeout)
   138 #define _PR_IOQ_MAX_OSFD(_cpu)		((_cpu)->md.md_unix.ioq_max_osfd)
   139 #define _PR_IOQ_OSFD_CNT(_cpu)		((_cpu)->md.md_unix.ioq_osfd_cnt)
   140 #define _PR_IOQ_POLLFDS(_cpu)		((_cpu)->md.md_unix.ioq_pollfds)
   141 #define _PR_IOQ_POLLFDS_SIZE(_cpu)	((_cpu)->md.md_unix.ioq_pollfds_size)
   143 #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu)	32
   145 struct _MDCPU {
   146     jmp_buf 			jb;
   147 	pthread_t 			pthread;
   148 	struct _MDCPU_Unix 	md_unix;
   149 };
   151 /*
   152 #define _MD_NEW_LOCK(lock) PR_SUCCESS
   153 #define _MD_FREE_LOCK(lock)
   154 #define _MD_LOCK(lock)
   155 #define _MD_UNLOCK(lock)
   156 */
   158 extern pthread_mutex_t _pr_heapLock;
   160 #define _PR_LOCK(lock) pthread_mutex_lock(lock)
   162 #define _PR_UNLOCK(lock) pthread_mutex_unlock(lock)
   165 #define _PR_LOCK_HEAP()	{									\
   166 				if (_pr_primordialCPU) {					\
   167 					_PR_LOCK(_pr_heapLock);					\
   168 				}
   170 #define _PR_UNLOCK_HEAP() 	if (_pr_primordialCPU)	{		\
   171 					_PR_UNLOCK(_pr_heapLock);				\
   172 				}											\
   173 			  }
   175 NSPR_API(PRStatus) _MD_NEW_LOCK(struct _MDLock *md);
   176 NSPR_API(void) _MD_FREE_LOCK(struct _MDLock *lockp);
   178 #define _MD_LOCK(_lockp) _PR_LOCK(&(_lockp)->mutex)
   179 #define _MD_UNLOCK(_lockp) _PR_UNLOCK(&(_lockp)->mutex)
   181 #define _MD_INIT_IO()
   182 #define _MD_IOQ_LOCK()
   183 #define _MD_IOQ_UNLOCK()
   184 #define _MD_CHECK_FOR_EXIT()
   186 NSPR_API(PRStatus) _MD_InitThread(struct PRThread *thread);
   187 #define _MD_INIT_THREAD _MD_InitThread
   188 #define _MD_INIT_ATTACHED_THREAD _MD_InitThread
   190 NSPR_API(void) _MD_ExitThread(struct PRThread *thread);
   191 #define _MD_EXIT_THREAD _MD_ExitThread
   193 NSPR_API(void) _MD_SuspendThread(struct PRThread *thread);
   194 #define _MD_SUSPEND_THREAD _MD_SuspendThread
   196 NSPR_API(void) _MD_ResumeThread(struct PRThread *thread);
   197 #define _MD_RESUME_THREAD _MD_ResumeThread
   199 NSPR_API(void) _MD_SuspendCPU(struct _PRCPU *thread);
   200 #define _MD_SUSPEND_CPU _MD_SuspendCPU
   202 NSPR_API(void) _MD_ResumeCPU(struct _PRCPU *thread);
   203 #define _MD_RESUME_CPU _MD_ResumeCPU
   205 #define _MD_BEGIN_SUSPEND_ALL()
   206 #define _MD_END_SUSPEND_ALL()
   207 #define _MD_BEGIN_RESUME_ALL()
   208 #define _MD_END_RESUME_ALL()
   210 NSPR_API(void) _MD_EarlyInit(void);
   211 #define _MD_EARLY_INIT _MD_EarlyInit
   213 #define _MD_FINAL_INIT _PR_UnixInit
   215 NSPR_API(void) _MD_InitLocks(void);
   216 #define _MD_INIT_LOCKS _MD_InitLocks
   218 NSPR_API(void) _MD_CleanThread(struct PRThread *thread);
   219 #define _MD_CLEAN_THREAD _MD_CleanThread
   221 NSPR_API(PRStatus) _MD_CreateThread(
   222                         struct PRThread *thread,
   223                         void (*start) (void *),
   224                         PRThreadPriority priority,
   225                         PRThreadScope scope,
   226                         PRThreadState state,
   227                         PRUint32 stackSize);
   228 #define _MD_CREATE_THREAD _MD_CreateThread
   230 extern void _MD_CleanupBeforeExit(void);
   231 #define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit
   233 NSPR_API(void) _MD_InitRunningCPU(struct _PRCPU *cpu);
   234 #define    _MD_INIT_RUNNING_CPU _MD_InitRunningCPU
   236 /* The _PR_MD_WAIT_LOCK and _PR_MD_WAKEUP_WAITER functions put to sleep and
   237  * awaken a thread which is waiting on a lock or cvar.
   238  */
   239 NSPR_API(PRStatus) _MD_wait(struct PRThread *, PRIntervalTime timeout);
   240 #define _MD_WAIT _MD_wait
   242 NSPR_API(PRStatus) _MD_WakeupWaiter(struct PRThread *);
   243 #define _MD_WAKEUP_WAITER _MD_WakeupWaiter
   245 NSPR_API(void) _MD_SetPriority(struct _MDThread *thread,
   246 	PRThreadPriority newPri);
   247 #define _MD_SET_PRIORITY _MD_SetPriority
   249 #endif /* PTHREADS_USER */
   251 #endif /* nspr_pthread_defs_h___ */

mercurial