/* * threads.c: Thread handles * * Author: * Dick Porter (dick@ximian.com) * * (C) 2002 Ximian, Inc. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if HAVE_VALGRIND_MEMCHECK_H #include #endif #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; static void thread_close (gpointer handle, gpointer data); static gboolean thread_own (gpointer handle); struct _WapiHandleOps _wapi_thread_ops = { thread_close, /* close */ NULL, /* signal */ thread_own, /* own */ NULL, /* is_owned */ }; static mono_once_t thread_ops_once=MONO_ONCE_INIT; #ifdef WITH_INCLUDED_LIBGC static void gc_init (void); #endif static void thread_ops_init (void) { _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD, WAPI_HANDLE_CAP_WAIT); #ifdef WITH_INCLUDED_LIBGC gc_init (); #endif } static void thread_close (gpointer handle, gpointer data) { struct _WapiHandle_thread *thread_handle = (struct _WapiHandle_thread *)data; #ifdef DEBUG g_message ("%s: closing thread handle %p", __func__, handle); #endif if (thread_handle->owner_pid == getpid ()) { g_ptr_array_free (thread_handle->owned_mutexes, TRUE); } } /* NB, always called with the shared handle lock held */ static gboolean thread_own (gpointer handle) { struct _WapiHandle_thread *thread_handle; gboolean ok; #ifdef DEBUG g_message ("%s: owning thread handle %p", __func__, handle); #endif ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return(FALSE); } if (thread_handle->owner_pid != getpid ()) { #ifdef DEBUG g_message ("%s: can't join thread, %d not owner process %d", __func__, getpid (), thread_handle->owner_pid); #endif /* FIXME: might need to return TRUE here so that other * processes can WaitFor thread handles */ return (FALSE); } if (thread_handle->joined == FALSE) { _wapi_timed_thread_join (thread_handle->thread, NULL, NULL); thread_handle->joined = TRUE; } return(TRUE); } /* Called by thread_exit(), but maybe by mono_thread_manage() too */ void _wapi_thread_abandon_mutexes (gpointer handle) { struct _WapiHandle_thread *thread_handle; gboolean ok; int i; ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return; } for (i = 0; i < thread_handle->owned_mutexes->len; i++) { gpointer mutex = g_ptr_array_index (thread_handle->owned_mutexes, i); _wapi_mutex_abandon (mutex, getpid (), thread_handle->thread->id); _wapi_thread_disown_mutex (thread_handle->thread->id, mutex); } } /* Called by the timed_thread code as a thread is finishing up */ static void thread_exit(guint32 exitstatus, gpointer handle) { struct _WapiHandle_thread *thread_handle; gboolean ok; int thr_ret; _wapi_thread_abandon_mutexes (handle); thr_ret = _wapi_handle_lock_shared_handles (); g_assert (thr_ret == 0); ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return; } #ifdef DEBUG g_message ("%s: Recording thread handle %p exit status", __func__, handle); #endif thread_handle->exitstatus = exitstatus; thread_handle->state = THREAD_STATE_EXITED; _wapi_shared_handle_set_signal_state (handle, TRUE); _wapi_handle_unlock_shared_handles (); #ifdef DEBUG g_message("%s: Recording thread handle %p id %ld status as %d", __func__, handle, thread_handle->thread->id, exitstatus); #endif /* Remove this thread from the hash */ pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup, (void *)&thread_hash_mutex); thr_ret = mono_mutex_lock(&thread_hash_mutex); g_assert (thr_ret == 0); g_hash_table_remove (thread_hash, (gpointer)(thread_handle->thread->id)); thr_ret = mono_mutex_unlock(&thread_hash_mutex); g_assert (thr_ret == 0); pthread_cleanup_pop (0); /* 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 (NULL, NULL); } /** * 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. NB * this is defined as a DWORD (ie 32bit) in the MS API, but we need to * cope with 64 bit IDs for s390x and amd64. * * Creates a new threading handle. * * Return value: a new handle, or NULL */ gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize, WapiThreadStart start, gpointer param, guint32 create, gsize *tid) { struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p; pthread_attr_t attr; gpointer handle; gboolean ok; int ret; int thr_ret; int i, unrefs = 0; gpointer ct_ret = NULL; mono_once (&thread_hash_once, thread_hash_init); mono_once (&thread_ops_once, thread_ops_init); if (start == NULL) { return(NULL); } thread_handle.state = THREAD_STATE_START; thread_handle.owner_pid = getpid (); thread_handle.owned_mutexes = g_ptr_array_new (); handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle); if (handle == _WAPI_HANDLE_INVALID) { g_warning ("%s: error creating thread handle", __func__); SetLastError (ERROR_GEN_FAILURE); return (NULL); } thr_ret = _wapi_handle_lock_shared_handles (); g_assert (thr_ret == 0); ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle_p); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); SetLastError (ERROR_GEN_FAILURE); goto cleanup; } /* Hold a reference while the thread is active, because we use * the handle to store thread exit information */ _wapi_handle_ref (handle); /* Lock around the thread create, so that the new thread cant * race us to look up the thread handle in GetCurrentThread() */ pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup, (void *)&thread_hash_mutex); thr_ret = mono_mutex_lock(&thread_hash_mutex); g_assert (thr_ret == 0); /* Set a 2M stack size. This is the default on Linux, but BSD * needs it. (The original bug report from Martin Dvorak * set the size to 2M-4k. I don't know why it's short by 4k, so * I'm leaving it as 2M until I'm told differently.) */ thr_ret = pthread_attr_init(&attr); g_assert (thr_ret == 0); /* defaults of 2Mb for 32bits and 4Mb for 64bits */ /* temporarily changed to use 1 MB: this allows more threads * to be used, as well as using less virtual memory and so * more is available for the GC heap. */ if (stacksize == 0){ #if HAVE_VALGRIND_MEMCHECK_H if (RUNNING_ON_VALGRIND) { stacksize = 1 << 20; } else { stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024; } #else stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024; #endif } #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE thr_ret = pthread_attr_setstacksize(&attr, stacksize); g_assert (thr_ret == 0); #endif ret = _wapi_timed_thread_create (&thread_handle_p->thread, &attr, create, start, thread_exit, param, handle); if (ret != 0) { #ifdef DEBUG g_message ("%s: Thread create error: %s", __func__, strerror(ret)); #endif /* Two, because of the reference we took above */ unrefs = 2; goto thread_hash_cleanup; } ct_ret = handle; g_hash_table_insert (thread_hash, (gpointer)(thread_handle_p->thread->id), handle); #ifdef DEBUG g_message("%s: Started thread handle %p thread %p ID %ld", __func__, handle, thread_handle_p->thread, thread_handle_p->thread->id); #endif if (tid != NULL) { #ifdef PTHREAD_POINTER_ID /* Don't use GPOINTER_TO_UINT here, it can't cope with * sizeof(void *) > sizeof(uint) when a cast to uint * would overflow */ *tid = (gsize)(thread_handle_p->thread->id); #else *tid = thread_handle_p->thread->id; #endif } thread_hash_cleanup: thr_ret = mono_mutex_unlock (&thread_hash_mutex); g_assert (thr_ret == 0); pthread_cleanup_pop (0); cleanup: _wapi_handle_unlock_shared_handles (); /* Must not call _wapi_handle_unref() with the shared handles * already locked */ for (i = 0; i < unrefs; i++) { _wapi_handle_unref (handle); } return(ct_ret); } gpointer _wapi_thread_handle_from_id (pthread_t tid) { gpointer ret=NULL; int thr_ret; pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup, (void *)&thread_hash_mutex); thr_ret = mono_mutex_lock(&thread_hash_mutex); g_assert (thr_ret == 0); ret = g_hash_table_lookup (thread_hash, (gpointer)(tid)); thr_ret = mono_mutex_unlock(&thread_hash_mutex); g_assert (thr_ret == 0); pthread_cleanup_pop (0); return(ret); } /* NB tid is 32bit in MS API, but we need 64bit on amd64 and s390x * (and probably others) */ gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, gsize tid) { gpointer ret=NULL; mono_once (&thread_hash_once, thread_hash_init); mono_once (&thread_ops_once, thread_ops_init); #ifdef DEBUG g_message ("%s: looking up thread %"G_GSIZE_FORMAT, __func__, tid); #endif ret = _wapi_thread_handle_from_id ((pthread_t)tid); if(ret!=NULL) { _wapi_handle_ref (ret); } #ifdef DEBUG g_message ("%s: returning thread handle %p", __func__, ret); #endif return(ret); } /** * 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; gboolean ok; ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return (FALSE); } #ifdef DEBUG g_message ("%s: Finding exit status for thread handle %p id %ld", __func__, handle, thread_handle->thread->id); #endif if (exitcode == NULL) { #ifdef DEBUG g_message ("%s: Nowhere to store exit code", __func__); #endif return(FALSE); } if (thread_handle->state != THREAD_STATE_EXITED) { #ifdef DEBUG g_message ("%s: Thread still active (state %d, exited is %d)", __func__, 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. NB this is defined as DWORD (ie 32 * bit) in the MS API, but we need to cope with 64 bit IDs for s390x * and amd64. This doesn't really break the API, it just embraces and * extends it on 64bit platforms :) */ gsize GetCurrentThreadId(void) { pthread_t tid = pthread_self(); #ifdef PTHREAD_POINTER_ID /* Don't use GPOINTER_TO_UINT here, it can't cope with * sizeof(void *) > sizeof(uint) when a cast to uint would * overflow */ return((gsize)tid); #else return(tid); #endif } static gpointer thread_attach(gsize *tid) { struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p; gpointer handle; gboolean ok; int ret; int thr_ret; int i, unrefs = 0; gpointer ta_ret = NULL; mono_once (&thread_hash_once, thread_hash_init); mono_once (&thread_ops_once, thread_ops_init); thread_handle.state = THREAD_STATE_START; thread_handle.owner_pid = getpid (); thread_handle.owned_mutexes = g_ptr_array_new (); handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle); if (handle == _WAPI_HANDLE_INVALID) { g_warning ("%s: error creating thread handle", __func__); SetLastError (ERROR_GEN_FAILURE); return (NULL); } thr_ret = _wapi_handle_lock_shared_handles (); g_assert (thr_ret == 0); ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle_p); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); SetLastError (ERROR_GEN_FAILURE); goto cleanup; } /* Hold a reference while the thread is active, because we use * the handle to store thread exit information */ _wapi_handle_ref (handle); /* Lock around the thread create, so that the new thread cant * race us to look up the thread handle in GetCurrentThread() */ pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup, (void *)&thread_hash_mutex); thr_ret = mono_mutex_lock(&thread_hash_mutex); g_assert (thr_ret == 0); ret = _wapi_timed_thread_attach (&thread_handle_p->thread, thread_exit, handle); if (ret != 0) { #ifdef DEBUG g_message ("%s: Thread attach error: %s", __func__, strerror(ret)); #endif /* Two, because of the reference we took above */ unrefs = 2; goto thread_hash_cleanup; } ta_ret = handle; g_hash_table_insert (thread_hash, (gpointer)(thread_handle_p->thread->id), handle); #ifdef DEBUG g_message("%s: Attached thread handle %p thread %p ID %ld", __func__, handle, thread_handle_p->thread, thread_handle_p->thread->id); #endif if (tid != NULL) { #ifdef PTHREAD_POINTER_ID /* Don't use GPOINTER_TO_UINT here, it can't cope with * sizeof(void *) > sizeof(uint) when a cast to uint * would overflow */ *tid = (gsize)(thread_handle_p->thread->id); #else *tid = thread_handle_p->thread->id; #endif } thread_hash_cleanup: thr_ret = mono_mutex_unlock (&thread_hash_mutex); g_assert (thr_ret == 0); pthread_cleanup_pop (0); cleanup: _wapi_handle_unlock_shared_handles (); /* Must not call _wapi_handle_unref() with the shared handles * already locked */ for (i = 0; i < unrefs; i++) { _wapi_handle_unref (handle); } return(ta_ret); } /** * 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; gsize tid; mono_once(&thread_hash_once, thread_hash_init); mono_once (&thread_ops_once, thread_ops_init); tid = GetCurrentThreadId(); ret = _wapi_thread_handle_from_id ((pthread_t)tid); if (!ret) { ret = thread_attach (NULL); } 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) { struct _WapiHandle_thread *thread_handle; gboolean ok; ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return (0xFFFFFFFF); } if (thread_handle->thread == NULL) { return(0xFFFFFFFF); } #ifdef WITH_INCLUDED_LIBGC if (thread_handle->thread->suspend_count <= 1) _wapi_timed_thread_resume (thread_handle->thread); return (--thread_handle->thread->suspend_count)); #else /* This is still a kludge that only copes with starting a * thread that was suspended on create, so don't bother with * the suspend count crap yet */ _wapi_timed_thread_resume (thread_handle->thread); return(0xFFFFFFFF); #endif } /** * 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) { #ifdef WITH_INCLUDED_LIBGC struct _WapiHandle_thread *thread_handle; gpointer current; gboolean ok; current = GetCurrentThread (); ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return (0xFFFFFFFF); } if (thread_handle->thread == NULL) { return(0xFFFFFFFF); } if (!thread_handle->thread->suspend_count) { if (handle == current) _wapi_timed_thread_suspend (thread_handle->thread); else { pthread_kill (thread_handle->thread->id, SIGPWR); while (MONO_SEM_WAIT (&thread_handle->thread->suspended_sem) != 0) { if (errno != EINTR) { return(0xFFFFFFFF); } } } } return (thread_handle->thread->suspend_count++); #else return(0xFFFFFFFF); #endif } /* * 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 guint32 TLS_spinlock=0; guint32 mono_pthread_key_for_tls (guint32 idx) { return (guint32)TLS_keys [idx]; } /** * 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; int thr_ret; MONO_SPIN_LOCK (TLS_spinlock); for(i=0; ithread, apc_callback, param); return(1); } gboolean _wapi_thread_cur_apc_pending (void) { return(_wapi_thread_apc_pending (GetCurrentThread ())); } gboolean _wapi_thread_apc_pending (gpointer handle) { struct _WapiHandle_thread *thread_handle; gboolean ok; ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return (FALSE); } return(_wapi_timed_thread_apc_pending (thread_handle->thread)); } gboolean _wapi_thread_dispatch_apc_queue (gpointer handle) { struct _WapiHandle_thread *thread_handle; gboolean ok; ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return (0); } _wapi_timed_thread_dispatch_apc_queue (thread_handle->thread); return(1); } void _wapi_thread_own_mutex (pthread_t tid, gpointer mutex) { struct _WapiHandle_thread *thread_handle; gboolean ok; gpointer thread; thread = _wapi_thread_handle_from_id (tid); if (thread == NULL) { g_warning ("%s: error looking up thread by ID", __func__); return; } ok = _wapi_lookup_handle (thread, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, thread); return; } _wapi_handle_ref (mutex); g_ptr_array_add (thread_handle->owned_mutexes, mutex); } void _wapi_thread_disown_mutex (pthread_t tid, gpointer mutex) { struct _WapiHandle_thread *thread_handle; gboolean ok; gpointer thread; thread = _wapi_thread_handle_from_id (tid); if (thread == NULL) { g_warning ("%s: error looking up thread by ID", __func__); return; } ok = _wapi_lookup_handle (thread, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, thread); return; } _wapi_handle_unref (mutex); g_ptr_array_remove (thread_handle->owned_mutexes, mutex); } #ifdef WITH_INCLUDED_LIBGC static void GC_suspend_handler (int sig) { struct _WapiHandle_thread *thread_handle; gpointer handle; gboolean ok; handle = GetCurrentThread (); ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return; } thread_handle->thread->stack_ptr = &ok; MONO_SEM_POST (&thread_handle->thread->suspended_sem); _wapi_timed_thread_suspend (thread_handle->thread); thread_handle->thread->stack_ptr = NULL; } static void gc_init (void) { struct sigaction act; act.sa_handler = GC_suspend_handler; g_assert (sigaction (SIGPWR, &act, NULL) == 0); } void mono_wapi_push_thread_stack (gpointer handle, gpointer stack_ptr) { struct _WapiHandle_thread *thread_handle; gboolean ok; ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return; } GC_push_all_stack (thread_handle->thread->stack_ptr, stack_ptr); } #endif /* WITH_INCLUDED_LIBGC */