1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/include/md/_nspr_pthread.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,251 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nspr_pthread_defs_h___ 1.10 +#define nspr_pthread_defs_h___ 1.11 + 1.12 +#include <pthread.h> 1.13 +#include "prthread.h" 1.14 + 1.15 +#if defined(PTHREADS_USER) 1.16 +/* 1.17 +** Thread Local Storage 1.18 +*/ 1.19 +extern pthread_key_t current_thread_key; 1.20 +extern pthread_key_t current_cpu_key; 1.21 +extern pthread_key_t last_thread_key; 1.22 +extern pthread_key_t intsoff_key; 1.23 + 1.24 +#define _MD_CURRENT_THREAD() \ 1.25 + ((struct PRThread *) pthread_getspecific(current_thread_key)) 1.26 +#define _MD_CURRENT_CPU() \ 1.27 + ((struct _PRCPU *) pthread_getspecific(current_cpu_key)) 1.28 +#define _MD_LAST_THREAD() \ 1.29 + ((struct PRThread *) pthread_getspecific(last_thread_key)) 1.30 + 1.31 +#define _MD_SET_CURRENT_THREAD(newval) \ 1.32 + pthread_setspecific(current_thread_key, (void *)newval) 1.33 + 1.34 +#define _MD_SET_CURRENT_CPU(newval) \ 1.35 + pthread_setspecific(current_cpu_key, (void *)newval) 1.36 + 1.37 +#define _MD_SET_LAST_THREAD(newval) \ 1.38 + pthread_setspecific(last_thread_key, (void *)newval) 1.39 + 1.40 +#define _MD_SET_INTSOFF(_val) 1.41 +#define _MD_GET_INTSOFF() 1 1.42 + 1.43 +/* 1.44 +** Initialize the thread context preparing it to execute _main. 1.45 +*/ 1.46 +#define _MD_INIT_CONTEXT(_thread, _sp, _main, status) \ 1.47 + PR_BEGIN_MACRO \ 1.48 + *status = PR_TRUE; \ 1.49 + if (SAVE_CONTEXT(_thread)) { \ 1.50 + (*_main)(); \ 1.51 + } \ 1.52 + _MD_SET_THR_SP(_thread, _sp); \ 1.53 + _thread->no_sched = 0; \ 1.54 + PR_END_MACRO 1.55 + 1.56 +#define _MD_SWITCH_CONTEXT(_thread) \ 1.57 + PR_BEGIN_MACRO \ 1.58 + PR_ASSERT(_thread->no_sched); \ 1.59 + if (!SAVE_CONTEXT(_thread)) { \ 1.60 + (_thread)->md.errcode = errno; \ 1.61 + _MD_SET_LAST_THREAD(_thread); \ 1.62 + _PR_Schedule(); \ 1.63 + } else { \ 1.64 + (_MD_LAST_THREAD())->no_sched = 0; \ 1.65 + } \ 1.66 + PR_END_MACRO 1.67 + 1.68 +/* 1.69 +** Restore a thread context, saved by _MD_SWITCH_CONTEXT 1.70 +*/ 1.71 +#define _MD_RESTORE_CONTEXT(_thread) \ 1.72 + PR_BEGIN_MACRO \ 1.73 + errno = (_thread)->md.errcode; \ 1.74 + _MD_SET_CURRENT_THREAD(_thread); \ 1.75 + _thread->no_sched = 1; \ 1.76 + GOTO_CONTEXT(_thread); \ 1.77 + PR_END_MACRO 1.78 + 1.79 + 1.80 +/* Machine-dependent (MD) data structures */ 1.81 + 1.82 +struct _MDThread { 1.83 + jmp_buf jb; 1.84 + int id; 1.85 + int errcode; 1.86 + pthread_t pthread; 1.87 + pthread_mutex_t pthread_mutex; 1.88 + pthread_cond_t pthread_cond; 1.89 + int wait; 1.90 +}; 1.91 + 1.92 +struct _MDThreadStack { 1.93 + PRInt8 notused; 1.94 +}; 1.95 + 1.96 +struct _MDLock { 1.97 + pthread_mutex_t mutex; 1.98 +}; 1.99 + 1.100 +struct _MDSemaphore { 1.101 + PRInt8 notused; 1.102 +}; 1.103 + 1.104 +struct _MDCVar { 1.105 + pthread_mutex_t mutex; 1.106 +}; 1.107 + 1.108 +struct _MDSegment { 1.109 + PRInt8 notused; 1.110 +}; 1.111 + 1.112 +/* 1.113 + * md-specific cpu structure field 1.114 + */ 1.115 +#define _PR_MD_MAX_OSFD FD_SETSIZE 1.116 + 1.117 +struct _MDCPU_Unix { 1.118 + PRCList ioQ; 1.119 + PRUint32 ioq_timeout; 1.120 + PRInt32 ioq_max_osfd; 1.121 + PRInt32 ioq_osfd_cnt; 1.122 +#ifndef _PR_USE_POLL 1.123 + fd_set fd_read_set, fd_write_set, fd_exception_set; 1.124 + PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD], 1.125 + fd_exception_cnt[_PR_MD_MAX_OSFD]; 1.126 +#else 1.127 + struct pollfd *ioq_pollfds; 1.128 + int ioq_pollfds_size; 1.129 +#endif /* _PR_USE_POLL */ 1.130 +}; 1.131 + 1.132 +#define _PR_IOQ(_cpu) ((_cpu)->md.md_unix.ioQ) 1.133 +#define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu)) 1.134 +#define _PR_FD_READ_SET(_cpu) ((_cpu)->md.md_unix.fd_read_set) 1.135 +#define _PR_FD_READ_CNT(_cpu) ((_cpu)->md.md_unix.fd_read_cnt) 1.136 +#define _PR_FD_WRITE_SET(_cpu) ((_cpu)->md.md_unix.fd_write_set) 1.137 +#define _PR_FD_WRITE_CNT(_cpu) ((_cpu)->md.md_unix.fd_write_cnt) 1.138 +#define _PR_FD_EXCEPTION_SET(_cpu) ((_cpu)->md.md_unix.fd_exception_set) 1.139 +#define _PR_FD_EXCEPTION_CNT(_cpu) ((_cpu)->md.md_unix.fd_exception_cnt) 1.140 +#define _PR_IOQ_TIMEOUT(_cpu) ((_cpu)->md.md_unix.ioq_timeout) 1.141 +#define _PR_IOQ_MAX_OSFD(_cpu) ((_cpu)->md.md_unix.ioq_max_osfd) 1.142 +#define _PR_IOQ_OSFD_CNT(_cpu) ((_cpu)->md.md_unix.ioq_osfd_cnt) 1.143 +#define _PR_IOQ_POLLFDS(_cpu) ((_cpu)->md.md_unix.ioq_pollfds) 1.144 +#define _PR_IOQ_POLLFDS_SIZE(_cpu) ((_cpu)->md.md_unix.ioq_pollfds_size) 1.145 + 1.146 +#define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu) 32 1.147 + 1.148 +struct _MDCPU { 1.149 + jmp_buf jb; 1.150 + pthread_t pthread; 1.151 + struct _MDCPU_Unix md_unix; 1.152 +}; 1.153 + 1.154 +/* 1.155 +#define _MD_NEW_LOCK(lock) PR_SUCCESS 1.156 +#define _MD_FREE_LOCK(lock) 1.157 +#define _MD_LOCK(lock) 1.158 +#define _MD_UNLOCK(lock) 1.159 +*/ 1.160 + 1.161 +extern pthread_mutex_t _pr_heapLock; 1.162 + 1.163 +#define _PR_LOCK(lock) pthread_mutex_lock(lock) 1.164 + 1.165 +#define _PR_UNLOCK(lock) pthread_mutex_unlock(lock) 1.166 + 1.167 + 1.168 +#define _PR_LOCK_HEAP() { \ 1.169 + if (_pr_primordialCPU) { \ 1.170 + _PR_LOCK(_pr_heapLock); \ 1.171 + } 1.172 + 1.173 +#define _PR_UNLOCK_HEAP() if (_pr_primordialCPU) { \ 1.174 + _PR_UNLOCK(_pr_heapLock); \ 1.175 + } \ 1.176 + } 1.177 + 1.178 +NSPR_API(PRStatus) _MD_NEW_LOCK(struct _MDLock *md); 1.179 +NSPR_API(void) _MD_FREE_LOCK(struct _MDLock *lockp); 1.180 + 1.181 +#define _MD_LOCK(_lockp) _PR_LOCK(&(_lockp)->mutex) 1.182 +#define _MD_UNLOCK(_lockp) _PR_UNLOCK(&(_lockp)->mutex) 1.183 + 1.184 +#define _MD_INIT_IO() 1.185 +#define _MD_IOQ_LOCK() 1.186 +#define _MD_IOQ_UNLOCK() 1.187 +#define _MD_CHECK_FOR_EXIT() 1.188 + 1.189 +NSPR_API(PRStatus) _MD_InitThread(struct PRThread *thread); 1.190 +#define _MD_INIT_THREAD _MD_InitThread 1.191 +#define _MD_INIT_ATTACHED_THREAD _MD_InitThread 1.192 + 1.193 +NSPR_API(void) _MD_ExitThread(struct PRThread *thread); 1.194 +#define _MD_EXIT_THREAD _MD_ExitThread 1.195 + 1.196 +NSPR_API(void) _MD_SuspendThread(struct PRThread *thread); 1.197 +#define _MD_SUSPEND_THREAD _MD_SuspendThread 1.198 + 1.199 +NSPR_API(void) _MD_ResumeThread(struct PRThread *thread); 1.200 +#define _MD_RESUME_THREAD _MD_ResumeThread 1.201 + 1.202 +NSPR_API(void) _MD_SuspendCPU(struct _PRCPU *thread); 1.203 +#define _MD_SUSPEND_CPU _MD_SuspendCPU 1.204 + 1.205 +NSPR_API(void) _MD_ResumeCPU(struct _PRCPU *thread); 1.206 +#define _MD_RESUME_CPU _MD_ResumeCPU 1.207 + 1.208 +#define _MD_BEGIN_SUSPEND_ALL() 1.209 +#define _MD_END_SUSPEND_ALL() 1.210 +#define _MD_BEGIN_RESUME_ALL() 1.211 +#define _MD_END_RESUME_ALL() 1.212 + 1.213 +NSPR_API(void) _MD_EarlyInit(void); 1.214 +#define _MD_EARLY_INIT _MD_EarlyInit 1.215 + 1.216 +#define _MD_FINAL_INIT _PR_UnixInit 1.217 + 1.218 +NSPR_API(void) _MD_InitLocks(void); 1.219 +#define _MD_INIT_LOCKS _MD_InitLocks 1.220 + 1.221 +NSPR_API(void) _MD_CleanThread(struct PRThread *thread); 1.222 +#define _MD_CLEAN_THREAD _MD_CleanThread 1.223 + 1.224 +NSPR_API(PRStatus) _MD_CreateThread( 1.225 + struct PRThread *thread, 1.226 + void (*start) (void *), 1.227 + PRThreadPriority priority, 1.228 + PRThreadScope scope, 1.229 + PRThreadState state, 1.230 + PRUint32 stackSize); 1.231 +#define _MD_CREATE_THREAD _MD_CreateThread 1.232 + 1.233 +extern void _MD_CleanupBeforeExit(void); 1.234 +#define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit 1.235 + 1.236 +NSPR_API(void) _MD_InitRunningCPU(struct _PRCPU *cpu); 1.237 +#define _MD_INIT_RUNNING_CPU _MD_InitRunningCPU 1.238 + 1.239 +/* The _PR_MD_WAIT_LOCK and _PR_MD_WAKEUP_WAITER functions put to sleep and 1.240 + * awaken a thread which is waiting on a lock or cvar. 1.241 + */ 1.242 +NSPR_API(PRStatus) _MD_wait(struct PRThread *, PRIntervalTime timeout); 1.243 +#define _MD_WAIT _MD_wait 1.244 + 1.245 +NSPR_API(PRStatus) _MD_WakeupWaiter(struct PRThread *); 1.246 +#define _MD_WAKEUP_WAITER _MD_WakeupWaiter 1.247 + 1.248 +NSPR_API(void) _MD_SetPriority(struct _MDThread *thread, 1.249 + PRThreadPriority newPri); 1.250 +#define _MD_SET_PRIORITY _MD_SetPriority 1.251 + 1.252 +#endif /* PTHREADS_USER */ 1.253 + 1.254 +#endif /* nspr_pthread_defs_h___ */