1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is the Netscape Portable Runtime (NSPR).
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998-2000
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
38
#ifndef nspr_pth_defs_h_
39
#define nspr_pth_defs_h_
42
** Appropriate definitions of entry points not used in a pthreads world
44
#define _PR_MD_BLOCK_CLOCK_INTERRUPTS()
45
#define _PR_MD_UNBLOCK_CLOCK_INTERRUPTS()
46
#define _PR_MD_DISABLE_CLOCK_INTERRUPTS()
47
#define _PR_MD_ENABLE_CLOCK_INTERRUPTS()
49
/* In good standards fashion, the DCE threads (based on posix-4) are not
50
* quite the same as newer posix implementations. These are mostly name
51
* changes and small differences, so macros usually do the trick
54
#define _PT_PTHREAD_MUTEXATTR_INIT pthread_mutexattr_create
55
#define _PT_PTHREAD_MUTEXATTR_DESTROY pthread_mutexattr_delete
56
#define _PT_PTHREAD_MUTEX_INIT(m, a) pthread_mutex_init(&(m), a)
57
#define _PT_PTHREAD_MUTEX_IS_LOCKED(m) (0 == pthread_mutex_trylock(&(m)))
58
#define _PT_PTHREAD_CONDATTR_INIT pthread_condattr_create
59
#define _PT_PTHREAD_COND_INIT(m, a) pthread_cond_init(&(m), a)
60
#define _PT_PTHREAD_CONDATTR_DESTROY pthread_condattr_delete
62
/* Notes about differences between DCE threads and pthreads 10:
63
* 1. pthread_mutex_trylock returns 1 when it locks the mutex
64
* 0 when it does not. The latest pthreads has a set of errno-like
66
* 2. return values from pthread_cond_timedwait are different.
73
* Mutex and condition attributes are not supported. The attr
74
* argument to pthread_mutex_init() and pthread_cond_init() must
77
* The memset calls in _PT_PTHREAD_MUTEX_INIT and _PT_PTHREAD_COND_INIT
78
* are to work around BSDI's using a single bit to indicate a mutex
79
* or condition variable is initialized. This entire BSDI section
80
* will go away when BSDI releases updated threads libraries for
83
#define _PT_PTHREAD_MUTEXATTR_INIT(x) 0
84
#define _PT_PTHREAD_MUTEXATTR_DESTROY(x) /* */
85
#define _PT_PTHREAD_MUTEX_INIT(m, a) (memset(&(m), 0, sizeof(m)), \
86
pthread_mutex_init(&(m), NULL))
87
#define _PT_PTHREAD_MUTEX_IS_LOCKED(m) (EBUSY == pthread_mutex_trylock(&(m)))
88
#define _PT_PTHREAD_CONDATTR_INIT(x) 0
89
#define _PT_PTHREAD_CONDATTR_DESTROY(x) /* */
90
#define _PT_PTHREAD_COND_INIT(m, a) (memset(&(m), 0, sizeof(m)), \
91
pthread_cond_init(&(m), NULL))
93
#define _PT_PTHREAD_MUTEXATTR_INIT pthread_mutexattr_init
94
#define _PT_PTHREAD_MUTEXATTR_DESTROY pthread_mutexattr_destroy
95
#define _PT_PTHREAD_MUTEX_INIT(m, a) pthread_mutex_init(&(m), &(a))
97
#define _PT_PTHREAD_MUTEX_IS_LOCKED(m) pt_pthread_mutex_is_locked(&(m))
99
#define _PT_PTHREAD_MUTEX_IS_LOCKED(m) (EBUSY == pthread_mutex_trylock(&(m)))
102
/* Conditional attribute init and destroy aren't implemented in bionic. */
103
#define _PT_PTHREAD_CONDATTR_INIT(x) 0
104
#define _PT_PTHREAD_CONDATTR_DESTROY(x) /* */
106
#define _PT_PTHREAD_CONDATTR_INIT pthread_condattr_init
107
#define _PT_PTHREAD_CONDATTR_DESTROY pthread_condattr_destroy
109
#define _PT_PTHREAD_COND_INIT(m, a) pthread_cond_init(&(m), &(a))
112
/* The pthreads standard does not specify an invalid value for the
113
* pthread_t handle. (0 is usually an invalid pthread identifier
114
* but there are exceptions, for example, DG/UX.) These macros
115
* define a way to set the handle to or compare the handle with an
116
* invalid identifier. These macros are not portable and may be
117
* more of a problem as we adapt to more pthreads implementations.
118
* They are only used in the PRMonitor functions. Do not use them
121
* Unfortunately some of our clients depend on certain properties
122
* of our PRMonitor implementation, preventing us from replacing
123
* it by a portable implementation.
124
* - High-performance servers like the fact that PR_EnterMonitor
125
* only calls PR_Lock and PR_ExitMonitor only calls PR_Unlock.
126
* (A portable implementation would use a PRLock and a PRCondVar
127
* to implement the recursive lock in a monitor and call both
128
* PR_Lock and PR_Unlock in PR_EnterMonitor and PR_ExitMonitor.)
129
* Unfortunately this forces us to read the monitor owner field
130
* without holding a lock.
131
* - One way to make it safe to read the monitor owner field
132
* without holding a lock is to make that field a PRThread*
133
* (one should be able to read a pointer with a single machine
134
* instruction). However, PR_GetCurrentThread calls calloc if
135
* it is called by a thread that was not created by NSPR. The
136
* malloc tracing tools in the Mozilla client use PRMonitor for
137
* locking in their malloc, calloc, and free functions. If
138
* PR_EnterMonitor calls any of these functions, infinite
141
#if defined(_PR_DCETHREADS)
142
#define _PT_PTHREAD_INVALIDATE_THR_HANDLE(t) \
143
memset(&(t), 0, sizeof(pthread_t))
144
#define _PT_PTHREAD_THR_HANDLE_IS_INVALID(t) \
145
(!memcmp(&(t), &pt_zero_tid, sizeof(pthread_t)))
146
#define _PT_PTHREAD_COPY_THR_HANDLE(st, dt) (dt) = (st)
147
#elif defined(IRIX) || defined(OSF1) || defined(AIX) || defined(SOLARIS) \
148
|| defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \
149
|| defined(HPUX) || defined(FREEBSD) \
150
|| defined(NETBSD) || defined(OPENBSD) || defined(BSDI) \
151
|| defined(NTO) || defined(DARWIN) \
152
|| defined(UNIXWARE) || defined(RISCOS) || defined(SYMBIAN)
154
/* Hurd pthreads don't have an invalid value for pthread_t. -- rmh */
155
#error Using Hurd pthreads
157
#define _PT_PTHREAD_INVALIDATE_THR_HANDLE(t) (t) = 0
158
#define _PT_PTHREAD_THR_HANDLE_IS_INVALID(t) (t) == 0
159
#define _PT_PTHREAD_COPY_THR_HANDLE(st, dt) (dt) = (st)
161
#error "pthreads is not supported for this architecture"
164
#if defined(_PR_DCETHREADS)
165
#define _PT_PTHREAD_ATTR_INIT pthread_attr_create
166
#define _PT_PTHREAD_ATTR_DESTROY pthread_attr_delete
167
#define _PT_PTHREAD_CREATE(t, a, f, r) pthread_create(t, a, f, r)
168
#define _PT_PTHREAD_KEY_CREATE pthread_keycreate
169
#define _PT_PTHREAD_ATTR_SETSCHEDPOLICY pthread_attr_setsched
170
#define _PT_PTHREAD_ATTR_GETSTACKSIZE(a, s) \
171
(*(s) = pthread_attr_getstacksize(*(a)), 0)
172
#define _PT_PTHREAD_GETSPECIFIC(k, r) \
173
pthread_getspecific((k), (pthread_addr_t *) &(r))
174
#elif defined(_PR_PTHREADS)
175
#define _PT_PTHREAD_ATTR_INIT pthread_attr_init
176
#define _PT_PTHREAD_ATTR_DESTROY pthread_attr_destroy
177
#define _PT_PTHREAD_CREATE(t, a, f, r) pthread_create(t, &a, f, r)
178
#define _PT_PTHREAD_KEY_CREATE pthread_key_create
179
#define _PT_PTHREAD_ATTR_SETSCHEDPOLICY pthread_attr_setschedpolicy
180
#define _PT_PTHREAD_ATTR_GETSTACKSIZE(a, s) pthread_attr_getstacksize(a, s)
181
#define _PT_PTHREAD_GETSPECIFIC(k, r) (r) = pthread_getspecific(k)
183
#error "Cannot determine pthread strategy"
186
#if defined(_PR_DCETHREADS)
187
#define _PT_PTHREAD_EXPLICIT_SCHED _PT_PTHREAD_DEFAULT_SCHED
191
* pthread_mutex_trylock returns different values in DCE threads and
194
#if defined(_PR_DCETHREADS)
195
#define PT_TRYLOCK_SUCCESS 1
196
#define PT_TRYLOCK_BUSY 0
198
#define PT_TRYLOCK_SUCCESS 0
199
#define PT_TRYLOCK_BUSY EBUSY
203
* These platforms don't have sigtimedwait()
205
#if (defined(AIX) && !defined(AIX4_3_PLUS)) \
206
|| defined(LINUX) || defined(__GNU__)|| defined(__GLIBC__) \
207
|| defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \
208
|| defined(BSDI) || defined(UNIXWARE) \
209
|| defined(DARWIN) || defined(SYMBIAN)
210
#define PT_NO_SIGTIMEDWAIT
214
#define PT_PRIO_MIN PRI_OTHER_MIN
215
#define PT_PRIO_MAX PRI_OTHER_MAX
217
#include <sys/sched.h>
218
#define PT_PRIO_MIN PX_PRIO_MIN
219
#define PT_PRIO_MAX PX_PRIO_MAX
221
#include <sys/priv.h>
222
#include <sys/sched.h>
223
#ifndef PTHREAD_CREATE_JOINABLE
224
#define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED
226
#define PT_PRIO_MIN DEFAULT_PRIO
227
#define PT_PRIO_MAX DEFAULT_PRIO
230
#if defined(_PR_DCETHREADS)
231
#define PT_PRIO_MIN PRI_OTHER_MIN
232
#define PT_PRIO_MAX PRI_OTHER_MAX
233
#else /* defined(_PR_DCETHREADS) */
234
#include <sys/sched.h>
235
#define PT_PRIO_MIN sched_get_priority_min(SCHED_OTHER)
236
#define PT_PRIO_MAX sched_get_priority_max(SCHED_OTHER)
237
#endif /* defined(_PR_DCETHREADS) */
239
#elif defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \
240
|| defined(FREEBSD) || defined(SYMBIAN)
241
#define PT_PRIO_MIN sched_get_priority_min(SCHED_OTHER)
242
#define PT_PRIO_MAX sched_get_priority_max(SCHED_OTHER)
245
* Neutrino has functions that return the priority range but
246
* they return invalid numbers, so I just hard coded these here
247
* for now. Jerry.Kirk@Nexarecorp.com
249
#define PT_PRIO_MIN 0
250
#define PT_PRIO_MAX 30
251
#elif defined(SOLARIS)
253
* Solaris doesn't seem to have macros for the min/max priorities.
254
* The range of 0-127 is mentioned in the pthread_setschedparam(3T)
255
* man pages, and pthread_setschedparam indeed allows 0-127. However,
256
* pthread_attr_setschedparam does not allow 0; it allows 1-127.
258
#define PT_PRIO_MIN 1
259
#define PT_PRIO_MAX 127
260
#elif defined(OPENBSD)
261
#define PT_PRIO_MIN 0
262
#define PT_PRIO_MAX 31
263
#elif defined(NETBSD) \
264
|| defined(BSDI) || defined(DARWIN) || defined(UNIXWARE) \
265
|| defined(RISCOS) /* XXX */
266
#define PT_PRIO_MIN 0
267
#define PT_PRIO_MAX 126
269
#error "pthreads is not supported for this architecture"
273
* The _PT_PTHREAD_YIELD function is called from a signal handler.
274
* Needed for garbage collection -- Look at PR_Suspend/PR_Resume
277
#if defined(_PR_DCETHREADS)
278
#define _PT_PTHREAD_YIELD() pthread_yield()
281
* sched_yield can't be called from a signal handler. Must use
284
#define _PT_PTHREAD_YIELD() pthread_yield_np()
286
extern int (*_PT_aix_yield_fcn)();
287
#define _PT_PTHREAD_YIELD() (*_PT_aix_yield_fcn)()
290
#define _PT_PTHREAD_YIELD() \
292
struct timespec onemillisec = {0}; \
293
onemillisec.tv_nsec = 1000000L; \
294
nanosleep(&onemillisec,NULL); \
296
#elif defined(HPUX) || defined(SOLARIS) \
297
|| defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \
298
|| defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \
299
|| defined(BSDI) || defined(NTO) || defined(DARWIN) \
300
|| defined(UNIXWARE) || defined(RISCOS) || defined(SYMBIAN)
301
#define _PT_PTHREAD_YIELD() sched_yield()
303
#error "Need to define _PT_PTHREAD_YIELD for this platform"
306
#endif /* nspr_pth_defs_h_ */