1 // posix-threads.cc - interface between libjava and POSIX threads.
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 // * Document signal handling limitations
16 // If we're using the Boehm GC, then we need to override some of the
17 // thread primitives. This is fairly gross.
23 #endif /* HAVE_BOEHM_GC */
32 #include <java/lang/Thread.h>
33 #include <java/lang/System.h>
35 // This is used to implement thread startup.
38 _Jv_ThreadStartFunc
*method
;
39 java::lang::Thread
*object
;
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
;
48 // This is the key used to map from the POSIX thread value back to the
49 // _Jv_Thread_t* representing the thread.
50 pthread_key_t _Jv_ThreadDataKey
;
52 // We keep a count of all non-daemon threads which are running. When
53 // this reaches zero, _Jv_ThreadWait returns.
54 static pthread_mutex_t daemon_mutex
;
55 static pthread_cond_t daemon_cond
;
56 static int non_daemon_count
;
58 // The signal to use when interrupting a thread.
60 // LinuxThreads usurps both SIGUSR1 and SIGUSR2.
62 #else /* LINUX_THREADS */
64 #endif /* LINUX_THREADS */
67 // These are the flags that can appear in _Jv_Thread_t.
71 #define FLAG_START 0x01
73 #define FLAG_DAEMON 0x02
74 // Thread was interrupted by _Jv_ThreadInterrupt.
75 #define FLAG_INTERRUPTED 0x04
80 _Jv_CondWait (_Jv_ConditionVariable_t
*cv
, _Jv_Mutex_t
*mu
,
81 jlong millis
, jint nanos
)
83 if (_Jv_PthreadCheckMonitor (mu
))
87 pthread_mutex_t
*pmu
= _Jv_PthreadGetMutex (mu
);
89 jlong m
, m2
, startTime
;
90 bool done_sleeping
= false;
92 if (millis
== 0 && nanos
== 0)
93 r
= pthread_cond_wait (cv
, pmu
);
96 startTime
= java::lang::System::currentTimeMillis();
97 m
= millis
+ startTime
;
101 ts
.tv_sec
= m
/ 1000;
102 ts
.tv_nsec
= ((m
% 1000) * 1000000) + nanos
;
104 r
= pthread_cond_timedwait (cv
, pmu
, &ts
);
108 /* We were interrupted by a signal. Either this is
109 because we were interrupted intentionally (i.e. by
110 Thread.interrupt()) or by the GC if it is
112 _Jv_Thread_t
*current
= _Jv_ThreadCurrentData();
113 if (current
->flags
& FLAG_INTERRUPTED
)
115 current
->flags
&= ~(FLAG_INTERRUPTED
);
116 done_sleeping
= true;
120 /* We were woken up by the GC or another signal. */
121 m2
= java::lang::System::currentTimeMillis ();
125 done_sleeping
= true;
129 else if (r
== ETIMEDOUT
)
131 /* A timeout is a normal result. */
133 done_sleeping
= true;
136 done_sleeping
= true;
138 while (! done_sleeping
);
144 #ifndef RECURSIVE_MUTEX_IS_DEFAULT
147 _Jv_MutexInit (_Jv_Mutex_t
*mu
)
149 #ifdef HAVE_RECURSIVE_MUTEX
150 pthread_mutexattr_t
*val
= NULL
;
152 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE)
153 pthread_mutexattr_t attr
;
155 // If this is slow, then allocate it statically and only initialize
157 pthread_mutexattr_init (&attr
);
158 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
160 #elif defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
161 pthread_mutexattr_t attr
;
162 pthread_mutexattr_init (&attr
);
163 pthread_mutexattr_setkind_np (&attr
, PTHREAD_MUTEX_RECURSIVE_NP
);
167 pthread_mutex_init (mu
, val
);
169 #if defined (HAVE_PTHREAD_MUTEXATTR_SETTYPE) || defined (HAVE_PTHREAD_MUTEXATTR_SETKIND_NP)
170 pthread_mutexattr_destroy (&attr
);
173 #else /* HAVE_RECURSIVE_MUTEX */
175 // No recursive mutex, so simulate one.
176 pthread_mutex_init (&mu
->mutex
, NULL
);
177 pthread_mutex_init (&mu
->mutex2
, NULL
);
178 pthread_cond_init (&mu
->cond
, 0);
181 #endif /* HAVE_RECURSIVE_MUTEX */
184 #endif /* not RECURSIVE_MUTEX_IS_DEFAULT */
186 #if ! defined (LINUX_THREADS) && ! defined (HAVE_RECURSIVE_MUTEX)
189 _Jv_MutexDestroy (_Jv_Mutex_t
*mu
)
191 pthread_mutex_destroy (&mu
->mutex
);
192 pthread_mutex_destroy (&mu
->mutex2
);
193 pthread_cond_destroy (&mu
->cond
);
197 _Jv_MutexLock (_Jv_Mutex_t
*mu
)
199 if (pthread_mutex_lock (&mu
->mutex
))
206 mu
->thread
= pthread_self ();
208 pthread_mutex_lock (&mu
->mutex2
);
211 else if (pthread_self () == mu
->thread
)
213 // Already have the lock.
219 // Try to acquire the lock.
220 pthread_cond_wait (&mu
->cond
, &mu
->mutex
);
223 pthread_mutex_unlock (&mu
->mutex
);
228 _Jv_MutexUnlock (_Jv_Mutex_t
*mu
)
230 if (pthread_mutex_lock (&mu
->mutex
))
233 if (mu
->count
== 0 || pthread_self () != mu
->thread
)
240 pthread_mutex_unlock (&mu
->mutex2
);
241 pthread_cond_signal (&mu
->cond
);
244 pthread_mutex_unlock (&mu
->mutex
);
248 #endif /* not LINUX_THREADS and not HAVE_RECURSIVE_MUTEX */
257 _Jv_InitThreads (void)
259 pthread_key_create (&_Jv_ThreadKey
, NULL
);
260 pthread_key_create (&_Jv_ThreadDataKey
, NULL
);
261 pthread_mutex_init (&daemon_mutex
, NULL
);
262 pthread_cond_init (&daemon_cond
, 0);
263 non_daemon_count
= 0;
265 // Arrange for the interrupt signal to interrupt system calls.
266 struct sigaction act
;
267 act
.sa_handler
= handle_intr
;
268 sigemptyset (&act
.sa_mask
);
270 sigaction (INTR
, &act
, NULL
);
272 // Arrange for SIGINT to be blocked to all threads. It is only
273 // deliverable to the master thread.
276 sigaddset (&mask
, SIGINT
);
277 pthread_sigmask (SIG_BLOCK
, &mask
, NULL
);
281 _Jv_ThreadInitData (_Jv_Thread_t
**data
, java::lang::Thread
*)
283 _Jv_Thread_t
*info
= new _Jv_Thread_t
;
286 info
->exception
= NULL
;
288 // FIXME register a finalizer for INFO here.
289 // FIXME also must mark INFO somehow.
295 _Jv_ThreadSetPriority (_Jv_Thread_t
*data
, jint prio
)
297 if (data
->flags
& FLAG_START
)
299 struct sched_param param
;
301 param
.sched_priority
= prio
;
302 pthread_setschedparam (data
->thread
, SCHED_RR
, ¶m
);
307 // This is called as a cleanup handler when a thread is exiting. We
308 // use it to throw the requested exception. It's entirely possible
309 // that this approach is doomed to failure, in which case we'll need
310 // to adopt some alternate. For instance, use a signal to implement
313 throw_cleanup (void *data
)
315 _Jv_Thread_t
*td
= (_Jv_Thread_t
*) data
;
316 _Jv_Throw ((java::lang::Throwable
*) td
->exception
);
320 _Jv_ThreadCancel (_Jv_Thread_t
*data
, void *error
)
322 data
->exception
= error
;
323 pthread_cancel (data
->thread
);
326 // This function is called when a thread is started. We don't arrange
327 // to call the `run' method directly, because this function must
330 really_start (void *x
)
332 struct starter
*info
= (struct starter
*) x
;
334 pthread_cleanup_push (throw_cleanup
, info
->data
);
335 pthread_setspecific (_Jv_ThreadKey
, info
->object
);
336 pthread_setspecific (_Jv_ThreadDataKey
, info
->data
);
337 info
->method (info
->object
);
338 pthread_cleanup_pop (0);
340 if (! (info
->data
->flags
& FLAG_DAEMON
))
342 pthread_mutex_lock (&daemon_mutex
);
344 if (! non_daemon_count
)
345 pthread_cond_signal (&daemon_cond
);
346 pthread_mutex_unlock (&daemon_mutex
);
353 _Jv_ThreadStart (java::lang::Thread
*thread
, _Jv_Thread_t
*data
,
354 _Jv_ThreadStartFunc
*meth
)
356 struct sched_param param
;
358 struct starter
*info
;
360 if (data
->flags
& FLAG_START
)
362 data
->flags
|= FLAG_START
;
364 param
.sched_priority
= thread
->getPriority();
366 pthread_attr_init (&attr
);
367 pthread_attr_setschedparam (&attr
, ¶m
);
369 // FIXME: handle marking the info object for GC.
370 info
= (struct starter
*) _Jv_AllocBytes (sizeof (struct starter
));
372 info
->object
= thread
;
375 if (! thread
->isDaemon())
377 pthread_mutex_lock (&daemon_mutex
);
379 pthread_mutex_unlock (&daemon_mutex
);
382 data
->flags
|= FLAG_DAEMON
;
383 pthread_create (&data
->thread
, &attr
, really_start
, (void *) info
);
385 pthread_attr_destroy (&attr
);
389 _Jv_ThreadWait (void)
391 // Arrange for SIGINT to be delivered to the master thread.
394 sigaddset (&mask
, SIGINT
);
395 pthread_sigmask (SIG_UNBLOCK
, &mask
, NULL
);
397 pthread_mutex_lock (&daemon_mutex
);
398 if (non_daemon_count
)
399 pthread_cond_wait (&daemon_cond
, &daemon_mutex
);
400 pthread_mutex_unlock (&daemon_mutex
);
404 _Jv_ThreadInterrupt (_Jv_Thread_t
*data
)
406 data
->flags
|= FLAG_INTERRUPTED
;
407 pthread_kill (data
->thread
, INTR
);