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;
33 #ifdef HAVE_RECURSIVE_MUTEX
34 typedef pthread_mutex_t _Jv_Mutex_t;
36 // Some systems do not have recursive mutexes, so we must simulate
37 // them. Solaris is one such system.
40 // Mutex used when locking this structure transiently.
41 pthread_mutex_t mutex;
42 // Mutex the thread holds the entire time this mutex is held. This
43 // is used to make condition variables work properly.
44 pthread_mutex_t mutex2;
45 // Condition variable used when waiting for this lock.
47 // Thread holding this mutex. If COUNT is 0, no thread is holding.
49 // Number of times mutex is held. If 0, the lock is not held.
52 #endif /* HAVE_RECURSIVE_MUTEX */
56 // Flag values are defined in implementation.
62 // Exception we want to throw when cancelled.
65 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
69 // Condition variables.
73 _Jv_CondInit (_Jv_ConditionVariable_t *cv)
75 pthread_cond_init (cv, 0);
80 // pthread_cond_destroy does nothing on Linux and it is a win to avoid
81 // defining this macro.
83 #define _Jv_HaveCondDestroy
86 _Jv_CondDestroy (_Jv_ConditionVariable_t *cv)
88 pthread_cond_destroy (cv);
91 #endif /* LINUX_THREADS */
93 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
94 jlong millis, jint nanos);
97 _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *)
99 // FIXME: check to see if mutex is held by current thread.
100 return pthread_cond_signal (cv);
104 _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *)
106 // FIXME: check to see if mutex is held by current thread.
107 return pthread_cond_broadcast (cv);
115 #ifdef RECURSIVE_MUTEX_IS_DEFAULT
117 _Jv_MutexInit (_Jv_Mutex_t *mu)
119 pthread_mutex_init (mu, NULL);
122 void _Jv_MutexInit (_Jv_Mutex_t *mu);
125 #ifndef LINUX_THREADS
127 // pthread_mutex_destroy does nothing on Linux and it is a win to avoid
128 // defining this macro.
130 #define _Jv_HaveMutexDestroy
132 #ifdef HAVE_RECURSIVE_MUTEX
135 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
137 pthread_mutex_destroy (mu);
140 #else /* HAVE_RECURSIVE_MUTEX */
142 extern void _Jv_MutexDestroy (_Jv_Mutex_t *mu);
144 #endif /* HAVE_RECURSIVE_MUTEX */
145 #endif /* LINUX_THREADS */
147 #ifdef HAVE_RECURSIVE_MUTEX
150 _Jv_MutexLock (_Jv_Mutex_t *mu)
152 return pthread_mutex_lock (mu);
156 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
158 return pthread_mutex_unlock (mu);
161 #else /* HAVE_RECURSIVE_MUTEX */
163 extern int _Jv_MutexLock (_Jv_Mutex_t *mu);
164 extern int _Jv_MutexUnlock (_Jv_Mutex_t *mu);
166 #endif /* HAVE_RECURSIVE_MUTEX */
170 // Thread creation and manipulation.
173 void _Jv_InitThreads (void);
175 void _Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *thread);
177 inline java::lang::Thread *
178 _Jv_ThreadCurrent (void)
180 extern pthread_key_t _Jv_ThreadKey;
181 return (java::lang::Thread *) pthread_getspecific (_Jv_ThreadKey);
185 _Jv_ThreadYield (void)
187 #ifdef HAVE_SCHED_YIELD
189 #endif /* HAVE_SCHED_YIELD */
192 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
194 void _Jv_ThreadCancel (_Jv_Thread_t *data, void *error);
196 // Like Cancel, but doesn't run cleanups.
198 _Jv_ThreadDestroy (_Jv_Thread_t *)
200 JvFail ("_Jv_ThreadDestroy");
203 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
204 _Jv_ThreadStartFunc *meth);
206 void _Jv_ThreadWait (void);
208 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
210 #endif /* __JV_POSIX_THREADS__ */