* config/i386/i386.c (ix86_expand_fp_absneg_operator): Use
[official-gcc.git] / libjava / include / posix-threads.h
blobccb69ada1a2677bdd97f72459891809ba96715b4
1 // -*- c++ -*-
2 // posix-threads.h - Defines for using POSIX threads.
4 /* Copyright (C) 1998, 1999, 2001, 2003 Free Software Foundation
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
10 details. */
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).
20 #include <pthread.h>
21 #include <sched.h>
24 // Typedefs.
27 typedef struct _Jv_Thread_t
29 // Flag values are defined in implementation.
30 int flags;
32 // Actual thread id.
33 pthread_t thread;
35 // Java Thread object.
36 java::lang::Thread *thread_obj;
38 // Condition variable and corresponding mutex, used to implement the
39 // interruptable wait/notify mechanism.
40 pthread_cond_t wait_cond;
41 pthread_mutex_t wait_mutex;
43 // Next thread for Condition Variable wait-list chain.
44 _Jv_Thread_t *next;
46 } _Jv_Thread_t;
48 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
51 // Condition Variables used to implement wait/notify/sleep/interrupt.
52 typedef struct
54 // Linked list of Threads that are waiting to be notified.
55 _Jv_Thread_t *first;
57 } _Jv_ConditionVariable_t;
59 typedef struct
61 // For compatibility, simplicity, and correctness, we do not use the native
62 // pthreads recursive mutex implementation, but simulate them instead.
64 // Mutex the thread holds the entire time this mutex is held.
65 pthread_mutex_t mutex;
67 // Thread holding this mutex.
68 pthread_t owner;
70 // Number of times mutex is held (lock depth). If 0, the lock is not held.
71 int count;
72 } _Jv_Mutex_t;
74 // This is a convenience function used only by the pthreads thread
75 // implementation. This is slow, but that's too bad -- we need to do
76 // the checks for correctness. It might be nice to be able to compile
77 // this out. Returns 0 if the lock is held by the current thread, and
78 // 1 otherwise.
79 inline int
80 _Jv_MutexCheckMonitor (_Jv_Mutex_t *mu)
82 return (mu->owner != pthread_self());
86 // Condition variables.
89 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
90 jlong millis, jint nanos);
92 int _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu);
94 int _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu);
96 inline void
97 _Jv_CondInit (_Jv_ConditionVariable_t *cv)
99 cv->first = 0;
103 // Mutexes.
106 #ifdef LOCK_DEBUG
107 # include <stdio.h>
108 #endif
110 inline void
111 _Jv_MutexInit (_Jv_Mutex_t *mu)
113 # ifdef LOCK_DEBUG /* Assumes Linuxthreads */
114 pthread_mutexattr_t attr;
115 pthread_mutexattr_init(&attr);
116 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
117 pthread_mutex_init (&mu->mutex, &attr);
118 # else
119 pthread_mutex_init (&mu->mutex, 0);
120 # endif
122 mu->count = 0;
123 mu->owner = 0;
126 inline int
127 _Jv_MutexLock (_Jv_Mutex_t *mu)
129 pthread_t self = pthread_self ();
130 if (mu->owner == self)
132 mu->count++;
134 else
136 # ifdef LOCK_DEBUG
137 int result = pthread_mutex_lock (&mu->mutex);
138 if (0 != result)
140 fprintf(stderr, "Pthread_mutex_lock returned %d\n", result);
141 for (;;) {}
143 # else
144 pthread_mutex_lock (&mu->mutex);
145 # endif
146 mu->count = 1;
147 mu->owner = self;
149 return 0;
152 inline int
153 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
155 if (_Jv_MutexCheckMonitor (mu))
157 # ifdef LOCK_DEBUG
158 fprintf(stderr, "_Jv_MutexUnlock: Not owner\n");
159 for (;;) {}
160 # endif
161 return 1;
164 mu->count--;
166 if (mu->count == 0)
168 mu->owner = 0;
169 # ifdef LOCK_DEBUG
170 int result = pthread_mutex_unlock (&mu->mutex);
171 if (0 != result)
173 fprintf(stderr, "Pthread_mutex_unlock returned %d\n", result);
174 for (;;) {}
176 # else
177 pthread_mutex_unlock (&mu->mutex);
178 # endif
180 return 0;
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 inline void
191 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
193 pthread_mutex_destroy (&mu->mutex);
196 #endif /* LINUX_THREADS */
199 // Thread creation and manipulation.
202 void _Jv_InitThreads (void);
204 _Jv_Thread_t *_Jv_ThreadInitData (java::lang::Thread *thread);
205 void _Jv_ThreadDestroyData (_Jv_Thread_t *data);
207 inline java::lang::Thread *
208 _Jv_ThreadCurrent (void)
210 extern pthread_key_t _Jv_ThreadKey;
211 return (java::lang::Thread *) pthread_getspecific (_Jv_ThreadKey);
214 #ifdef JV_HASH_SYNCHRONIZATION
215 // Should be specialized to just load the "current thread" register
216 // on platforms that support it. Speed is of the essence. The value
217 // of the descriptor is not, so long as there is a one-to-one correspondence
218 // to threads.
221 #ifdef __ia64__
223 typedef size_t _Jv_ThreadId_t;
225 register size_t _Jv_self __asm__("r13");
226 // For linux_threads this is really a pointer to its thread data
227 // structure. We treat it as opaque. That should also work
228 // on other operating systems that follow the ABI standard.
230 // This should become the prototype for machines that maintain a thread
231 // pointer in a register.
232 inline _Jv_ThreadId_t
233 _Jv_ThreadSelf (void)
235 return _Jv_self;
238 #define JV_SELF_DEFINED
240 #endif /* __ia64__ */
242 #ifdef __alpha__
244 typedef void *_Jv_ThreadId_t;
246 inline _Jv_ThreadId_t
247 _Jv_ThreadSelf (void)
249 return __builtin_thread_pointer ();
252 #define JV_SELF_DEFINED
254 #endif /* __alpha__ */
256 #if defined(SLOW_PTHREAD_SELF)
258 #include "sysdep/locks.h"
260 typedef pthread_t _Jv_ThreadId_t;
262 // E.g. on X86 Linux, pthread_self() is too slow for our purpose.
263 // Instead we maintain a cache based on the current sp value.
264 // This is similar to what's done for thread local allocation in the
265 // GC, only far simpler.
266 // This code should probably go away when Linux/X86 starts using a
267 // segment register to hold the thread id.
268 # define LOG_THREAD_SPACING 12
269 // If two thread pointer values are closer than
270 // 1 << LOG_THREAD_SPACING, we assume they belong
271 // to the same thread.
272 # define SELF_CACHE_SIZE 1024
273 # define SC_INDEX(sp) (((unsigned long)(sp) >> 19) & (SELF_CACHE_SIZE-1))
274 // Mapping from sp value to cache index.
275 // Note that this is not in any real sense a hash
276 // function, since we need to be able to clear
277 // all possibly matching slots on thread startup.
278 // Thus all entries that might correspond to
279 // a given thread are intentionally contiguous.
280 // Works well with anything that allocates at least
281 // 512KB stacks.
282 # define SC_CLEAR_MIN (-16) // When starting a new thread, we clear
283 # define SC_CLEAR_MAX 0 // all self cache entries between
284 // SC_INDEX(sp)+SC_CLEAR_MIN and
285 // SC_INDEX(sp)+SC_CLEAR_MAX to ensure
286 // we never see stale values. The
287 // current values assume a downward
288 // growing stack of size <= 7.5 MB.
289 # define BAD_HIGH_SP_VALUE ((size_t)(-1))
291 extern volatile
292 struct self_cache_entry {
293 size_t high_sp_bits; // sp value >> LOG_THREAD_SPACING
294 pthread_t self; // Corresponding thread
295 } _Jv_self_cache[];
297 void _Jv_Self_Cache_Init();
299 _Jv_ThreadId_t
300 _Jv_ThreadSelf_out_of_line(volatile self_cache_entry *sce,
301 size_t high_sp_bits);
303 inline _Jv_ThreadId_t
304 _Jv_ThreadSelf (void)
306 int dummy;
307 size_t sp = (size_t)(&dummy);
308 unsigned h = SC_INDEX(sp);
309 volatile self_cache_entry *sce = _Jv_self_cache + h;
310 pthread_t candidate_self = sce -> self; // Read must precede following one.
311 read_barrier();
312 if (sce -> high_sp_bits == sp >> LOG_THREAD_SPACING)
314 // The sce -> self value we read must be valid. An intervening
315 // cache replacement by another thread would have first replaced
316 // high_sp_bits by something else, and it can't possibly change
317 // back without our intervention.
318 return candidate_self;
320 else
321 return _Jv_ThreadSelf_out_of_line(sce, sp >> LOG_THREAD_SPACING);
324 #define JV_SELF_DEFINED
326 #endif /* SLOW_PTHREAD_SELF */
328 #ifndef JV_SELF_DEFINED /* If all else fails, call pthread_self directly */
330 typedef pthread_t _Jv_ThreadId_t;
332 inline _Jv_ThreadId_t
333 _Jv_ThreadSelf (void)
335 return pthread_self();
338 #endif /* !JV_SELF_DEFINED */
340 #endif /* JV_HASH_SYNCHRONIZATION */
342 inline _Jv_Thread_t *
343 _Jv_ThreadCurrentData (void)
345 extern pthread_key_t _Jv_ThreadDataKey;
346 return (_Jv_Thread_t *) pthread_getspecific (_Jv_ThreadDataKey);
349 inline void
350 _Jv_ThreadYield (void)
352 #ifdef HAVE_SCHED_YIELD
353 sched_yield ();
354 #endif /* HAVE_SCHED_YIELD */
357 void _Jv_ThreadRegister (_Jv_Thread_t *data);
358 void _Jv_ThreadUnRegister ();
360 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
362 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
363 _Jv_ThreadStartFunc *meth);
365 void _Jv_ThreadWait (void);
367 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
369 #endif /* __JV_POSIX_THREADS__ */