OSDN Git Service

improve open-coding of complex divide, use new method in g77
[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 #ifdef HAVE_RECURSIVE_MUTEX
34 typedef pthread_mutex_t _Jv_Mutex_t;
35 #else
36 // Some systems do not have recursive mutexes, so we must simulate
37 // them.  Solaris is one such system.
38 typedef struct
39 {
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.
46   pthread_cond_t cond;
47   // Thread holding this mutex.  If COUNT is 0, no thread is holding.
48   pthread_t thread;
49   // Number of times mutex is held.  If 0, the lock is not held.
50   int count;
51 } _Jv_Mutex_t;
52 #endif /* HAVE_RECURSIVE_MUTEX */
53
54 typedef struct
55 {
56   // Flag values are defined in implementation.
57   int flags;
58
59   // Actual thread id.
60   pthread_t thread;
61
62   // Exception we want to throw when cancelled.
63   void *exception;
64 } _Jv_Thread_t;
65 typedef void _Jv_ThreadStartFunc (java::lang::Thread *);
66
67
68 //
69 // Condition variables.
70 //
71
72 inline void
73 _Jv_CondInit (_Jv_ConditionVariable_t *cv)
74 {
75   pthread_cond_init (cv, 0);
76 }
77
78 #ifndef LINUX_THREADS
79
80 // pthread_cond_destroy does nothing on Linux and it is a win to avoid
81 // defining this macro.
82
83 #define _Jv_HaveCondDestroy
84
85 inline void
86 _Jv_CondDestroy (_Jv_ConditionVariable_t *cv)
87 {
88   pthread_cond_destroy (cv);
89 }
90
91 #endif /* LINUX_THREADS */
92
93 int _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
94                   jlong millis, jint nanos);
95
96 inline int
97 _Jv_CondNotify (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *)
98 {
99   // FIXME: check to see if mutex is held by current thread.
100   return pthread_cond_signal (cv);
101 }
102
103 inline int
104 _Jv_CondNotifyAll (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *)
105 {
106   // FIXME: check to see if mutex is held by current thread.
107   return pthread_cond_broadcast (cv);
108 }
109
110
111 //
112 // Mutexes.
113 //
114
115 #ifdef RECURSIVE_MUTEX_IS_DEFAULT
116 inline void
117 _Jv_MutexInit (_Jv_Mutex_t *mu)
118 {
119   pthread_mutex_init (mu, NULL);
120 }
121 #else
122 void _Jv_MutexInit (_Jv_Mutex_t *mu);
123 #endif
124
125 #ifndef LINUX_THREADS
126
127 // pthread_mutex_destroy does nothing on Linux and it is a win to avoid
128 // defining this macro.
129
130 #define _Jv_HaveMutexDestroy
131
132 #ifdef HAVE_RECURSIVE_MUTEX
133
134 inline void
135 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
136 {
137   pthread_mutex_destroy (mu);
138 }
139
140 #else /* HAVE_RECURSIVE_MUTEX */
141
142 extern void _Jv_MutexDestroy (_Jv_Mutex_t *mu);
143
144 #endif /* HAVE_RECURSIVE_MUTEX */
145 #endif /* LINUX_THREADS */
146
147 #ifdef HAVE_RECURSIVE_MUTEX
148
149 inline int
150 _Jv_MutexLock (_Jv_Mutex_t *mu)
151 {
152   return pthread_mutex_lock (mu);
153 }
154
155 inline int
156 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
157 {
158   return pthread_mutex_unlock (mu);
159 }
160
161 #else /* HAVE_RECURSIVE_MUTEX */
162
163 extern int _Jv_MutexLock (_Jv_Mutex_t *mu);
164 extern int _Jv_MutexUnlock (_Jv_Mutex_t *mu);
165
166 #endif /* HAVE_RECURSIVE_MUTEX */
167
168
169 //
170 // Thread creation and manipulation.
171 //
172
173 void _Jv_InitThreads (void);
174
175 void _Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *thread);
176
177 inline java::lang::Thread *
178 _Jv_ThreadCurrent (void)
179 {
180   extern pthread_key_t _Jv_ThreadKey;
181   return (java::lang::Thread *) pthread_getspecific (_Jv_ThreadKey);
182 }
183
184 inline void
185 _Jv_ThreadYield (void)
186 {
187 #ifdef HAVE_SCHED_YIELD
188   sched_yield ();
189 #endif /* HAVE_SCHED_YIELD */
190 }
191
192 void _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio);
193
194 void _Jv_ThreadCancel (_Jv_Thread_t *data, void *error);
195
196 // Like Cancel, but doesn't run cleanups.
197 inline void
198 _Jv_ThreadDestroy (_Jv_Thread_t *)
199 {
200   JvFail ("_Jv_ThreadDestroy");
201 }
202
203 void _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
204                       _Jv_ThreadStartFunc *meth);
205
206 void _Jv_ThreadWait (void);
207
208 void _Jv_ThreadInterrupt (_Jv_Thread_t *data);
209
210 #endif /* __JV_POSIX_THREADS__ */