2 // posix-threads.h - Defines for using POSIX threads.
4 /* Copyright (C) 1998, 1999 Cygnus Solutions
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 #ifndef __JV_POSIX_THREADS__
13 #define __JV_POSIX_THREADS__
15 // NOTE: This file may only reference those pthread functions which
16 // are known not to be overridden by the Boehm GC. If in doubt, scan
17 // boehm-gc/gc.h. This is yucky but lets us avoid including gc.h
18 // everywhere (which would be truly yucky).
23 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE) || defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
24 # define HAVE_RECURSIVE_MUTEX 1
32 typedef pthread_cond_t _Jv_ConditionVariable_t;
34 #if defined (PTHREAD_MUTEX_HAVE_M_COUNT) || defined (PTHREAD_MUTEX_HAVE___M_COUNT)
36 // On Linux we use implementation details of mutexes in order to get
38 typedef pthread_mutex_t _Jv_Mutex_t;
40 #else /* LINUX_THREADS */
42 #define PTHREAD_MUTEX_IS_STRUCT
46 // Mutex used when locking this structure transiently.
47 pthread_mutex_t mutex;
48 #ifndef HAVE_RECURSIVE_MUTEX
49 // Some systems do not have recursive mutexes, so we must simulate
50 // them. Solaris is one such system.
52 // Mutex the thread holds the entire time this mutex is held. This
53 // is used to make condition variables work properly.
54 pthread_mutex_t mutex2;
55 // Condition variable used when waiting for this lock.
57 // Thread holding this mutex. If COUNT is 0, no thread is holding.
59 #endif /* HAVE_RECURSIVE_MUTEX */
61 // Number of times mutex is held. If 0, the lock is not held. We
62 // do this even if we have a native recursive mutex so that we can
63 // keep track of whether the lock is held; this lets us do error
64 // checking. FIXME it would be nice to optimize this; on some
65 // systems we could do so by relying on implementation details of
74 // Flag values are defined in implementation.
80 // Exception we want to throw when cancelled.
83 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
86 // This convenience function is used to return the POSIX mutex
87 // corresponding to our mutex.
88 inline pthread_mutex_t *
89 _Jv_PthreadGetMutex (_Jv_Mutex_t *mu)
91 #if ! defined (PTHREAD_MUTEX_IS_STRUCT)
93 #elif defined (HAVE_RECURSIVE_MUTEX)
102 // This is a convenience function used only by the pthreads thread
103 // implementation. This is slow, but that's too bad -- we need to do
104 // the checks for correctness. It might be nice to be able to compile
107 _Jv_PthreadCheckMonitor (_Jv_Mutex_t *mu)
109 pthread_mutex_t *pmu = _Jv_PthreadGetMutex (mu);
110 // See if the mutex is locked by this thread.
111 if (pthread_mutex_trylock (pmu))
113 #if defined (PTHREAD_MUTEX_HAVE_M_COUNT)
114 // On Linux we exploit knowledge of the implementation.
115 int r = pmu->m_count == 1;
116 #elif defined (PTHREAD_MUTEX_HAVE___M_COUNT)
117 int r = (pthread_t) pmu->__m_owner == pthread_self ();
119 int r = mu->count == 0;
121 pthread_mutex_unlock (pmu);
126 // Condition variables.
130 _Jv_CondInit (_Jv_ConditionVariable_t *cv)
132 pthread_cond_init (cv, 0);
135 #ifndef LINUX_THREADS
137 // pthread_cond_destroy does nothing on Linux and it is a win to avoid
138 // defining this macro.
140 #define _Jv_HaveCondDestroy
143 _Jv_CondDestroy (_Jv_ConditionVariable_t *cv)
145 pthread_cond_destroy (cv);
148 #endif /* LINUX_THREADS */
150 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
151 jlong millis, jint nanos);
154 _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
156 return _Jv_PthreadCheckMonitor (mu) || pthread_cond_signal (cv);
160 _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
162 return _Jv_PthreadCheckMonitor (mu) || pthread_cond_broadcast (cv);
170 #ifdef RECURSIVE_MUTEX_IS_DEFAULT
172 _Jv_MutexInit (_Jv_Mutex_t *mu)
174 pthread_mutex_init (_Jv_PthreadGetMutex (mu), NULL);
175 #ifdef PTHREAD_MUTEX_IS_STRUCT
180 void _Jv_MutexInit (_Jv_Mutex_t *mu);
183 #ifndef LINUX_THREADS
185 // pthread_mutex_destroy does nothing on Linux and it is a win to avoid
186 // defining this macro.
188 #define _Jv_HaveMutexDestroy
190 #ifdef HAVE_RECURSIVE_MUTEX
193 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
195 pthread_mutex_destroy (mu);
198 #else /* HAVE_RECURSIVE_MUTEX */
200 extern void _Jv_MutexDestroy (_Jv_Mutex_t *mu);
202 #endif /* HAVE_RECURSIVE_MUTEX */
203 #endif /* LINUX_THREADS */
205 #ifdef HAVE_RECURSIVE_MUTEX
208 _Jv_MutexLock (_Jv_Mutex_t *mu)
210 int r = pthread_mutex_lock (mu);
211 #ifdef PTHREAD_MUTEX_IS_STRUCT
219 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
221 int r = pthread_mutex_unlock (mu);
222 #ifdef PTHREAD_MUTEX_IS_STRUCT
229 #else /* HAVE_RECURSIVE_MUTEX */
231 extern int _Jv_MutexLock (_Jv_Mutex_t *mu);
232 extern int _Jv_MutexUnlock (_Jv_Mutex_t *mu);
234 #endif /* HAVE_RECURSIVE_MUTEX */
238 // Thread creation and manipulation.
241 void _Jv_InitThreads (void);
243 void _Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *thread);
245 inline java::lang::Thread *
246 _Jv_ThreadCurrent (void)
248 extern pthread_key_t _Jv_ThreadKey;
249 return (java::lang::Thread *) pthread_getspecific (_Jv_ThreadKey);
252 inline _Jv_Thread_t *
253 _Jv_ThreadCurrentData (void)
255 extern pthread_key_t _Jv_ThreadDataKey;
256 return (_Jv_Thread_t *) pthread_getspecific (_Jv_ThreadDataKey);
260 _Jv_ThreadYield (void)
262 #ifdef HAVE_SCHED_YIELD
264 #endif /* HAVE_SCHED_YIELD */
267 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
269 void _Jv_ThreadCancel (_Jv_Thread_t *data, void *error);
271 // Like Cancel, but doesn't run cleanups.
273 _Jv_ThreadDestroy (_Jv_Thread_t *)
275 JvFail ("_Jv_ThreadDestroy");
278 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
279 _Jv_ThreadStartFunc *meth);
281 void _Jv_ThreadWait (void);
283 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
285 #endif /* __JV_POSIX_THREADS__ */