|
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/. */ |
|
5 |
|
6 #ifndef nspr_pthread_defs_h___ |
|
7 #define nspr_pthread_defs_h___ |
|
8 |
|
9 #include <pthread.h> |
|
10 #include "prthread.h" |
|
11 |
|
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; |
|
20 |
|
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)) |
|
27 |
|
28 #define _MD_SET_CURRENT_THREAD(newval) \ |
|
29 pthread_setspecific(current_thread_key, (void *)newval) |
|
30 |
|
31 #define _MD_SET_CURRENT_CPU(newval) \ |
|
32 pthread_setspecific(current_cpu_key, (void *)newval) |
|
33 |
|
34 #define _MD_SET_LAST_THREAD(newval) \ |
|
35 pthread_setspecific(last_thread_key, (void *)newval) |
|
36 |
|
37 #define _MD_SET_INTSOFF(_val) |
|
38 #define _MD_GET_INTSOFF() 1 |
|
39 |
|
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 |
|
52 |
|
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 |
|
64 |
|
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 |
|
75 |
|
76 |
|
77 /* Machine-dependent (MD) data structures */ |
|
78 |
|
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 }; |
|
88 |
|
89 struct _MDThreadStack { |
|
90 PRInt8 notused; |
|
91 }; |
|
92 |
|
93 struct _MDLock { |
|
94 pthread_mutex_t mutex; |
|
95 }; |
|
96 |
|
97 struct _MDSemaphore { |
|
98 PRInt8 notused; |
|
99 }; |
|
100 |
|
101 struct _MDCVar { |
|
102 pthread_mutex_t mutex; |
|
103 }; |
|
104 |
|
105 struct _MDSegment { |
|
106 PRInt8 notused; |
|
107 }; |
|
108 |
|
109 /* |
|
110 * md-specific cpu structure field |
|
111 */ |
|
112 #define _PR_MD_MAX_OSFD FD_SETSIZE |
|
113 |
|
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 }; |
|
128 |
|
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) |
|
142 |
|
143 #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu) 32 |
|
144 |
|
145 struct _MDCPU { |
|
146 jmp_buf jb; |
|
147 pthread_t pthread; |
|
148 struct _MDCPU_Unix md_unix; |
|
149 }; |
|
150 |
|
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 */ |
|
157 |
|
158 extern pthread_mutex_t _pr_heapLock; |
|
159 |
|
160 #define _PR_LOCK(lock) pthread_mutex_lock(lock) |
|
161 |
|
162 #define _PR_UNLOCK(lock) pthread_mutex_unlock(lock) |
|
163 |
|
164 |
|
165 #define _PR_LOCK_HEAP() { \ |
|
166 if (_pr_primordialCPU) { \ |
|
167 _PR_LOCK(_pr_heapLock); \ |
|
168 } |
|
169 |
|
170 #define _PR_UNLOCK_HEAP() if (_pr_primordialCPU) { \ |
|
171 _PR_UNLOCK(_pr_heapLock); \ |
|
172 } \ |
|
173 } |
|
174 |
|
175 NSPR_API(PRStatus) _MD_NEW_LOCK(struct _MDLock *md); |
|
176 NSPR_API(void) _MD_FREE_LOCK(struct _MDLock *lockp); |
|
177 |
|
178 #define _MD_LOCK(_lockp) _PR_LOCK(&(_lockp)->mutex) |
|
179 #define _MD_UNLOCK(_lockp) _PR_UNLOCK(&(_lockp)->mutex) |
|
180 |
|
181 #define _MD_INIT_IO() |
|
182 #define _MD_IOQ_LOCK() |
|
183 #define _MD_IOQ_UNLOCK() |
|
184 #define _MD_CHECK_FOR_EXIT() |
|
185 |
|
186 NSPR_API(PRStatus) _MD_InitThread(struct PRThread *thread); |
|
187 #define _MD_INIT_THREAD _MD_InitThread |
|
188 #define _MD_INIT_ATTACHED_THREAD _MD_InitThread |
|
189 |
|
190 NSPR_API(void) _MD_ExitThread(struct PRThread *thread); |
|
191 #define _MD_EXIT_THREAD _MD_ExitThread |
|
192 |
|
193 NSPR_API(void) _MD_SuspendThread(struct PRThread *thread); |
|
194 #define _MD_SUSPEND_THREAD _MD_SuspendThread |
|
195 |
|
196 NSPR_API(void) _MD_ResumeThread(struct PRThread *thread); |
|
197 #define _MD_RESUME_THREAD _MD_ResumeThread |
|
198 |
|
199 NSPR_API(void) _MD_SuspendCPU(struct _PRCPU *thread); |
|
200 #define _MD_SUSPEND_CPU _MD_SuspendCPU |
|
201 |
|
202 NSPR_API(void) _MD_ResumeCPU(struct _PRCPU *thread); |
|
203 #define _MD_RESUME_CPU _MD_ResumeCPU |
|
204 |
|
205 #define _MD_BEGIN_SUSPEND_ALL() |
|
206 #define _MD_END_SUSPEND_ALL() |
|
207 #define _MD_BEGIN_RESUME_ALL() |
|
208 #define _MD_END_RESUME_ALL() |
|
209 |
|
210 NSPR_API(void) _MD_EarlyInit(void); |
|
211 #define _MD_EARLY_INIT _MD_EarlyInit |
|
212 |
|
213 #define _MD_FINAL_INIT _PR_UnixInit |
|
214 |
|
215 NSPR_API(void) _MD_InitLocks(void); |
|
216 #define _MD_INIT_LOCKS _MD_InitLocks |
|
217 |
|
218 NSPR_API(void) _MD_CleanThread(struct PRThread *thread); |
|
219 #define _MD_CLEAN_THREAD _MD_CleanThread |
|
220 |
|
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 |
|
229 |
|
230 extern void _MD_CleanupBeforeExit(void); |
|
231 #define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit |
|
232 |
|
233 NSPR_API(void) _MD_InitRunningCPU(struct _PRCPU *cpu); |
|
234 #define _MD_INIT_RUNNING_CPU _MD_InitRunningCPU |
|
235 |
|
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 |
|
241 |
|
242 NSPR_API(PRStatus) _MD_WakeupWaiter(struct PRThread *); |
|
243 #define _MD_WAKEUP_WAITER _MD_WakeupWaiter |
|
244 |
|
245 NSPR_API(void) _MD_SetPriority(struct _MDThread *thread, |
|
246 PRThreadPriority newPri); |
|
247 #define _MD_SET_PRIORITY _MD_SetPriority |
|
248 |
|
249 #endif /* PTHREADS_USER */ |
|
250 |
|
251 #endif /* nspr_pthread_defs_h___ */ |