nsprpub/pr/include/md/_nspr_pthread.h

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

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

mercurial