2 * threads.c: Thread handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
13 #include "mono/utils/mono-hash.h"
22 #include <mono/io-layer/wapi.h>
23 #include <mono/io-layer/wapi-private.h>
24 #include <mono/io-layer/timed-thread.h>
25 #include <mono/io-layer/handles-private.h>
26 #include <mono/io-layer/misc-private.h>
27 #include <mono/io-layer/mono-mutex.h>
28 #include <mono/io-layer/thread-private.h>
29 #include <mono/io-layer/mono-spinlock.h>
33 #undef TLS_PTHREAD_MUTEX
35 /* Hash threads with tids. I thought of using TLS for this, but that
36 * would have to set the data in the new thread, which is more hassle
38 static mono_once_t thread_hash_once = MONO_ONCE_INIT;
39 static mono_mutex_t thread_hash_mutex = MONO_MUTEX_INITIALIZER;
40 static GHashTable *thread_hash=NULL;
43 static MonoGHashTable *tls_gc_hash = NULL;
46 static void thread_close_private (gpointer handle);
47 static void thread_own (gpointer handle);
49 struct _WapiHandleOps _wapi_thread_ops = {
50 NULL, /* close_shared */
51 thread_close_private, /* close_private */
57 static mono_once_t thread_ops_once=MONO_ONCE_INIT;
59 static void thread_ops_init (void)
61 _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD,
62 WAPI_HANDLE_CAP_WAIT);
65 static void thread_close_private (gpointer handle)
67 struct _WapiHandlePrivate_thread *thread_handle;
70 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, NULL,
71 (gpointer *)&thread_handle);
73 g_warning (G_GNUC_PRETTY_FUNCTION
74 ": error looking up thread handle %p", handle);
79 g_message(G_GNUC_PRETTY_FUNCTION
80 ": closing thread handle %p with thread %p id %ld",
81 handle, thread_handle->thread,
82 thread_handle->thread->id);
85 if(thread_handle->thread!=NULL) {
86 _wapi_timed_thread_destroy (thread_handle->thread);
90 static void thread_own (gpointer handle)
92 struct _WapiHandle_thread *thread_handle;
93 struct _WapiHandlePrivate_thread *thread_private_handle;
96 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
97 (gpointer *)&thread_handle,
98 (gpointer *)&thread_private_handle);
100 g_warning (G_GNUC_PRETTY_FUNCTION
101 ": error looking up thread handle %p", handle);
105 if(thread_private_handle->joined==FALSE) {
106 _wapi_timed_thread_join (thread_private_handle->thread, NULL,
108 thread_private_handle->joined=TRUE;
112 static void thread_exit(guint32 exitstatus, gpointer handle)
114 struct _WapiHandle_thread *thread_handle;
115 struct _WapiHandlePrivate_thread *thread_private_handle;
118 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
119 (gpointer *)&thread_handle,
120 (gpointer *)&thread_private_handle);
122 g_warning (G_GNUC_PRETTY_FUNCTION
123 ": error looking up thread handle %p", handle);
127 _wapi_handle_lock_handle (handle);
130 g_message (G_GNUC_PRETTY_FUNCTION
131 ": Recording thread handle %p exit status", handle);
134 thread_handle->exitstatus=exitstatus;
135 thread_handle->state=THREAD_STATE_EXITED;
136 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
138 _wapi_handle_unlock_handle (handle);
141 g_message(G_GNUC_PRETTY_FUNCTION
142 ": Recording thread handle %p id %ld status as %d",
143 handle, thread_private_handle->thread->id, exitstatus);
146 /* Remove this thread from the hash */
147 mono_mutex_lock(&thread_hash_mutex);
148 g_hash_table_remove(thread_hash, &thread_private_handle->thread->id);
149 mono_mutex_unlock(&thread_hash_mutex);
151 /* The thread is no longer active, so unref it */
152 _wapi_handle_unref (handle);
155 static void thread_hash_init(void)
157 thread_hash=g_hash_table_new(g_int_hash, g_int_equal);
162 * @security: Ignored for now.
163 * @stacksize: the size in bytes of the new thread's stack. Use 0 to
164 * default to the normal stack size. (Ignored for now).
165 * @start: The function that the new thread should start with
166 * @param: The parameter to give to @start.
167 * @create: If 0, the new thread is ready to run immediately. If
168 * %CREATE_SUSPENDED, the new thread will be in the suspended state,
169 * requiring a ResumeThread() call to continue running.
170 * @tid: If non-NULL, the ID of the new thread is stored here.
172 * Creates a new threading handle.
174 * Return value: a new handle, or NULL
176 gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize G_GNUC_UNUSED,
177 WapiThreadStart start, gpointer param, guint32 create G_GNUC_UNUSED,
180 struct _WapiHandle_thread *thread_handle;
181 struct _WapiHandlePrivate_thread *thread_private_handle;
186 mono_once(&thread_hash_once, thread_hash_init);
187 mono_once (&thread_ops_once, thread_ops_init);
193 handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
194 if(handle==_WAPI_HANDLE_INVALID) {
195 g_warning (G_GNUC_PRETTY_FUNCTION
196 ": error creating thread handle");
200 _wapi_handle_lock_handle (handle);
202 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
203 (gpointer *)&thread_handle,
204 (gpointer *)&thread_private_handle);
206 g_warning (G_GNUC_PRETTY_FUNCTION
207 ": error looking up thread handle %p", handle);
208 _wapi_handle_unlock_handle (handle);
212 /* Hold a reference while the thread is active, because we use
213 * the handle to store thread exit information
215 _wapi_handle_ref (handle);
217 thread_handle->state=THREAD_STATE_START;
219 /* Lock around the thread create, so that the new thread cant
220 * race us to look up the thread handle in GetCurrentThread()
222 mono_mutex_lock(&thread_hash_mutex);
224 ret=_wapi_timed_thread_create(&thread_private_handle->thread, NULL,
225 start, thread_exit, param, handle);
228 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
231 mono_mutex_unlock(&thread_hash_mutex);
232 _wapi_handle_unlock_handle (handle);
233 _wapi_handle_unref (handle);
235 /* And again, because of the reference we took above */
236 _wapi_handle_unref (handle);
240 g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
242 mono_mutex_unlock(&thread_hash_mutex);
245 g_message(G_GNUC_PRETTY_FUNCTION
246 ": Started thread handle %p thread %p ID %ld", handle,
247 thread_private_handle->thread,
248 thread_private_handle->thread->id);
252 *tid=thread_private_handle->thread->id;
255 _wapi_handle_unlock_handle (handle);
260 gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 tid)
265 g_message (G_GNUC_PRETTY_FUNCTION ": looking up thread %d", tid);
268 mono_mutex_lock(&thread_hash_mutex);
270 ret=g_hash_table_lookup(thread_hash, &tid);
271 mono_mutex_unlock(&thread_hash_mutex);
274 _wapi_handle_ref (ret);
278 g_message (G_GNUC_PRETTY_FUNCTION ": returning thread handle %p", ret);
286 * @exitcode: Sets the thread's exit code, which can be read from
287 * another thread with GetExitCodeThread().
289 * Terminates the calling thread. A thread can also exit by returning
290 * from its start function. When the last thread in a process
291 * terminates, the process itself terminates.
293 void ExitThread(guint32 exitcode)
295 /* No thread created yet. */
296 if (thread_hash == NULL)
299 _wapi_timed_thread_exit(exitcode);
304 * @handle: The thread handle to query
305 * @exitcode: The thread @handle exit code is stored here
307 * Finds the exit code of @handle, and stores it in @exitcode. If the
308 * thread @handle is still running, the value stored is %STILL_ACTIVE.
310 * Return value: %TRUE, or %FALSE on error.
312 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
314 struct _WapiHandle_thread *thread_handle;
315 struct _WapiHandlePrivate_thread *thread_private_handle;
318 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
319 (gpointer *)&thread_handle,
320 (gpointer *)&thread_private_handle);
322 g_warning (G_GNUC_PRETTY_FUNCTION
323 ": error looking up thread handle %p", handle);
328 g_message(G_GNUC_PRETTY_FUNCTION
329 ": Finding exit status for thread handle %p id %ld", handle,
330 thread_private_handle->thread->id);
335 g_message(G_GNUC_PRETTY_FUNCTION
336 ": Nowhere to store exit code");
341 if(thread_handle->state!=THREAD_STATE_EXITED) {
343 g_message(G_GNUC_PRETTY_FUNCTION
344 ": Thread still active (state %d, exited is %d)",
345 thread_handle->state, THREAD_STATE_EXITED);
347 *exitcode=STILL_ACTIVE;
351 *exitcode=thread_handle->exitstatus;
357 * GetCurrentThreadId:
359 * Looks up the thread ID of the current thread. This ID can be
360 * passed to OpenThread() to create a new handle on this thread.
362 * Return value: the thread ID.
364 guint32 GetCurrentThreadId(void)
366 pthread_t tid=pthread_self();
374 * Looks up the handle associated with the current thread. Under
375 * Windows this is a pseudohandle, and must be duplicated with
376 * DuplicateHandle() for some operations.
378 * Return value: The current thread handle, or %NULL on failure.
379 * (Unknown whether Windows has a possible failure here. It may be
380 * necessary to implement the pseudohandle-constant behaviour).
382 gpointer GetCurrentThread(void)
387 tid=GetCurrentThreadId();
389 mono_mutex_lock(&thread_hash_mutex);
391 ret=g_hash_table_lookup(thread_hash, &tid);
393 mono_mutex_unlock(&thread_hash_mutex);
400 * @handle: the thread handle to resume
402 * Decrements the suspend count of thread @handle. A thread can only
403 * run if its suspend count is zero.
405 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
407 guint32 ResumeThread(gpointer handle G_GNUC_UNUSED)
414 * @handle: the thread handle to suspend
416 * Increments the suspend count of thread @handle. A thread can only
417 * run if its suspend count is zero.
419 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
421 guint32 SuspendThread(gpointer handle G_GNUC_UNUSED)
427 * We assume here that TLS_MINIMUM_AVAILABLE is less than
428 * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
431 * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
432 * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
436 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
437 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
438 #ifdef TLS_PTHREAD_MUTEX
439 static mono_mutex_t TLS_mutex=MONO_MUTEX_INITIALIZER;
441 static guint32 TLS_spinlock=0;
447 * Allocates a Thread Local Storage (TLS) index. Any thread in the
448 * same process can use this index to store and retrieve values that
449 * are local to that thread.
451 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
454 guint32 TlsAlloc(void)
458 #ifdef TLS_PTHREAD_MUTEX
459 mono_mutex_lock(&TLS_mutex);
461 MONO_SPIN_LOCK (TLS_spinlock);
464 for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
465 if(TLS_used[i]==FALSE) {
467 pthread_key_create(&TLS_keys[i], NULL);
469 #ifdef TLS_PTHREAD_MUTEX
470 mono_mutex_unlock(&TLS_mutex);
472 MONO_SPIN_UNLOCK (TLS_spinlock);
476 g_message (G_GNUC_PRETTY_FUNCTION ": returning key %d",
484 #ifdef TLS_PTHREAD_MUTEX
485 mono_mutex_unlock(&TLS_mutex);
487 MONO_SPIN_UNLOCK (TLS_spinlock);
491 g_message (G_GNUC_PRETTY_FUNCTION ": out of indices");
495 return(TLS_OUT_OF_INDEXES);
498 #define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
502 * @idx: The TLS index to free
504 * Releases a TLS index, making it available for reuse. This call
505 * will delete any TLS data stored under index @idx in all threads.
507 * Return value: %TRUE on success, %FALSE otherwise.
509 gboolean TlsFree(guint32 idx)
512 g_message (G_GNUC_PRETTY_FUNCTION ": freeing key %d", idx);
515 #ifdef TLS_PTHREAD_MUTEX
516 mono_mutex_lock(&TLS_mutex);
518 MONO_SPIN_LOCK (TLS_spinlock);
521 if(TLS_used[idx]==FALSE) {
522 #ifdef TLS_PTHREAD_MUTEX
523 mono_mutex_unlock(&TLS_mutex);
525 MONO_SPIN_UNLOCK (TLS_spinlock);
531 pthread_key_delete(TLS_keys[idx]);
534 mono_g_hash_table_remove (tls_gc_hash, MAKE_GC_ID (idx));
537 #ifdef TLS_PTHREAD_MUTEX
538 mono_mutex_unlock(&TLS_mutex);
540 MONO_SPIN_UNLOCK (TLS_spinlock);
548 * @idx: The TLS index to retrieve
550 * Retrieves the TLS data stored under index @idx.
552 * Return value: The value stored in the TLS index @idx in the current
553 * thread, or %NULL on error. As %NULL can be a valid return value,
554 * in this case GetLastError() returns %ERROR_SUCCESS.
556 gpointer TlsGetValue(guint32 idx)
561 g_message (G_GNUC_PRETTY_FUNCTION ": looking up key %d", idx);
564 #ifdef TLS_PTHREAD_MUTEX
565 mono_mutex_lock(&TLS_mutex);
567 MONO_SPIN_LOCK (TLS_spinlock);
570 if(TLS_used[idx]==FALSE) {
572 g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
575 #ifdef TLS_PTHREAD_MUTEX
576 mono_mutex_unlock(&TLS_mutex);
578 MONO_SPIN_UNLOCK (TLS_spinlock);
583 ret=pthread_getspecific(TLS_keys[idx]);
586 g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", ret);
589 #ifdef TLS_PTHREAD_MUTEX
590 mono_mutex_unlock(&TLS_mutex);
592 MONO_SPIN_UNLOCK (TLS_spinlock);
600 * @idx: The TLS index to store
601 * @value: The value to store under index @idx
603 * Stores @value at TLS index @idx.
605 * Return value: %TRUE on success, %FALSE otherwise.
607 gboolean TlsSetValue(guint32 idx, gpointer value)
612 g_message (G_GNUC_PRETTY_FUNCTION ": setting key %d to %p", idx,
616 #ifdef TLS_PTHREAD_MUTEX
617 mono_mutex_lock(&TLS_mutex);
619 MONO_SPIN_LOCK (TLS_spinlock);
622 if(TLS_used[idx]==FALSE) {
624 g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
627 #ifdef TLS_PTHREAD_MUTEX
628 mono_mutex_unlock(&TLS_mutex);
630 MONO_SPIN_UNLOCK (TLS_spinlock);
635 ret=pthread_setspecific(TLS_keys[idx], value);
638 g_message (G_GNUC_PRETTY_FUNCTION
639 ": pthread_setspecific error: %s", strerror (ret));
642 #ifdef TLS_PTHREAD_MUTEX
643 mono_mutex_unlock(&TLS_mutex);
645 MONO_SPIN_UNLOCK (TLS_spinlock);
652 tls_gc_hash = mono_g_hash_table_new(g_direct_hash, g_direct_equal);
653 mono_g_hash_table_insert (tls_gc_hash, MAKE_GC_ID (idx), value);
656 #ifdef TLS_PTHREAD_MUTEX
657 mono_mutex_unlock(&TLS_mutex);
659 MONO_SPIN_UNLOCK (TLS_spinlock);
667 * @ms: The time in milliseconds to suspend for
669 * Suspends execution of the current thread for @ms milliseconds. A
670 * value of zero causes the thread to relinquish its time slice. A
671 * value of %INFINITE causes an infinite delay.
673 void Sleep(guint32 ms)
675 struct timespec req, rem;
680 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
688 /* FIXME: check for INFINITE and sleep forever */
689 divvy=div((int)ms, 1000);
691 req.tv_sec=divvy.quot;
692 req.tv_nsec=divvy.rem*1000000;
695 ret=nanosleep(&req, &rem);
697 /* Sleep interrupted with rem time remaining */
699 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
701 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
709 /* FIXME: implement alertable */
710 void SleepEx(guint32 ms, gboolean alertable)
712 if(alertable==TRUE) {
713 g_warning(G_GNUC_PRETTY_FUNCTION ": alertable not implemented");