OSDN Git Service

38629f44b010a9361b90635d09d3ead376048f9f
[pf3gnuchains/gcc-fork.git] / libjava / include / posix-threads.h
1 // -*- c++ -*-
2 // posix-threads.h - Defines for using POSIX threads.
3
4 /* Copyright (C) 1998, 1999  Cygnus Solutions
5
6    This file is part of libgcj.
7
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
10 details.  */
11
12 #ifndef __JV_POSIX_THREADS__
13 #define __JV_POSIX_THREADS__
14
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).
19
20 #include <pthread.h>
21 #include <sched.h>
22
23 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE) || defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
24 #  define HAVE_RECURSIVE_MUTEX 1
25 #endif
26
27
28 //
29 // Typedefs.
30 //
31
32 typedef pthread_cond_t _Jv_ConditionVariable_t;
33
34 #if defined (PTHREAD_MUTEX_HAVE_M_COUNT) || defined (PTHREAD_MUTEX_HAVE___M_COUNT)
35
36 // On Linux we use implementation details of mutexes in order to get
37 // faster results.
38 typedef pthread_mutex_t _Jv_Mutex_t;
39
40 #else /* LINUX_THREADS */
41
42 #define PTHREAD_MUTEX_IS_STRUCT
43
44 typedef struct
45 {
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.
51
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.
56   pthread_cond_t cond;
57   // Thread holding this mutex.  If COUNT is 0, no thread is holding.
58   pthread_t thread;
59 #endif /* HAVE_RECURSIVE_MUTEX */
60
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
66   // recursive mutexes.
67   int count;
68 } _Jv_Mutex_t;
69
70 #endif
71
72 typedef struct
73 {
74   // Flag values are defined in implementation.
75   int flags;
76
77   // Actual thread id.
78   pthread_t thread;
79
80   // Exception we want to throw when cancelled.
81   void *exception;
82 } _Jv_Thread_t;
83 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
84
85
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)
90 {
91 #if ! defined (PTHREAD_MUTEX_IS_STRUCT)
92   return mu;
93 #elif defined (HAVE_RECURSIVE_MUTEX)
94   return &mu->mutex;
95 #else
96   return &mu->mutex2;
97 #endif
98 }
99
100 #include <stdio.h>
101
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
105 // this out.
106 inline int
107 _Jv_PthreadCheckMonitor (_Jv_Mutex_t *mu)
108 {
109   pthread_mutex_t *pmu = _Jv_PthreadGetMutex (mu);
110   // See if the mutex is locked by this thread.
111   if (pthread_mutex_trylock (pmu))
112     return 1;
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   // In glibc 2.1, the first time the mutex is grabbed __m_count is
118   // set to 0 and __m_owner is set to pthread_self().
119   int r = ! pmu->__m_count;
120 #else
121   int r = mu->count == 0;
122 #endif
123   pthread_mutex_unlock (pmu);
124   return r;
125 }
126
127 //
128 // Condition variables.
129 //
130
131 inline void
132 _Jv_CondInit (_Jv_ConditionVariable_t *cv)
133 {
134   pthread_cond_init (cv, 0);
135 }
136
137 #ifndef LINUX_THREADS
138
139 // pthread_cond_destroy does nothing on Linux and it is a win to avoid
140 // defining this macro.
141
142 #define _Jv_HaveCondDestroy
143
144 inline void
145 _Jv_CondDestroy (_Jv_ConditionVariable_t *cv)
146 {
147   pthread_cond_destroy (cv);
148 }
149
150 #endif /* LINUX_THREADS */
151
152 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
153                   jlong millis, jint nanos);
154
155 inline int
156 _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
157 {
158   return _Jv_PthreadCheckMonitor (mu) || pthread_cond_signal (cv);
159 }
160
161 inline int
162 _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu)
163 {
164   return _Jv_PthreadCheckMonitor (mu) || pthread_cond_broadcast (cv);
165 }
166
167
168 //
169 // Mutexes.
170 //
171
172 #ifdef RECURSIVE_MUTEX_IS_DEFAULT
173 inline void
174 _Jv_MutexInit (_Jv_Mutex_t *mu)
175 {
176   pthread_mutex_init (_Jv_PthreadGetMutex (mu), NULL);
177 #ifdef PTHREAD_MUTEX_IS_STRUCT
178   mu->count = 0;
179 #endif
180 }
181 #else
182 void _Jv_MutexInit (_Jv_Mutex_t *mu);
183 #endif
184
185 #ifndef LINUX_THREADS
186
187 // pthread_mutex_destroy does nothing on Linux and it is a win to avoid
188 // defining this macro.
189
190 #define _Jv_HaveMutexDestroy
191
192 #ifdef HAVE_RECURSIVE_MUTEX
193
194 inline void
195 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
196 {
197   pthread_mutex_destroy (mu);
198 }
199
200 #else /* HAVE_RECURSIVE_MUTEX */
201
202 extern void _Jv_MutexDestroy (_Jv_Mutex_t *mu);
203
204 #endif /* HAVE_RECURSIVE_MUTEX */
205 #endif /* LINUX_THREADS */
206
207 #ifdef HAVE_RECURSIVE_MUTEX
208
209 inline int
210 _Jv_MutexLock (_Jv_Mutex_t *mu)
211 {
212   int r = pthread_mutex_lock (mu);
213 #ifdef PTHREAD_MUTEX_IS_STRUCT
214   if (! r)
215     ++mu->count;
216 #endif
217   return r;
218 }
219
220 inline int
221 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
222 {
223   int r = pthread_mutex_unlock (mu);
224 #ifdef PTHREAD_MUTEX_IS_STRUCT
225   if (! r)
226     --mu->count;
227 #endif
228   return r;
229 }
230
231 #else /* HAVE_RECURSIVE_MUTEX */
232
233 extern int _Jv_MutexLock (_Jv_Mutex_t *mu);
234 extern int _Jv_MutexUnlock (_Jv_Mutex_t *mu);
235
236 #endif /* HAVE_RECURSIVE_MUTEX */
237
238
239 //
240 // Thread creation and manipulation.
241 //
242
243 void _Jv_InitThreads (void);
244
245 void _Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *thread);
246
247 inline java::lang::Thread *
248 _Jv_ThreadCurrent (void)
249 {
250   extern pthread_key_t _Jv_ThreadKey;
251   return (java::lang::Thread *) pthread_getspecific (_Jv_ThreadKey);
252 }
253
254 inline _Jv_Thread_t *
255 _Jv_ThreadCurrentData (void)
256 {
257   extern pthread_key_t _Jv_ThreadDataKey;
258   return (_Jv_Thread_t *) pthread_getspecific (_Jv_ThreadDataKey);
259 }
260
261 inline void
262 _Jv_ThreadYield (void)
263 {
264 #ifdef HAVE_SCHED_YIELD
265   sched_yield ();
266 #endif /* HAVE_SCHED_YIELD */
267 }
268
269 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
270
271 void _Jv_ThreadCancel (_Jv_Thread_t *data, void *error);
272
273 // Like Cancel, but doesn't run cleanups.
274 inline void
275 _Jv_ThreadDestroy (_Jv_Thread_t *)
276 {
277   JvFail ("_Jv_ThreadDestroy");
278 }
279
280 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
281                       _Jv_ThreadStartFunc *meth);
282
283 void _Jv_ThreadWait (void);
284
285 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
286
287 #endif /* __JV_POSIX_THREADS__ */