2 * threads.c: Thread handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
12 #include <mono/os/gc_wrapper.h>
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
36 /* Hash threads with tids. I thought of using TLS for this, but that
37 * would have to set the data in the new thread, which is more hassle
39 static mono_once_t thread_hash_once = MONO_ONCE_INIT;
40 static mono_mutex_t thread_hash_mutex = MONO_MUTEX_INITIALIZER;
41 static GHashTable *thread_hash=NULL;
44 static MonoGHashTable *tls_gc_hash = NULL;
47 static void thread_close_private (gpointer handle);
48 static void thread_own (gpointer handle);
50 struct _WapiHandleOps _wapi_thread_ops = {
51 NULL, /* close_shared */
52 thread_close_private, /* close_private */
58 static mono_once_t thread_ops_once=MONO_ONCE_INIT;
60 static void thread_ops_init (void)
62 _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD,
63 WAPI_HANDLE_CAP_WAIT);
66 static void thread_close_private (gpointer handle)
68 struct _WapiHandlePrivate_thread *thread_handle;
71 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, NULL,
72 (gpointer *)&thread_handle);
74 g_warning (G_GNUC_PRETTY_FUNCTION
75 ": error looking up thread handle %p", handle);
80 g_message(G_GNUC_PRETTY_FUNCTION
81 ": closing thread handle %p with thread %p id %ld",
82 handle, thread_handle->thread,
83 thread_handle->thread->id);
86 if(thread_handle->thread!=NULL) {
87 _wapi_timed_thread_destroy (thread_handle->thread);
91 static void thread_own (gpointer handle)
93 struct _WapiHandle_thread *thread_handle;
94 struct _WapiHandlePrivate_thread *thread_private_handle;
97 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
98 (gpointer *)&thread_handle,
99 (gpointer *)&thread_private_handle);
101 g_warning (G_GNUC_PRETTY_FUNCTION
102 ": error looking up thread handle %p", handle);
106 if(thread_private_handle->joined==FALSE) {
107 _wapi_timed_thread_join (thread_private_handle->thread, NULL,
109 thread_private_handle->joined=TRUE;
113 static void thread_exit(guint32 exitstatus, gpointer handle)
115 struct _WapiHandle_thread *thread_handle;
116 struct _WapiHandlePrivate_thread *thread_private_handle;
119 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
120 (gpointer *)&thread_handle,
121 (gpointer *)&thread_private_handle);
123 g_warning (G_GNUC_PRETTY_FUNCTION
124 ": error looking up thread handle %p", handle);
128 _wapi_handle_lock_handle (handle);
131 g_message (G_GNUC_PRETTY_FUNCTION
132 ": Recording thread handle %p exit status", handle);
135 thread_handle->exitstatus=exitstatus;
136 thread_handle->state=THREAD_STATE_EXITED;
137 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
139 _wapi_handle_unlock_handle (handle);
142 g_message(G_GNUC_PRETTY_FUNCTION
143 ": Recording thread handle %p id %ld status as %d",
144 handle, thread_private_handle->thread->id, exitstatus);
147 /* Remove this thread from the hash */
148 mono_mutex_lock(&thread_hash_mutex);
149 g_hash_table_remove(thread_hash, &thread_private_handle->thread->id);
150 mono_mutex_unlock(&thread_hash_mutex);
152 /* The thread is no longer active, so unref it */
153 _wapi_handle_unref (handle);
156 static void thread_hash_init(void)
158 thread_hash=g_hash_table_new(g_int_hash, g_int_equal);
163 * @security: Ignored for now.
164 * @stacksize: the size in bytes of the new thread's stack. Use 0 to
165 * default to the normal stack size. (Ignored for now).
166 * @start: The function that the new thread should start with
167 * @param: The parameter to give to @start.
168 * @create: If 0, the new thread is ready to run immediately. If
169 * %CREATE_SUSPENDED, the new thread will be in the suspended state,
170 * requiring a ResumeThread() call to continue running.
171 * @tid: If non-NULL, the ID of the new thread is stored here.
173 * Creates a new threading handle.
175 * Return value: a new handle, or NULL
177 gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize G_GNUC_UNUSED,
178 WapiThreadStart start, gpointer param, guint32 create,
181 struct _WapiHandle_thread *thread_handle;
182 struct _WapiHandlePrivate_thread *thread_private_handle;
187 mono_once(&thread_hash_once, thread_hash_init);
188 mono_once (&thread_ops_once, thread_ops_init);
194 handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
195 if(handle==_WAPI_HANDLE_INVALID) {
196 g_warning (G_GNUC_PRETTY_FUNCTION
197 ": error creating thread handle");
201 _wapi_handle_lock_handle (handle);
203 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
204 (gpointer *)&thread_handle,
205 (gpointer *)&thread_private_handle);
207 g_warning (G_GNUC_PRETTY_FUNCTION
208 ": error looking up thread handle %p", handle);
209 _wapi_handle_unlock_handle (handle);
213 /* Hold a reference while the thread is active, because we use
214 * the handle to store thread exit information
216 _wapi_handle_ref (handle);
218 thread_handle->state=THREAD_STATE_START;
220 /* Lock around the thread create, so that the new thread cant
221 * race us to look up the thread handle in GetCurrentThread()
223 mono_mutex_lock(&thread_hash_mutex);
225 ret=_wapi_timed_thread_create(&thread_private_handle->thread, NULL,
226 create, start, thread_exit, param,
230 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
233 mono_mutex_unlock(&thread_hash_mutex);
234 _wapi_handle_unlock_handle (handle);
235 _wapi_handle_unref (handle);
237 /* And again, because of the reference we took above */
238 _wapi_handle_unref (handle);
242 g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
244 mono_mutex_unlock(&thread_hash_mutex);
247 g_message(G_GNUC_PRETTY_FUNCTION
248 ": Started thread handle %p thread %p ID %ld", handle,
249 thread_private_handle->thread,
250 thread_private_handle->thread->id);
254 #ifdef PTHREAD_POINTER_ID
255 *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
257 *tid=thread_private_handle->thread->id;
261 _wapi_handle_unlock_handle (handle);
266 gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 tid)
271 g_message (G_GNUC_PRETTY_FUNCTION ": looking up thread %d", tid);
274 mono_mutex_lock(&thread_hash_mutex);
276 ret=g_hash_table_lookup(thread_hash, &tid);
277 mono_mutex_unlock(&thread_hash_mutex);
280 _wapi_handle_ref (ret);
284 g_message (G_GNUC_PRETTY_FUNCTION ": returning thread handle %p", ret);
292 * @exitcode: Sets the thread's exit code, which can be read from
293 * another thread with GetExitCodeThread().
295 * Terminates the calling thread. A thread can also exit by returning
296 * from its start function. When the last thread in a process
297 * terminates, the process itself terminates.
299 void ExitThread(guint32 exitcode)
301 /* No thread created yet. */
302 if (thread_hash == NULL)
305 _wapi_timed_thread_exit(exitcode);
310 * @handle: The thread handle to query
311 * @exitcode: The thread @handle exit code is stored here
313 * Finds the exit code of @handle, and stores it in @exitcode. If the
314 * thread @handle is still running, the value stored is %STILL_ACTIVE.
316 * Return value: %TRUE, or %FALSE on error.
318 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
320 struct _WapiHandle_thread *thread_handle;
321 struct _WapiHandlePrivate_thread *thread_private_handle;
324 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
325 (gpointer *)&thread_handle,
326 (gpointer *)&thread_private_handle);
328 g_warning (G_GNUC_PRETTY_FUNCTION
329 ": error looking up thread handle %p", handle);
334 g_message(G_GNUC_PRETTY_FUNCTION
335 ": Finding exit status for thread handle %p id %ld", handle,
336 thread_private_handle->thread->id);
341 g_message(G_GNUC_PRETTY_FUNCTION
342 ": Nowhere to store exit code");
347 if(thread_handle->state!=THREAD_STATE_EXITED) {
349 g_message(G_GNUC_PRETTY_FUNCTION
350 ": Thread still active (state %d, exited is %d)",
351 thread_handle->state, THREAD_STATE_EXITED);
353 *exitcode=STILL_ACTIVE;
357 *exitcode=thread_handle->exitstatus;
363 * GetCurrentThreadId:
365 * Looks up the thread ID of the current thread. This ID can be
366 * passed to OpenThread() to create a new handle on this thread.
368 * Return value: the thread ID.
370 guint32 GetCurrentThreadId(void)
372 pthread_t tid=pthread_self();
374 #ifdef PTHREAD_POINTER_ID
375 return(GPOINTER_TO_UINT(tid));
384 * Looks up the handle associated with the current thread. Under
385 * Windows this is a pseudohandle, and must be duplicated with
386 * DuplicateHandle() for some operations.
388 * Return value: The current thread handle, or %NULL on failure.
389 * (Unknown whether Windows has a possible failure here. It may be
390 * necessary to implement the pseudohandle-constant behaviour).
392 gpointer GetCurrentThread(void)
397 tid=GetCurrentThreadId();
399 mono_mutex_lock(&thread_hash_mutex);
401 ret=g_hash_table_lookup(thread_hash, &tid);
403 mono_mutex_unlock(&thread_hash_mutex);
410 * @handle: the thread handle to resume
412 * Decrements the suspend count of thread @handle. A thread can only
413 * run if its suspend count is zero.
415 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
417 guint32 ResumeThread(gpointer handle)
419 struct _WapiHandle_thread *thread_handle;
420 struct _WapiHandlePrivate_thread *thread_private_handle;
423 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
424 (gpointer *)&thread_handle,
425 (gpointer *)&thread_private_handle);
427 g_warning (G_GNUC_PRETTY_FUNCTION
428 ": error looking up thread handle %p", handle);
432 /* This is still a kludge that only copes with starting a
433 * thread that was suspended on create, so don't bother with
434 * the suspend count crap yet
436 _wapi_timed_thread_resume (thread_private_handle->thread);
443 * @handle: the thread handle to suspend
445 * Increments the suspend count of thread @handle. A thread can only
446 * run if its suspend count is zero.
448 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
450 guint32 SuspendThread(gpointer handle G_GNUC_UNUSED)
456 * We assume here that TLS_MINIMUM_AVAILABLE is less than
457 * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
460 * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
461 * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
465 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
466 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
467 #ifdef TLS_PTHREAD_MUTEX
468 static mono_mutex_t TLS_mutex=MONO_MUTEX_INITIALIZER;
470 static guint32 TLS_spinlock=0;
476 * Allocates a Thread Local Storage (TLS) index. Any thread in the
477 * same process can use this index to store and retrieve values that
478 * are local to that thread.
480 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
483 guint32 TlsAlloc(void)
487 #ifdef TLS_PTHREAD_MUTEX
488 mono_mutex_lock(&TLS_mutex);
490 MONO_SPIN_LOCK (TLS_spinlock);
493 for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
494 if(TLS_used[i]==FALSE) {
496 pthread_key_create(&TLS_keys[i], NULL);
498 #ifdef TLS_PTHREAD_MUTEX
499 mono_mutex_unlock(&TLS_mutex);
501 MONO_SPIN_UNLOCK (TLS_spinlock);
505 g_message (G_GNUC_PRETTY_FUNCTION ": returning key %d",
513 #ifdef TLS_PTHREAD_MUTEX
514 mono_mutex_unlock(&TLS_mutex);
516 MONO_SPIN_UNLOCK (TLS_spinlock);
520 g_message (G_GNUC_PRETTY_FUNCTION ": out of indices");
524 return(TLS_OUT_OF_INDEXES);
527 #define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
531 * @idx: The TLS index to free
533 * Releases a TLS index, making it available for reuse. This call
534 * will delete any TLS data stored under index @idx in all threads.
536 * Return value: %TRUE on success, %FALSE otherwise.
538 gboolean TlsFree(guint32 idx)
541 g_message (G_GNUC_PRETTY_FUNCTION ": freeing key %d", idx);
544 #ifdef TLS_PTHREAD_MUTEX
545 mono_mutex_lock(&TLS_mutex);
547 MONO_SPIN_LOCK (TLS_spinlock);
550 if(TLS_used[idx]==FALSE) {
551 #ifdef TLS_PTHREAD_MUTEX
552 mono_mutex_unlock(&TLS_mutex);
554 MONO_SPIN_UNLOCK (TLS_spinlock);
560 pthread_key_delete(TLS_keys[idx]);
563 mono_g_hash_table_remove (tls_gc_hash, MAKE_GC_ID (idx));
566 #ifdef TLS_PTHREAD_MUTEX
567 mono_mutex_unlock(&TLS_mutex);
569 MONO_SPIN_UNLOCK (TLS_spinlock);
577 * @idx: The TLS index to retrieve
579 * Retrieves the TLS data stored under index @idx.
581 * Return value: The value stored in the TLS index @idx in the current
582 * thread, or %NULL on error. As %NULL can be a valid return value,
583 * in this case GetLastError() returns %ERROR_SUCCESS.
585 gpointer TlsGetValue(guint32 idx)
590 g_message (G_GNUC_PRETTY_FUNCTION ": looking up key %d", idx);
593 #ifdef TLS_PTHREAD_MUTEX
594 mono_mutex_lock(&TLS_mutex);
596 MONO_SPIN_LOCK (TLS_spinlock);
599 if(TLS_used[idx]==FALSE) {
601 g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
604 #ifdef TLS_PTHREAD_MUTEX
605 mono_mutex_unlock(&TLS_mutex);
607 MONO_SPIN_UNLOCK (TLS_spinlock);
612 ret=pthread_getspecific(TLS_keys[idx]);
615 g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", ret);
618 #ifdef TLS_PTHREAD_MUTEX
619 mono_mutex_unlock(&TLS_mutex);
621 MONO_SPIN_UNLOCK (TLS_spinlock);
629 * @idx: The TLS index to store
630 * @value: The value to store under index @idx
632 * Stores @value at TLS index @idx.
634 * Return value: %TRUE on success, %FALSE otherwise.
636 gboolean TlsSetValue(guint32 idx, gpointer value)
641 g_message (G_GNUC_PRETTY_FUNCTION ": setting key %d to %p", idx,
645 #ifdef TLS_PTHREAD_MUTEX
646 mono_mutex_lock(&TLS_mutex);
648 MONO_SPIN_LOCK (TLS_spinlock);
651 if(TLS_used[idx]==FALSE) {
653 g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
656 #ifdef TLS_PTHREAD_MUTEX
657 mono_mutex_unlock(&TLS_mutex);
659 MONO_SPIN_UNLOCK (TLS_spinlock);
664 ret=pthread_setspecific(TLS_keys[idx], value);
667 g_message (G_GNUC_PRETTY_FUNCTION
668 ": pthread_setspecific error: %s", strerror (ret));
671 #ifdef TLS_PTHREAD_MUTEX
672 mono_mutex_unlock(&TLS_mutex);
674 MONO_SPIN_UNLOCK (TLS_spinlock);
681 tls_gc_hash = mono_g_hash_table_new(g_direct_hash, g_direct_equal);
682 mono_g_hash_table_insert (tls_gc_hash, MAKE_GC_ID (idx), value);
685 #ifdef TLS_PTHREAD_MUTEX
686 mono_mutex_unlock(&TLS_mutex);
688 MONO_SPIN_UNLOCK (TLS_spinlock);
696 * @ms: The time in milliseconds to suspend for
698 * Suspends execution of the current thread for @ms milliseconds. A
699 * value of zero causes the thread to relinquish its time slice. A
700 * value of %INFINITE causes an infinite delay.
702 void Sleep(guint32 ms)
704 struct timespec req, rem;
709 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
717 /* FIXME: check for INFINITE and sleep forever */
718 divvy=div((int)ms, 1000);
720 req.tv_sec=divvy.quot;
721 req.tv_nsec=divvy.rem*1000000;
724 ret=nanosleep(&req, &rem);
726 /* Sleep interrupted with rem time remaining */
728 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
730 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
738 /* FIXME: implement alertable */
739 void SleepEx(guint32 ms, gboolean alertable)
741 if(alertable==TRUE) {
742 g_warning(G_GNUC_PRETTY_FUNCTION ": alertable not implemented");