2 * threads.c: Thread handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
19 #include <sys/types.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>
30 #include <mono/io-layer/mutex-private.h>
32 #if HAVE_VALGRIND_MEMCHECK_H
33 #include <valgrind/memcheck.h>
40 /* Hash threads with tids. I thought of using TLS for this, but that
41 * would have to set the data in the new thread, which is more hassle
43 static mono_once_t thread_hash_once = MONO_ONCE_INIT;
44 static mono_mutex_t thread_hash_mutex = MONO_MUTEX_INITIALIZER;
45 static GHashTable *thread_hash=NULL;
47 static void thread_close (gpointer handle, gpointer data);
48 static gboolean thread_own (gpointer handle);
50 struct _WapiHandleOps _wapi_thread_ops = {
51 thread_close, /* close */
57 static mono_once_t thread_ops_once=MONO_ONCE_INIT;
59 #ifdef WITH_INCLUDED_LIBGC
60 static void gc_init (void);
63 static void thread_ops_init (void)
65 _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD,
66 WAPI_HANDLE_CAP_WAIT);
68 #ifdef WITH_INCLUDED_LIBGC
73 static void thread_close (gpointer handle, gpointer data)
75 struct _WapiHandle_thread *thread_handle = (struct _WapiHandle_thread *)data;
78 g_message ("%s: closing thread handle %p", __func__, handle);
81 g_ptr_array_free (thread_handle->owned_mutexes, TRUE);
84 static gboolean thread_own (gpointer handle)
86 struct _WapiHandle_thread *thread_handle;
90 g_message ("%s: owning thread handle %p", __func__, handle);
93 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
94 (gpointer *)&thread_handle);
96 g_warning ("%s: error looking up thread handle %p", __func__,
101 if (thread_handle->joined == FALSE) {
102 _wapi_timed_thread_join (thread_handle->thread, NULL, NULL);
103 thread_handle->joined = TRUE;
109 static void thread_exit(guint32 exitstatus, gpointer handle)
111 struct _WapiHandle_thread *thread_handle;
116 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
118 thr_ret = _wapi_handle_lock_handle (handle);
119 g_assert (thr_ret == 0);
121 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
122 (gpointer *)&thread_handle);
124 g_warning ("%s: error looking up thread handle %p", __func__,
129 for (i = 0; i < thread_handle->owned_mutexes->len; i++) {
130 _wapi_mutex_abandon (g_ptr_array_index (thread_handle->owned_mutexes, i), getpid (), thread_handle->thread->id);
134 g_message ("%s: Recording thread handle %p exit status", __func__,
138 thread_handle->exitstatus = exitstatus;
139 thread_handle->state = THREAD_STATE_EXITED;
141 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
143 thr_ret = _wapi_handle_unlock_handle (handle);
144 g_assert (thr_ret == 0);
145 pthread_cleanup_pop (0);
148 g_message("%s: Recording thread handle %p id %ld status as %d",
149 __func__, handle, thread_handle->thread->id, exitstatus);
152 /* Remove this thread from the hash */
153 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
154 (void *)&thread_hash_mutex);
155 thr_ret = mono_mutex_lock(&thread_hash_mutex);
156 g_assert (thr_ret == 0);
158 g_hash_table_remove (thread_hash, (gpointer)(thread_handle->thread->id));
160 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
161 g_assert (thr_ret == 0);
162 pthread_cleanup_pop (0);
164 /* The thread is no longer active, so unref it */
165 _wapi_handle_unref (handle);
168 static void thread_hash_init(void)
170 thread_hash = g_hash_table_new (NULL, NULL);
175 * @security: Ignored for now.
176 * @stacksize: the size in bytes of the new thread's stack. Use 0 to
177 * default to the normal stack size. (Ignored for now).
178 * @start: The function that the new thread should start with
179 * @param: The parameter to give to @start.
180 * @create: If 0, the new thread is ready to run immediately. If
181 * %CREATE_SUSPENDED, the new thread will be in the suspended state,
182 * requiring a ResumeThread() call to continue running.
183 * @tid: If non-NULL, the ID of the new thread is stored here. NB
184 * this is defined as a DWORD (ie 32bit) in the MS API, but we need to
185 * cope with 64 bit IDs for s390x and amd64.
187 * Creates a new threading handle.
189 * Return value: a new handle, or NULL
191 gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize,
192 WapiThreadStart start, gpointer param, guint32 create,
195 struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
202 gpointer ct_ret = NULL;
204 mono_once (&thread_hash_once, thread_hash_init);
205 mono_once (&thread_ops_once, thread_ops_init);
211 thread_handle.state = THREAD_STATE_START;
212 thread_handle.owner_pid = getpid ();
213 thread_handle.owned_mutexes = g_ptr_array_new ();
215 handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle);
216 if (handle == _WAPI_HANDLE_INVALID) {
217 g_warning ("%s: error creating thread handle", __func__);
218 SetLastError (ERROR_GEN_FAILURE);
223 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
225 thr_ret = _wapi_handle_lock_handle (handle);
226 g_assert (thr_ret == 0);
228 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
229 (gpointer *)&thread_handle_p);
231 g_warning ("%s: error looking up thread handle %p", __func__,
233 SetLastError (ERROR_GEN_FAILURE);
238 /* Hold a reference while the thread is active, because we use
239 * the handle to store thread exit information
241 _wapi_handle_ref (handle);
243 /* Lock around the thread create, so that the new thread cant
244 * race us to look up the thread handle in GetCurrentThread()
246 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
247 (void *)&thread_hash_mutex);
248 thr_ret = mono_mutex_lock(&thread_hash_mutex);
249 g_assert (thr_ret == 0);
251 /* Set a 2M stack size. This is the default on Linux, but BSD
252 * needs it. (The original bug report from Martin Dvorak <md@9ll.cz>
253 * set the size to 2M-4k. I don't know why it's short by 4k, so
254 * I'm leaving it as 2M until I'm told differently.)
256 thr_ret = pthread_attr_init(&attr);
257 g_assert (thr_ret == 0);
259 /* defaults of 2Mb for 32bits and 4Mb for 64bits */
260 /* temporarily changed to use 1 MB: this allows more threads
261 * to be used, as well as using less virtual memory and so
262 * more is available for the GC heap.
265 #if HAVE_VALGRIND_MEMCHECK_H
266 if (RUNNING_ON_VALGRIND) {
269 stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
272 stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
276 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
277 thr_ret = pthread_attr_setstacksize(&attr, stacksize);
278 g_assert (thr_ret == 0);
281 ret = _wapi_timed_thread_create (&thread_handle_p->thread, &attr,
282 create, start, thread_exit, param,
286 g_message ("%s: Thread create error: %s", __func__,
290 /* Two, because of the reference we took above */
293 goto thread_hash_cleanup;
297 g_hash_table_insert (thread_hash,
298 (gpointer)(thread_handle_p->thread->id),
302 g_message("%s: Started thread handle %p thread %p ID %ld", __func__,
303 handle, thread_handle_p->thread,
304 thread_handle_p->thread->id);
308 #ifdef PTHREAD_POINTER_ID
309 /* Don't use GPOINTER_TO_UINT here, it can't cope with
310 * sizeof(void *) > sizeof(uint) when a cast to uint
313 *tid = (gsize)(thread_handle_p->thread->id);
315 *tid = thread_handle_p->thread->id;
320 thr_ret = mono_mutex_unlock (&thread_hash_mutex);
321 g_assert (thr_ret == 0);
322 pthread_cleanup_pop (0);
325 thr_ret = _wapi_handle_unlock_handle (handle);
326 g_assert (thr_ret == 0);
327 pthread_cleanup_pop (0);
329 /* Must not call _wapi_handle_unref() with the handle already
332 for (i = 0; i < unrefs; i++) {
333 _wapi_handle_unref (handle);
339 gpointer _wapi_thread_handle_from_id (pthread_t tid)
344 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
345 (void *)&thread_hash_mutex);
346 thr_ret = mono_mutex_lock(&thread_hash_mutex);
347 g_assert (thr_ret == 0);
349 ret = g_hash_table_lookup (thread_hash, (gpointer)(tid));
351 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
352 g_assert (thr_ret == 0);
353 pthread_cleanup_pop (0);
358 /* NB tid is 32bit in MS API, but we need 64bit on amd64 and s390x
359 * (and probably others)
361 gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, gsize tid)
365 mono_once (&thread_hash_once, thread_hash_init);
366 mono_once (&thread_ops_once, thread_ops_init);
369 g_message ("%s: looking up thread %"G_GSIZE_FORMAT, __func__, tid);
372 ret = _wapi_thread_handle_from_id ((pthread_t)tid);
374 _wapi_handle_ref (ret);
378 g_message ("%s: returning thread handle %p", __func__, ret);
386 * @exitcode: Sets the thread's exit code, which can be read from
387 * another thread with GetExitCodeThread().
389 * Terminates the calling thread. A thread can also exit by returning
390 * from its start function. When the last thread in a process
391 * terminates, the process itself terminates.
393 void ExitThread(guint32 exitcode)
395 _wapi_timed_thread_exit(exitcode);
400 * @handle: The thread handle to query
401 * @exitcode: The thread @handle exit code is stored here
403 * Finds the exit code of @handle, and stores it in @exitcode. If the
404 * thread @handle is still running, the value stored is %STILL_ACTIVE.
406 * Return value: %TRUE, or %FALSE on error.
408 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
410 struct _WapiHandle_thread *thread_handle;
413 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
414 (gpointer *)&thread_handle);
416 g_warning ("%s: error looking up thread handle %p", __func__,
422 g_message ("%s: Finding exit status for thread handle %p id %ld",
423 __func__, handle, thread_handle->thread->id);
426 if (exitcode == NULL) {
428 g_message ("%s: Nowhere to store exit code", __func__);
433 if (thread_handle->state != THREAD_STATE_EXITED) {
435 g_message ("%s: Thread still active (state %d, exited is %d)",
436 __func__, thread_handle->state,
437 THREAD_STATE_EXITED);
439 *exitcode = STILL_ACTIVE;
443 *exitcode = thread_handle->exitstatus;
449 * GetCurrentThreadId:
451 * Looks up the thread ID of the current thread. This ID can be
452 * passed to OpenThread() to create a new handle on this thread.
454 * Return value: the thread ID. NB this is defined as DWORD (ie 32
455 * bit) in the MS API, but we need to cope with 64 bit IDs for s390x
456 * and amd64. This doesn't really break the API, it just embraces and
457 * extends it on 64bit platforms :)
459 gsize GetCurrentThreadId(void)
461 pthread_t tid = pthread_self();
463 #ifdef PTHREAD_POINTER_ID
464 /* Don't use GPOINTER_TO_UINT here, it can't cope with
465 * sizeof(void *) > sizeof(uint) when a cast to uint would
474 static gpointer thread_attach(gsize *tid)
476 struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
482 gpointer ta_ret = NULL;
484 mono_once (&thread_hash_once, thread_hash_init);
485 mono_once (&thread_ops_once, thread_ops_init);
487 thread_handle.state = THREAD_STATE_START;
488 thread_handle.owner_pid = getpid ();
489 thread_handle.owned_mutexes = g_ptr_array_new ();
491 handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle);
492 if (handle == _WAPI_HANDLE_INVALID) {
493 g_warning ("%s: error creating thread handle", __func__);
495 SetLastError (ERROR_GEN_FAILURE);
499 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
501 thr_ret = _wapi_handle_lock_handle (handle);
502 g_assert (thr_ret == 0);
504 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
505 (gpointer *)&thread_handle_p);
507 g_warning ("%s: error looking up thread handle %p", __func__,
510 SetLastError (ERROR_GEN_FAILURE);
514 /* Hold a reference while the thread is active, because we use
515 * the handle to store thread exit information
517 _wapi_handle_ref (handle);
519 /* Lock around the thread create, so that the new thread cant
520 * race us to look up the thread handle in GetCurrentThread()
522 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
523 (void *)&thread_hash_mutex);
524 thr_ret = mono_mutex_lock(&thread_hash_mutex);
525 g_assert (thr_ret == 0);
527 ret = _wapi_timed_thread_attach (&thread_handle_p->thread, thread_exit,
531 g_message ("%s: Thread attach error: %s", __func__,
535 /* Two, because of the reference we took above */
538 goto thread_hash_cleanup;
542 g_hash_table_insert (thread_hash,
543 (gpointer)(thread_handle_p->thread->id),
547 g_message("%s: Attached thread handle %p thread %p ID %ld", __func__,
548 handle, thread_handle_p->thread,
549 thread_handle_p->thread->id);
553 #ifdef PTHREAD_POINTER_ID
554 /* Don't use GPOINTER_TO_UINT here, it can't cope with
555 * sizeof(void *) > sizeof(uint) when a cast to uint
558 *tid = (gsize)(thread_handle_p->thread->id);
560 *tid = thread_handle_p->thread->id;
565 thr_ret = mono_mutex_unlock (&thread_hash_mutex);
566 g_assert (thr_ret == 0);
567 pthread_cleanup_pop (0);
570 thr_ret = _wapi_handle_unlock_handle (handle);
571 g_assert (thr_ret == 0);
572 pthread_cleanup_pop (0);
574 /* Must not call _wapi_handle_unref() with the handle already
577 for (i = 0; i < unrefs; i++) {
578 _wapi_handle_unref (handle);
587 * Looks up the handle associated with the current thread. Under
588 * Windows this is a pseudohandle, and must be duplicated with
589 * DuplicateHandle() for some operations.
591 * Return value: The current thread handle, or %NULL on failure.
592 * (Unknown whether Windows has a possible failure here. It may be
593 * necessary to implement the pseudohandle-constant behaviour).
595 gpointer GetCurrentThread(void)
600 mono_once(&thread_hash_once, thread_hash_init);
601 mono_once (&thread_ops_once, thread_ops_init);
603 tid = GetCurrentThreadId();
605 ret = _wapi_thread_handle_from_id ((pthread_t)tid);
607 ret = thread_attach (NULL);
615 * @handle: the thread handle to resume
617 * Decrements the suspend count of thread @handle. A thread can only
618 * run if its suspend count is zero.
620 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
622 guint32 ResumeThread(gpointer handle)
624 struct _WapiHandle_thread *thread_handle;
627 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
628 (gpointer *)&thread_handle);
630 g_warning ("%s: error looking up thread handle %p", __func__,
636 if (thread_handle->thread == NULL) {
640 #ifdef WITH_INCLUDED_LIBGC
641 if (thread_handle->thread->suspend_count <= 1)
642 _wapi_timed_thread_resume (thread_handle->thread);
644 return (--thread_handle->thread->suspend_count));
646 /* This is still a kludge that only copes with starting a
647 * thread that was suspended on create, so don't bother with
648 * the suspend count crap yet
650 _wapi_timed_thread_resume (thread_handle->thread);
657 * @handle: the thread handle to suspend
659 * Increments the suspend count of thread @handle. A thread can only
660 * run if its suspend count is zero.
662 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
664 guint32 SuspendThread(gpointer handle)
666 #ifdef WITH_INCLUDED_LIBGC
667 struct _WapiHandle_thread *thread_handle;
671 current = GetCurrentThread ();
672 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
673 (gpointer *)&thread_handle);
675 g_warning ("%s: error looking up thread handle %p", __func__,
680 if (thread_handle->thread == NULL) {
684 if (!thread_handle->thread->suspend_count) {
685 if (handle == current)
686 _wapi_timed_thread_suspend (thread_handle->thread);
688 pthread_kill (thread_handle->thread->id, SIGPWR);
689 while (MONO_SEM_WAIT (&thread_handle->thread->suspended_sem) != 0) {
690 if (errno != EINTR) {
697 return (thread_handle->thread->suspend_count++);
704 * We assume here that TLS_MINIMUM_AVAILABLE is less than
705 * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
708 * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
709 * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
713 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
714 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
715 static guint32 TLS_spinlock=0;
718 mono_pthread_key_for_tls (guint32 idx)
720 return (guint32)TLS_keys [idx];
726 * Allocates a Thread Local Storage (TLS) index. Any thread in the
727 * same process can use this index to store and retrieve values that
728 * are local to that thread.
730 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
733 guint32 TlsAlloc(void)
738 MONO_SPIN_LOCK (TLS_spinlock);
740 for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
741 if(TLS_used[i]==FALSE) {
743 thr_ret = pthread_key_create(&TLS_keys[i], NULL);
744 g_assert (thr_ret == 0);
746 MONO_SPIN_UNLOCK (TLS_spinlock);
749 g_message ("%s: returning key %d", __func__, i);
756 MONO_SPIN_UNLOCK (TLS_spinlock);
759 g_message ("%s: out of indices", __func__);
763 return(TLS_OUT_OF_INDEXES);
766 #define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
770 * @idx: The TLS index to free
772 * Releases a TLS index, making it available for reuse. This call
773 * will delete any TLS data stored under index @idx in all threads.
775 * Return value: %TRUE on success, %FALSE otherwise.
777 gboolean TlsFree(guint32 idx)
782 g_message ("%s: freeing key %d", __func__, idx);
785 MONO_SPIN_LOCK (TLS_spinlock);
787 if(TLS_used[idx]==FALSE) {
788 MONO_SPIN_UNLOCK (TLS_spinlock);
794 thr_ret = pthread_key_delete(TLS_keys[idx]);
795 g_assert (thr_ret == 0);
797 MONO_SPIN_UNLOCK (TLS_spinlock);
804 * @idx: The TLS index to retrieve
806 * Retrieves the TLS data stored under index @idx.
808 * Return value: The value stored in the TLS index @idx in the current
809 * thread, or %NULL on error. As %NULL can be a valid return value,
810 * in this case GetLastError() returns %ERROR_SUCCESS.
812 gpointer TlsGetValue(guint32 idx)
817 g_message ("%s: looking up key %d", __func__, idx);
820 ret=pthread_getspecific(TLS_keys[idx]);
823 g_message ("%s: returning %p", __func__, ret);
831 * @idx: The TLS index to store
832 * @value: The value to store under index @idx
834 * Stores @value at TLS index @idx.
836 * Return value: %TRUE on success, %FALSE otherwise.
838 gboolean TlsSetValue(guint32 idx, gpointer value)
843 g_message ("%s: setting key %d to %p", __func__, idx, value);
846 MONO_SPIN_LOCK (TLS_spinlock);
848 if(TLS_used[idx]==FALSE) {
850 g_message ("%s: key %d unused", __func__, idx);
853 MONO_SPIN_UNLOCK (TLS_spinlock);
858 ret=pthread_setspecific(TLS_keys[idx], value);
861 g_message ("%s: pthread_setspecific error: %s", __func__,
865 MONO_SPIN_UNLOCK (TLS_spinlock);
870 MONO_SPIN_UNLOCK (TLS_spinlock);
877 * @ms: The time in milliseconds to suspend for
878 * @alertable: if TRUE, the wait can be interrupted by an APC call
880 * Suspends execution of the current thread for @ms milliseconds. A
881 * value of zero causes the thread to relinquish its time slice. A
882 * value of %INFINITE causes an infinite delay.
884 guint32 SleepEx(guint32 ms, gboolean alertable)
886 struct timespec req, rem;
889 gpointer current_thread = NULL;
892 g_message("%s: Sleeping for %d ms", __func__, ms);
896 current_thread = GetCurrentThread ();
897 if (_wapi_thread_apc_pending (current_thread)) {
898 _wapi_thread_dispatch_apc_queue (current_thread);
899 return WAIT_IO_COMPLETION;
908 /* FIXME: check for INFINITE and sleep forever */
913 req.tv_nsec=ms_rem*1000000;
916 ret=nanosleep(&req, &rem);
918 if (alertable && _wapi_thread_apc_pending (current_thread)) {
919 _wapi_thread_dispatch_apc_queue (current_thread);
920 return WAIT_IO_COMPLETION;
924 /* Sleep interrupted with rem time remaining */
926 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
928 g_message("%s: Still got %d ms to go", __func__, rems);
937 void Sleep(guint32 ms)
942 guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle,
945 struct _WapiHandle_thread *thread_handle;
948 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
949 (gpointer *)&thread_handle);
951 g_warning ("%s: error looking up thread handle %p", __func__,
956 _wapi_timed_thread_queue_apc (thread_handle->thread, apc_callback,
961 gboolean _wapi_thread_cur_apc_pending (void)
963 return(_wapi_thread_apc_pending (GetCurrentThread ()));
966 gboolean _wapi_thread_apc_pending (gpointer handle)
968 struct _WapiHandle_thread *thread_handle;
971 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
972 (gpointer *)&thread_handle);
974 g_warning ("%s: error looking up thread handle %p", __func__,
979 return(_wapi_timed_thread_apc_pending (thread_handle->thread));
982 gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
984 struct _WapiHandle_thread *thread_handle;
987 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
988 (gpointer *)&thread_handle);
990 g_warning ("%s: error looking up thread handle %p", __func__,
995 _wapi_timed_thread_dispatch_apc_queue (thread_handle->thread);
999 void _wapi_thread_own_mutex (pthread_t tid, gpointer mutex)
1001 struct _WapiHandle_thread *thread_handle;
1005 thread = _wapi_thread_handle_from_id (tid);
1006 if (thread == NULL) {
1007 g_warning ("%s: error looking up thread by ID", __func__);
1011 ok = _wapi_lookup_handle (thread, WAPI_HANDLE_THREAD,
1012 (gpointer *)&thread_handle);
1014 g_warning ("%s: error looking up thread handle %p", __func__,
1019 g_ptr_array_add (thread_handle->owned_mutexes, mutex);
1022 void _wapi_thread_disown_mutex (pthread_t tid, gpointer mutex)
1024 struct _WapiHandle_thread *thread_handle;
1028 thread = _wapi_thread_handle_from_id (tid);
1029 if (thread == NULL) {
1030 g_warning ("%s: error looking up thread by ID", __func__);
1034 ok = _wapi_lookup_handle (thread, WAPI_HANDLE_THREAD,
1035 (gpointer *)&thread_handle);
1037 g_warning ("%s: error looking up thread handle %p", __func__,
1042 g_ptr_array_remove (thread_handle->owned_mutexes, mutex);
1047 #ifdef WITH_INCLUDED_LIBGC
1049 static void GC_suspend_handler (int sig)
1051 struct _WapiHandle_thread *thread_handle;
1055 handle = GetCurrentThread ();
1056 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1057 (gpointer *)&thread_handle);
1059 g_warning ("%s: error looking up thread handle %p", __func__,
1064 thread_handle->thread->stack_ptr = &ok;
1065 MONO_SEM_POST (&thread_handle->thread->suspended_sem);
1067 _wapi_timed_thread_suspend (thread_handle->thread);
1069 thread_handle->thread->stack_ptr = NULL;
1072 static void gc_init (void)
1074 struct sigaction act;
1076 act.sa_handler = GC_suspend_handler;
1077 g_assert (sigaction (SIGPWR, &act, NULL) == 0);
1080 void mono_wapi_push_thread_stack (gpointer handle, gpointer stack_ptr)
1082 struct _WapiHandle_thread *thread_handle;
1085 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1086 (gpointer *)&thread_handle);
1088 g_warning ("%s: error looking up thread handle %p", __func__,
1093 GC_push_all_stack (thread_handle->thread->stack_ptr, stack_ptr);
1096 #endif /* WITH_INCLUDED_LIBGC */