* posix-threads.cc (_Jv_CondWait): Fix currentTimeMillis() overflow.
[gcc.git] / libjava / posix-threads.cc
1 // posix-threads.cc - interface between libjava and POSIX threads.
2
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 // TO DO:
12 // * Document signal handling limitations
13
14 #include <config.h>
15
16 // If we're using the Boehm GC, then we need to override some of the
17 // thread primitives. This is fairly gross.
18 #ifdef HAVE_BOEHM_GC
19 extern "C"
20 {
21 #include <boehm-config.h>
22 #include <gc.h>
23 };
24 #endif /* HAVE_BOEHM_GC */
25
26 #include <stdlib.h>
27 #include <time.h>
28 #include <signal.h>
29
30 #include <cni.h>
31 #include <jvm.h>
32 #include <java/lang/Thread.h>
33 #include <java/lang/System.h>
34
35 // This is used to implement thread startup.
36 struct starter
37 {
38 _Jv_ThreadStartFunc *method;
39 java::lang::Thread *object;
40 _Jv_Thread_t *data;
41 };
42
43 // This is the key used to map from the POSIX thread value back to the
44 // Java object representing the thread. The key is global to all
45 // threads, so it is ok to make it a global here.
46 pthread_key_t _Jv_ThreadKey;
47
48 // We keep a count of all non-daemon threads which are running. When
49 // this reaches zero, _Jv_ThreadWait returns.
50 static pthread_mutex_t daemon_mutex;
51 static pthread_cond_t daemon_cond;
52 static int non_daemon_count;
53
54 // The signal to use when interrupting a thread.
55 #ifdef LINUX_THREADS
56 // LinuxThreads usurps both SIGUSR1 and SIGUSR2.
57 # define INTR SIGHUP
58 #else /* LINUX_THREADS */
59 # define INTR SIGUSR2
60 #endif /* LINUX_THREADS */
61
62 //
63 // These are the flags that can appear in _Jv_Thread_t.
64 //
65
66 // Thread started.
67 #define FLAG_START 0x01
68 // Thread is daemon.
69 #define FLAG_DAEMON 0x02
70
71 \f
72
73 int
74 _Jv_CondWait (_Jv_ConditionVariable_t *cv, _Jv_Mutex_t *mu,
75 jlong millis, jint nanos)
76 {
77 int r;
78 pthread_mutex_t *pmu;
79 #ifdef HAVE_RECURSIVE_MUTEX
80 pmu = mu;
81 #else
82 pmu = &mu->mutex2;
83 #endif
84 if (millis == 0 && nanos == 0)
85 r = pthread_cond_wait (cv, pmu);
86 else
87 {
88 struct timespec ts;
89 jlong m = millis + java::lang::System::currentTimeMillis ();
90 ts.tv_sec = m / 1000;
91 ts.tv_nsec = ((m % 1000) * 1000000) + nanos;
92
93 r = pthread_cond_timedwait (cv, pmu, &ts);
94 }
95 return r;
96 }
97
98 #ifndef RECURSIVE_MUTEX_IS_DEFAULT
99
100 void
101 _Jv_MutexInit (_Jv_Mutex_t *mu)
102 {
103 #ifdef HAVE_RECURSIVE_MUTEX
104 pthread_mutexattr_t *val = NULL;
105
106 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE)
107 pthread_mutexattr_t attr;
108
109 // If this is slow, then allocate it statically and only initialize
110 // it once.
111 pthread_mutexattr_init (&attr);
112 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
113 val = &attr;
114 #elif defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
115 pthread_mutexattr_t attr;
116 pthread_mutexattr_init (&attr);
117 pthread_mutexattr_setkind_np (&attr, PTHREAD_MUTEX_RECURSIVE_NP);
118 val = &attr;
119 #endif
120
121 pthread_mutex_init (mu, val);
122
123 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE) || defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
124 pthread_mutexattr_destroy (&attr);
125 #endif
126
127 #else /* HAVE_RECURSIVE_MUTEX */
128
129 // No recursive mutex, so simulate one.
130 pthread_mutex_init (&mu->mutex, NULL);
131 pthread_mutex_init (&mu->mutex2, NULL);
132 pthread_cond_init (&mu->cond, 0);
133 mu->count = 0;
134
135 #endif /* HAVE_RECURSIVE_MUTEX */
136 }
137
138 #endif /* not RECURSIVE_MUTEX_IS_DEFAULT */
139
140 #if ! defined (LINUX_THREADS) && ! defined (HAVE_RECURSIVE_MUTEX)
141
142 void
143 _Jv_MutexDestroy (_Jv_Mutex_t *mu)
144 {
145 pthread_mutex_destroy (&mu->mutex);
146 pthread_mutex_destroy (&mu->mutex2);
147 pthread_cond_destroy (&mu->cond);
148 }
149
150 int
151 _Jv_MutexLock (_Jv_Mutex_t *mu)
152 {
153 if (pthread_mutex_lock (&mu->mutex))
154 return -1;
155 while (1)
156 {
157 if (mu->count == 0)
158 {
159 // Grab the lock.
160 mu->thread = pthread_self ();
161 mu->count = 1;
162 pthread_mutex_lock (&mu->mutex2);
163 break;
164 }
165 else if (pthread_self () == mu->thread)
166 {
167 // Already have the lock.
168 mu->count += 1;
169 break;
170 }
171 else
172 {
173 // Try to acquire the lock.
174 pthread_cond_wait (&mu->cond, &mu->mutex);
175 }
176 }
177 pthread_mutex_unlock (&mu->mutex);
178 return 0;
179 }
180
181 int
182 _Jv_MutexUnlock (_Jv_Mutex_t *mu)
183 {
184 if (pthread_mutex_lock (&mu->mutex))
185 return -1;
186 int r = 0;
187 if (mu->count == 0 || pthread_self () != mu->thread)
188 r = -1;
189 else
190 {
191 mu->count -= 1;
192 if (! mu->count)
193 {
194 pthread_mutex_unlock (&mu->mutex2);
195 pthread_cond_signal (&mu->cond);
196 }
197 }
198 pthread_mutex_unlock (&mu->mutex);
199 return r;
200 }
201
202 #endif /* not LINUX_THREADS and not HAVE_RECURSIVE_MUTEX */
203
204 static void
205 handle_intr (int)
206 {
207 // Do nothing.
208 }
209
210 void
211 _Jv_InitThreads (void)
212 {
213 pthread_key_create (&_Jv_ThreadKey, NULL);
214 pthread_mutex_init (&daemon_mutex, NULL);
215 pthread_cond_init (&daemon_cond, 0);
216 non_daemon_count = 0;
217
218 // Arrange for the interrupt signal to interrupt system calls.
219 struct sigaction act;
220 act.sa_handler = handle_intr;
221 sigemptyset (&act.sa_mask);
222 act.sa_flags = 0;
223 sigaction (INTR, &act, NULL);
224
225 // Arrange for SIGINT to be blocked to all threads. It is only
226 // deliverable to the master thread.
227 sigset_t mask;
228 sigemptyset (&mask);
229 sigaddset (&mask, SIGINT);
230 pthread_sigmask (SIG_BLOCK, &mask, NULL);
231 }
232
233 void
234 _Jv_ThreadInitData (_Jv_Thread_t **data, java::lang::Thread *)
235 {
236 _Jv_Thread_t *info = new _Jv_Thread_t;
237
238 info->flags = 0;
239 info->exception = NULL;
240
241 // FIXME register a finalizer for INFO here.
242 // FIXME also must mark INFO somehow.
243
244 *data = info;
245 }
246
247 void
248 _Jv_ThreadSetPriority (_Jv_Thread_t *data, jint prio)
249 {
250 if (data->flags & FLAG_START)
251 {
252 struct sched_param param;
253
254 param.sched_priority = prio;
255 pthread_setschedparam (data->thread, SCHED_RR, &param);
256 }
257 }
258
259
260 // This is called as a cleanup handler when a thread is exiting. We
261 // use it to throw the requested exception. It's entirely possible
262 // that this approach is doomed to failure, in which case we'll need
263 // to adopt some alternate. For instance, use a signal to implement
264 // _Jv_ThreadCancel.
265 static void
266 throw_cleanup (void *data)
267 {
268 _Jv_Thread_t *td = (_Jv_Thread_t *) data;
269 _Jv_Throw ((java::lang::Throwable *) td->exception);
270 }
271
272 void
273 _Jv_ThreadCancel (_Jv_Thread_t *data, void *error)
274 {
275 data->exception = error;
276 pthread_cancel (data->thread);
277 }
278
279 // This function is called when a thread is started. We don't arrange
280 // to call the `run' method directly, because this function must
281 // return a value.
282 static void *
283 really_start (void *x)
284 {
285 struct starter *info = (struct starter *) x;
286
287 pthread_cleanup_push (throw_cleanup, info->data);
288 pthread_setspecific (_Jv_ThreadKey, info->object);
289 info->method (info->object);
290 pthread_cleanup_pop (0);
291
292 if (! (info->data->flags & FLAG_DAEMON))
293 {
294 pthread_mutex_lock (&daemon_mutex);
295 --non_daemon_count;
296 if (! non_daemon_count)
297 pthread_cond_signal (&daemon_cond);
298 pthread_mutex_unlock (&daemon_mutex);
299 }
300
301 return NULL;
302 }
303
304 void
305 _Jv_ThreadStart (java::lang::Thread *thread, _Jv_Thread_t *data,
306 _Jv_ThreadStartFunc *meth)
307 {
308 struct sched_param param;
309 pthread_attr_t attr;
310 struct starter *info;
311
312 if (data->flags & FLAG_START)
313 return;
314 data->flags |= FLAG_START;
315
316 param.sched_priority = thread->getPriority();
317
318 pthread_attr_init (&attr);
319 pthread_attr_setschedparam (&attr, &param);
320
321 // FIXME: handle marking the info object for GC.
322 info = (struct starter *) _Jv_AllocBytes (sizeof (struct starter));
323 info->method = meth;
324 info->object = thread;
325 info->data = data;
326
327 if (! thread->isDaemon())
328 {
329 pthread_mutex_lock (&daemon_mutex);
330 ++non_daemon_count;
331 pthread_mutex_unlock (&daemon_mutex);
332 }
333 else
334 data->flags |= FLAG_DAEMON;
335 pthread_create (&data->thread, &attr, really_start, (void *) info);
336
337 pthread_attr_destroy (&attr);
338 }
339
340 void
341 _Jv_ThreadWait (void)
342 {
343 // Arrange for SIGINT to be delivered to the master thread.
344 sigset_t mask;
345 sigemptyset (&mask);
346 sigaddset (&mask, SIGINT);
347 pthread_sigmask (SIG_UNBLOCK, &mask, NULL);
348
349 pthread_mutex_lock (&daemon_mutex);
350 if (non_daemon_count)
351 pthread_cond_wait (&daemon_cond, &daemon_mutex);
352 pthread_mutex_unlock (&daemon_mutex);
353 }
354
355 void
356 _Jv_ThreadInterrupt (_Jv_Thread_t *data)
357 {
358 pthread_kill (data->thread, INTR);
359 }