/* * threads.c: Thread handles * * Author: * Dick Porter (dick@ximian.com) * * (C) 2002 Ximian, Inc. */ #include #if HAVE_BOEHM_GC #include #include "mono/utils/mono-hash.h" #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #undef DEBUG #undef TLS_DEBUG /* Hash threads with tids. I thought of using TLS for this, but that * would have to set the data in the new thread, which is more hassle */ static mono_once_t thread_hash_once = MONO_ONCE_INIT; static mono_mutex_t thread_hash_mutex = MONO_MUTEX_INITIALIZER; static GHashTable *thread_hash=NULL; #if HAVE_BOEHM_GC static MonoGHashTable *tls_gc_hash = NULL; #endif static void thread_close_private (gpointer handle); static void thread_own (gpointer handle); struct _WapiHandleOps _wapi_thread_ops = { NULL, /* close_shared */ thread_close_private, /* close_private */ NULL, /* signal */ thread_own, /* own */ NULL, /* is_owned */ }; static mono_once_t thread_ops_once=MONO_ONCE_INIT; static void thread_ops_init (void) { _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD, WAPI_HANDLE_CAP_WAIT); } static void thread_close_private (gpointer handle) { struct _WapiHandlePrivate_thread *thread_handle; gboolean ok; ok=_wapi_lookup_handle (handle, WAPI_HANDLE_UNUSED, NULL, (gpointer *)&thread_handle); if(ok==FALSE) { g_warning (G_GNUC_PRETTY_FUNCTION ": error looking up thread handle %p", handle); return; } #ifdef DEBUG g_message(G_GNUC_PRETTY_FUNCTION ": closing thread handle %p with thread %p id %ld", handle, thread_handle->thread, thread_handle->thread->id); #endif if(thread_handle->thread!=NULL) { _wapi_timed_thread_destroy (thread_handle->thread); } } static void thread_own (gpointer handle) { struct _WapiHandle_thread *thread_handle; struct _WapiHandlePrivate_thread *thread_private_handle; gboolean ok; ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle, (gpointer *)&thread_private_handle); if(ok==FALSE) { g_warning (G_GNUC_PRETTY_FUNCTION ": error looking up thread handle %p", handle); return; } if(thread_handle->joined==FALSE) { _wapi_timed_thread_join (thread_private_handle->thread, NULL, NULL); thread_handle->joined=TRUE; } } static void thread_exit(guint32 exitstatus, gpointer handle) { struct _WapiHandle_thread *thread_handle; struct _WapiHandlePrivate_thread *thread_private_handle; gboolean ok; ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle, (gpointer *)&thread_private_handle); if(ok==FALSE) { g_warning (G_GNUC_PRETTY_FUNCTION ": error looking up thread handle %p", handle); return; } _wapi_handle_lock_handle (handle); thread_handle->exitstatus=exitstatus; thread_handle->state=THREAD_STATE_EXITED; _wapi_handle_set_signal_state (handle, TRUE, TRUE); _wapi_handle_unlock_handle (handle); #ifdef DEBUG g_message(G_GNUC_PRETTY_FUNCTION ": Recording thread handle %p id %ld status as %d", handle, thread_private_handle->thread->id, exitstatus); #endif /* Remove this thread from the hash */ mono_mutex_lock(&thread_hash_mutex); g_hash_table_remove(thread_hash, &thread_private_handle->thread->id); mono_mutex_unlock(&thread_hash_mutex); /* The thread is no longer active, so unref it */ _wapi_handle_unref (handle); } static void thread_hash_init(void) { thread_hash=g_hash_table_new(g_int_hash, g_int_equal); } /** * CreateThread: * @security: Ignored for now. * @stacksize: the size in bytes of the new thread's stack. Use 0 to * default to the normal stack size. (Ignored for now). * @start: The function that the new thread should start with * @param: The parameter to give to @start. * @create: If 0, the new thread is ready to run immediately. If * %CREATE_SUSPENDED, the new thread will be in the suspended state, * requiring a ResumeThread() call to continue running. * @tid: If non-NULL, the ID of the new thread is stored here. * * Creates a new threading handle. * * Return value: a new handle, or NULL */ gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize G_GNUC_UNUSED, WapiThreadStart start, gpointer param, guint32 create G_GNUC_UNUSED, guint32 *tid) { struct _WapiHandle_thread *thread_handle; struct _WapiHandlePrivate_thread *thread_private_handle; gpointer handle; gboolean ok; int ret; mono_once(&thread_hash_once, thread_hash_init); mono_once (&thread_ops_once, thread_ops_init); if(start==NULL) { return(NULL); } handle=_wapi_handle_new (WAPI_HANDLE_THREAD); if(handle==_WAPI_HANDLE_INVALID) { g_warning (G_GNUC_PRETTY_FUNCTION ": error creating thread handle"); return(NULL); } _wapi_handle_lock_handle (handle); ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle, (gpointer *)&thread_private_handle); if(ok==FALSE) { g_warning (G_GNUC_PRETTY_FUNCTION ": error looking up thread handle %p", handle); _wapi_handle_unlock_handle (handle); return(NULL); } /* Hold a reference while the thread is active, because we use * the handle to store thread exit information */ _wapi_handle_ref (handle); thread_handle->state=THREAD_STATE_START; /* Lock around the thread create, so that the new thread cant * race us to look up the thread handle in GetCurrentThread() */ mono_mutex_lock(&thread_hash_mutex); ret=_wapi_timed_thread_create(&thread_private_handle->thread, NULL, start, thread_exit, param, handle); if(ret!=0) { #ifdef DEBUG g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s", strerror(ret)); #endif mono_mutex_unlock(&thread_hash_mutex); _wapi_handle_unlock_handle (handle); _wapi_handle_unref (handle); /* And again, because of the reference we took above */ _wapi_handle_unref (handle); return(NULL); } g_hash_table_insert(thread_hash, &thread_private_handle->thread->id, handle); mono_mutex_unlock(&thread_hash_mutex); #ifdef DEBUG g_message(G_GNUC_PRETTY_FUNCTION ": Started thread handle %p thread %p ID %ld", handle, thread_private_handle->thread, thread_private_handle->thread->id); #endif if(tid!=NULL) { *tid=thread_private_handle->thread->id; } _wapi_handle_unlock_handle (handle); return(handle); } /** * ExitThread: * @exitcode: Sets the thread's exit code, which can be read from * another thread with GetExitCodeThread(). * * Terminates the calling thread. A thread can also exit by returning * from its start function. When the last thread in a process * terminates, the process itself terminates. */ void ExitThread(guint32 exitcode) { _wapi_timed_thread_exit(exitcode); } /** * GetExitCodeThread: * @handle: The thread handle to query * @exitcode: The thread @handle exit code is stored here * * Finds the exit code of @handle, and stores it in @exitcode. If the * thread @handle is still running, the value stored is %STILL_ACTIVE. * * Return value: %TRUE, or %FALSE on error. */ gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode) { struct _WapiHandle_thread *thread_handle; struct _WapiHandlePrivate_thread *thread_private_handle; gboolean ok; ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle, (gpointer *)&thread_private_handle); if(ok==FALSE) { g_warning (G_GNUC_PRETTY_FUNCTION ": error looking up thread handle %p", handle); return(FALSE); } #ifdef DEBUG g_message(G_GNUC_PRETTY_FUNCTION ": Finding exit status for thread handle %p id %ld", handle, thread_private_handle->thread->id); #endif if(exitcode==NULL) { #ifdef DEBUG g_message(G_GNUC_PRETTY_FUNCTION ": Nowhere to store exit code"); #endif return(FALSE); } if(thread_handle->state!=THREAD_STATE_EXITED) { #ifdef DEBUG g_message(G_GNUC_PRETTY_FUNCTION ": Thread still active (state %d, exited is %d)", thread_handle->state, THREAD_STATE_EXITED); #endif *exitcode=STILL_ACTIVE; return(TRUE); } *exitcode=thread_handle->exitstatus; return(TRUE); } /** * GetCurrentThreadId: * * Looks up the thread ID of the current thread. This ID can be * passed to OpenThread() to create a new handle on this thread. * * Return value: the thread ID. */ guint32 GetCurrentThreadId(void) { pthread_t tid=pthread_self(); return(tid); } /** * GetCurrentThread: * * Looks up the handle associated with the current thread. Under * Windows this is a pseudohandle, and must be duplicated with * DuplicateHandle() for some operations. * * Return value: The current thread handle, or %NULL on failure. * (Unknown whether Windows has a possible failure here. It may be * necessary to implement the pseudohandle-constant behaviour). */ gpointer GetCurrentThread(void) { gpointer ret=NULL; guint32 tid; tid=GetCurrentThreadId(); mono_mutex_lock(&thread_hash_mutex); ret=g_hash_table_lookup(thread_hash, &tid); mono_mutex_unlock(&thread_hash_mutex); return(ret); } /** * ResumeThread: * @handle: the thread handle to resume * * Decrements the suspend count of thread @handle. A thread can only * run if its suspend count is zero. * * Return value: the previous suspend count, or 0xFFFFFFFF on error. */ guint32 ResumeThread(gpointer handle G_GNUC_UNUSED) { return(0xFFFFFFFF); } /** * SuspendThread: * @handle: the thread handle to suspend * * Increments the suspend count of thread @handle. A thread can only * run if its suspend count is zero. * * Return value: the previous suspend count, or 0xFFFFFFFF on error. */ guint32 SuspendThread(gpointer handle G_GNUC_UNUSED) { return(0xFFFFFFFF); } /* * We assume here that TLS_MINIMUM_AVAILABLE is less than * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for * library usage. * * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be * fine. */ static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE]; static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE}; static mono_mutex_t TLS_mutex=MONO_MUTEX_INITIALIZER; /** * TlsAlloc: * * Allocates a Thread Local Storage (TLS) index. Any thread in the * same process can use this index to store and retrieve values that * are local to that thread. * * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index * is available. */ guint32 TlsAlloc(void) { guint32 i; mono_mutex_lock(&TLS_mutex); for(i=0; i