2 * threads.c: Thread handles
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Ximian, Inc.
14 #include <mono/os/gc_wrapper.h>
20 #include <sys/types.h>
23 #include <mono/io-layer/wapi.h>
24 #include <mono/io-layer/wapi-private.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>
39 /* Hash threads with tids. I thought of using TLS for this, but that
40 * would have to set the data in the new thread, which is more hassle
42 static mono_once_t thread_hash_once = MONO_ONCE_INIT;
43 static pthread_key_t thread_hash_key;
45 /* This key is used with attached threads and a destructor to signal
46 * when attached threads exit, as they don't have the thread_exit()
49 static pthread_key_t thread_attached_key;
51 struct _WapiHandleOps _wapi_thread_ops = {
56 NULL, /* special_wait */
60 static mono_once_t thread_ops_once=MONO_ONCE_INIT;
62 static void thread_ops_init (void)
64 _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD,
65 WAPI_HANDLE_CAP_WAIT);
68 void _wapi_thread_cleanup (void)
72 ret = pthread_key_delete (thread_hash_key);
75 ret = pthread_key_delete (thread_attached_key);
79 /* Called by thread_exit(), but maybe indirectly by
80 * mono_thread_manage() via mono_thread_signal_self() too
82 static void _wapi_thread_abandon_mutexes (gpointer handle)
84 struct _WapiHandle_thread *thread_handle;
87 pid_t pid = _wapi_getpid ();
88 pthread_t tid = pthread_self ();
91 g_message ("%s: Thread %p abandoning held mutexes", __func__, handle);
95 handle = _wapi_thread_handle_from_id (pthread_self ());
97 /* Something gone badly wrong... */
102 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
103 (gpointer *)&thread_handle);
105 g_warning ("%s: error looking up thread handle %p", __func__,
110 if (thread_handle->owner_pid != pid ||
111 !pthread_equal (thread_handle->id, tid)) {
115 for (i = 0; i < thread_handle->owned_mutexes->len; i++) {
116 gpointer mutex = g_ptr_array_index (thread_handle->owned_mutexes, i);
118 _wapi_mutex_abandon (mutex, pid, tid);
119 _wapi_thread_disown_mutex (mutex);
123 void _wapi_thread_set_termination_details (gpointer handle,
126 struct _WapiHandle_thread *thread_handle;
130 if (_wapi_handle_issignalled (handle) ||
131 _wapi_handle_type (handle) == WAPI_HANDLE_UNUSED) {
132 /* We must have already deliberately finished with
133 * this thread, so don't do any more now
139 g_message ("%s: Thread %p terminating", __func__, handle);
142 thr_ret = _wapi_handle_lock_shared_handles ();
143 g_assert (thr_ret == 0);
145 _wapi_thread_abandon_mutexes (handle);
147 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
148 (gpointer *)&thread_handle);
150 g_warning ("%s: error looking up thread handle %p", __func__,
153 _wapi_handle_unlock_shared_handles ();
157 thread_handle->exitstatus = exitstatus;
158 thread_handle->state = THREAD_STATE_EXITED;
159 MONO_SEM_DESTROY (&thread_handle->suspend_sem);
160 g_ptr_array_free (thread_handle->owned_mutexes, TRUE);
162 _wapi_shared_handle_set_signal_state (handle, TRUE);
164 _wapi_handle_unlock_shared_handles ();
167 g_message("%s: Recording thread handle %p id %ld status as %d",
168 __func__, handle, thread_handle->id, exitstatus);
171 /* The thread is no longer active, so unref it */
172 _wapi_handle_unref (handle);
175 void _wapi_thread_signal_self (guint32 exitstatus)
179 handle = _wapi_thread_handle_from_id (pthread_self ());
180 if (handle == NULL) {
181 /* Something gone badly wrong... */
185 _wapi_thread_set_termination_details (handle, exitstatus);
188 /* Called by the thread creation code as a thread is finishing up, and
191 static void thread_exit (guint32 exitstatus, gpointer handle) G_GNUC_NORETURN;
192 static void thread_exit (guint32 exitstatus, gpointer handle)
194 _wapi_thread_set_termination_details (handle, exitstatus);
196 /* Call pthread_exit() to call destructors and really exit the
202 static void thread_attached_exit (gpointer handle)
204 /* Drop the extra reference we take in thread_attach, now this
208 _wapi_thread_set_termination_details (handle, 0);
211 static void thread_hash_init(void)
215 thr_ret = pthread_key_create (&thread_hash_key, NULL);
216 g_assert (thr_ret == 0);
218 thr_ret = pthread_key_create (&thread_attached_key,
219 thread_attached_exit);
220 g_assert (thr_ret == 0);
223 static void _wapi_thread_suspend (struct _WapiHandle_thread *thread)
225 g_assert (thread->owner_pid == _wapi_getpid ());
226 g_assert (pthread_equal (thread->id, pthread_self ()));
228 while (MONO_SEM_WAIT (&thread->suspend_sem) != 0 &&
232 static void _wapi_thread_resume (struct _WapiHandle_thread *thread)
234 if (thread->owner_pid != _wapi_getpid ()) {
238 MONO_SEM_POST (&thread->suspend_sem);
241 static void *thread_start_routine (gpointer args) G_GNUC_NORETURN;
242 static void *thread_start_routine (gpointer args)
244 struct _WapiHandle_thread *thread = (struct _WapiHandle_thread *)args;
247 thr_ret = pthread_detach (pthread_self ());
248 g_assert (thr_ret == 0);
250 thr_ret = pthread_setspecific (thread_hash_key,
251 (void *)thread->handle);
252 g_assert (thr_ret == 0);
254 thread->id = pthread_self();
257 g_message ("%s: started thread id %ld", __func__, thread->id);
260 if (thread->create_flags & CREATE_SUSPENDED) {
261 _wapi_thread_suspend (thread);
264 thread_exit (thread->start_routine (thread->start_arg),
268 /* Even though we tell gcc that this function doesn't return,
269 * other compilers won't see that.
277 * @security: Ignored for now.
278 * @stacksize: the size in bytes of the new thread's stack. Use 0 to
279 * default to the normal stack size. (Ignored for now).
280 * @start: The function that the new thread should start with
281 * @param: The parameter to give to @start.
282 * @create: If 0, the new thread is ready to run immediately. If
283 * %CREATE_SUSPENDED, the new thread will be in the suspended state,
284 * requiring a ResumeThread() call to continue running.
285 * @tid: If non-NULL, the ID of the new thread is stored here. NB
286 * this is defined as a DWORD (ie 32bit) in the MS API, but we need to
287 * cope with 64 bit IDs for s390x and amd64.
289 * Creates a new threading handle.
291 * Return value: a new handle, or NULL
293 gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize,
294 WapiThreadStart start, gpointer param, guint32 create,
297 struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
304 gpointer ct_ret = NULL;
306 mono_once (&thread_hash_once, thread_hash_init);
307 mono_once (&thread_ops_once, thread_ops_init);
313 thread_handle.state = THREAD_STATE_START;
314 thread_handle.owner_pid = _wapi_getpid ();
315 thread_handle.owned_mutexes = g_ptr_array_new ();
316 thread_handle.create_flags = create;
317 thread_handle.start_routine = start;
318 thread_handle.start_arg = param;
320 handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle);
321 if (handle == _WAPI_HANDLE_INVALID) {
322 g_warning ("%s: error creating thread handle", __func__);
323 SetLastError (ERROR_GEN_FAILURE);
328 thr_ret = _wapi_handle_lock_shared_handles ();
329 g_assert (thr_ret == 0);
331 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
332 (gpointer *)&thread_handle_p);
334 g_warning ("%s: error looking up thread handle %p", __func__,
336 SetLastError (ERROR_GEN_FAILURE);
341 /* Hold a reference while the thread is active, because we use
342 * the handle to store thread exit information
344 _wapi_handle_ref (handle);
346 /* Set a 2M stack size. This is the default on Linux, but BSD
347 * needs it. (The original bug report from Martin Dvorak <md@9ll.cz>
348 * set the size to 2M-4k. I don't know why it's short by 4k, so
349 * I'm leaving it as 2M until I'm told differently.)
351 thr_ret = pthread_attr_init(&attr);
352 g_assert (thr_ret == 0);
354 /* defaults of 2Mb for 32bits and 4Mb for 64bits */
355 /* temporarily changed to use 1 MB: this allows more threads
356 * to be used, as well as using less virtual memory and so
357 * more is available for the GC heap.
360 #if HAVE_VALGRIND_MEMCHECK_H
361 if (RUNNING_ON_VALGRIND) {
364 stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
367 stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
371 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
372 thr_ret = pthread_attr_setstacksize(&attr, stacksize);
373 g_assert (thr_ret == 0);
376 MONO_SEM_INIT (&thread_handle_p->suspend_sem, 0);
377 thread_handle_p->handle = handle;
379 ret = pthread_create (&thread_handle_p->id, &attr,
380 thread_start_routine, (void *)thread_handle_p);
383 g_message ("%s: Thread create error: %s", __func__,
387 /* Two, because of the reference we took above */
395 g_message("%s: Started thread handle %p ID %ld", __func__, handle,
396 thread_handle_p->id);
400 #ifdef PTHREAD_POINTER_ID
401 /* Don't use GPOINTER_TO_UINT here, it can't cope with
402 * sizeof(void *) > sizeof(uint) when a cast to uint
405 *tid = (gsize)(thread_handle_p->id);
407 *tid = thread_handle_p->id;
412 _wapi_handle_unlock_shared_handles ();
414 /* Must not call _wapi_handle_unref() with the shared handles
417 for (i = 0; i < unrefs; i++) {
418 _wapi_handle_unref (handle);
424 /* The only time this function is called when tid != pthread_self ()
425 * is from OpenThread (), so we can fast-path most cases by just
426 * looking up the handle in TLS. OpenThread () must cope with a NULL
427 * return and do a handle search in that case.
429 gpointer _wapi_thread_handle_from_id (pthread_t tid)
433 if (pthread_equal (tid, pthread_self ()) &&
434 (ret = pthread_getspecific (thread_hash_key)) != NULL) {
435 /* We know the handle */
438 g_message ("%s: Returning %p for self thread %ld from TLS",
446 g_message ("%s: Returning NULL for unknown or non-self thread %ld",
454 static gboolean find_thread_by_id (gpointer handle, gpointer user_data)
456 pthread_t tid = (pthread_t)user_data;
457 struct _WapiHandle_thread *thread_handle;
460 /* Ignore threads that have already exited (ie they are signalled) */
461 if (_wapi_handle_issignalled (handle) == FALSE) {
462 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
463 (gpointer *)&thread_handle);
465 /* It's possible that the handle has vanished
466 * during the _wapi_search_handle before it
467 * gets here, so don't spam the console with
474 g_message ("%s: looking at thread %ld from process %d", __func__, thread_handle->id, thread_handle->owner_pid);
477 if (thread_handle->owner_pid != _wapi_getpid ()) {
478 /* Not sure if ms has this limitation with
479 * OpenThread(), but pthreads IDs are not
480 * unique across processes
483 g_message ("%s: not this process", __func__);
488 if (pthread_equal (thread_handle->id, tid)) {
490 g_message ("%s: found the thread we are looking for",
498 g_message ("%s: not found %ld, returning FALSE", __func__, tid);
504 /* NB tid is 32bit in MS API, but we need 64bit on amd64 and s390x
505 * (and probably others)
507 gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, gsize tid)
511 mono_once (&thread_hash_once, thread_hash_init);
512 mono_once (&thread_ops_once, thread_ops_init);
515 g_message ("%s: looking up thread %"G_GSIZE_FORMAT, __func__, tid);
518 ret = _wapi_thread_handle_from_id ((pthread_t)tid);
520 /* We need to search for this thread */
521 ret = _wapi_search_handle (WAPI_HANDLE_THREAD, find_thread_by_id, (gpointer)tid, NULL, TRUE); /* FIXME: have a proper look at this, me might not need to set search_shared = TRUE */
523 /* if _wapi_search_handle() returns a found handle, it
526 _wapi_handle_ref (ret);
530 g_message ("%s: returning thread handle %p", __func__, ret);
538 * @exitcode: Sets the thread's exit code, which can be read from
539 * another thread with GetExitCodeThread().
541 * Terminates the calling thread. A thread can also exit by returning
542 * from its start function. When the last thread in a process
543 * terminates, the process itself terminates.
545 void ExitThread(guint32 exitcode)
547 gpointer thread = _wapi_thread_handle_from_id (pthread_self ());
549 if (thread != NULL) {
550 thread_exit(exitcode, thread);
552 /* Just blow this thread away */
559 * @handle: The thread handle to query
560 * @exitcode: The thread @handle exit code is stored here
562 * Finds the exit code of @handle, and stores it in @exitcode. If the
563 * thread @handle is still running, the value stored is %STILL_ACTIVE.
565 * Return value: %TRUE, or %FALSE on error.
567 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
569 struct _WapiHandle_thread *thread_handle;
572 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
573 (gpointer *)&thread_handle);
575 g_warning ("%s: error looking up thread handle %p", __func__,
581 g_message ("%s: Finding exit status for thread handle %p id %ld",
582 __func__, handle, thread_handle->id);
585 if (exitcode == NULL) {
587 g_message ("%s: Nowhere to store exit code", __func__);
592 if (thread_handle->state != THREAD_STATE_EXITED) {
594 g_message ("%s: Thread still active (state %d, exited is %d)",
595 __func__, thread_handle->state,
596 THREAD_STATE_EXITED);
598 *exitcode = STILL_ACTIVE;
602 *exitcode = thread_handle->exitstatus;
608 * GetCurrentThreadId:
610 * Looks up the thread ID of the current thread. This ID can be
611 * passed to OpenThread() to create a new handle on this thread.
613 * Return value: the thread ID. NB this is defined as DWORD (ie 32
614 * bit) in the MS API, but we need to cope with 64 bit IDs for s390x
615 * and amd64. This doesn't really break the API, it just embraces and
616 * extends it on 64bit platforms :)
618 gsize GetCurrentThreadId(void)
620 pthread_t tid = pthread_self();
622 #ifdef PTHREAD_POINTER_ID
623 /* Don't use GPOINTER_TO_UINT here, it can't cope with
624 * sizeof(void *) > sizeof(uint) when a cast to uint would
633 static gpointer thread_attach(gsize *tid)
635 struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
640 mono_once (&thread_hash_once, thread_hash_init);
641 mono_once (&thread_ops_once, thread_ops_init);
643 thread_handle.state = THREAD_STATE_START;
644 thread_handle.owner_pid = _wapi_getpid ();
645 thread_handle.owned_mutexes = g_ptr_array_new ();
647 handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle);
648 if (handle == _WAPI_HANDLE_INVALID) {
649 g_warning ("%s: error creating thread handle", __func__);
651 SetLastError (ERROR_GEN_FAILURE);
655 thr_ret = _wapi_handle_lock_shared_handles ();
656 g_assert (thr_ret == 0);
658 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
659 (gpointer *)&thread_handle_p);
661 g_warning ("%s: error looking up thread handle %p", __func__,
664 SetLastError (ERROR_GEN_FAILURE);
668 /* Hold a reference while the thread is active, because we use
669 * the handle to store thread exit information
671 _wapi_handle_ref (handle);
673 /* suspend_sem is not used for attached threads, but
674 * thread_exit() might try to destroy it
676 MONO_SEM_INIT (&thread_handle_p->suspend_sem, 0);
677 thread_handle_p->handle = handle;
678 thread_handle_p->id = pthread_self ();
680 thr_ret = pthread_setspecific (thread_hash_key, (void *)handle);
681 g_assert (thr_ret == 0);
683 thr_ret = pthread_setspecific (thread_attached_key, (void *)handle);
684 g_assert (thr_ret == 0);
687 g_message("%s: Attached thread handle %p ID %ld", __func__, handle,
688 thread_handle_p->id);
692 #ifdef PTHREAD_POINTER_ID
693 /* Don't use GPOINTER_TO_UINT here, it can't cope with
694 * sizeof(void *) > sizeof(uint) when a cast to uint
697 *tid = (gsize)(thread_handle_p->id);
699 *tid = thread_handle_p->id;
704 _wapi_handle_unlock_shared_handles ();
709 gpointer _wapi_thread_duplicate ()
713 mono_once (&thread_hash_once, thread_hash_init);
714 mono_once (&thread_ops_once, thread_ops_init);
716 ret = _wapi_thread_handle_from_id (pthread_self ());
718 ret = thread_attach (NULL);
720 _wapi_handle_ref (ret);
729 * Looks up the handle associated with the current thread. Under
730 * Windows this is a pseudohandle, and must be duplicated with
731 * DuplicateHandle() for some operations.
733 * Return value: The current thread handle, or %NULL on failure.
734 * (Unknown whether Windows has a possible failure here. It may be
735 * necessary to implement the pseudohandle-constant behaviour).
737 gpointer GetCurrentThread(void)
739 mono_once(&thread_hash_once, thread_hash_init);
740 mono_once (&thread_ops_once, thread_ops_init);
742 return(_WAPI_THREAD_CURRENT);
747 * @handle: the thread handle to resume
749 * Decrements the suspend count of thread @handle. A thread can only
750 * run if its suspend count is zero.
752 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
754 guint32 ResumeThread(gpointer handle)
756 struct _WapiHandle_thread *thread_handle;
759 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
760 (gpointer *)&thread_handle);
762 g_warning ("%s: error looking up thread handle %p", __func__,
768 /* This is still a kludge that only copes with starting a
769 * thread that was suspended on create, so don't bother with
770 * the suspend count crap yet
772 _wapi_thread_resume (thread_handle);
778 * @handle: the thread handle to suspend
780 * Increments the suspend count of thread @handle. A thread can only
781 * run if its suspend count is zero.
783 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
785 guint32 SuspendThread(gpointer handle)
791 * We assume here that TLS_MINIMUM_AVAILABLE is less than
792 * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
795 * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
796 * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
800 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
801 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
802 static guint32 TLS_spinlock=0;
805 mono_pthread_key_for_tls (guint32 idx)
807 return (guint32)TLS_keys [idx];
813 * Allocates a Thread Local Storage (TLS) index. Any thread in the
814 * same process can use this index to store and retrieve values that
815 * are local to that thread.
817 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
820 guint32 TlsAlloc(void)
825 MONO_SPIN_LOCK (TLS_spinlock);
827 for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
828 if(TLS_used[i]==FALSE) {
830 thr_ret = pthread_key_create(&TLS_keys[i], NULL);
831 g_assert (thr_ret == 0);
833 MONO_SPIN_UNLOCK (TLS_spinlock);
836 g_message ("%s: returning key %d", __func__, i);
843 MONO_SPIN_UNLOCK (TLS_spinlock);
846 g_message ("%s: out of indices", __func__);
850 return(TLS_OUT_OF_INDEXES);
853 #define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
857 * @idx: The TLS index to free
859 * Releases a TLS index, making it available for reuse. This call
860 * will delete any TLS data stored under index @idx in all threads.
862 * Return value: %TRUE on success, %FALSE otherwise.
864 gboolean TlsFree(guint32 idx)
869 g_message ("%s: freeing key %d", __func__, idx);
872 MONO_SPIN_LOCK (TLS_spinlock);
874 if(TLS_used[idx]==FALSE) {
875 MONO_SPIN_UNLOCK (TLS_spinlock);
881 thr_ret = pthread_key_delete(TLS_keys[idx]);
882 g_assert (thr_ret == 0);
884 MONO_SPIN_UNLOCK (TLS_spinlock);
891 * @idx: The TLS index to retrieve
893 * Retrieves the TLS data stored under index @idx.
895 * Return value: The value stored in the TLS index @idx in the current
896 * thread, or %NULL on error. As %NULL can be a valid return value,
897 * in this case GetLastError() returns %ERROR_SUCCESS.
899 gpointer TlsGetValue(guint32 idx)
904 g_message ("%s: looking up key %d", __func__, idx);
907 ret=pthread_getspecific(TLS_keys[idx]);
910 g_message ("%s: returning %p", __func__, ret);
918 * @idx: The TLS index to store
919 * @value: The value to store under index @idx
921 * Stores @value at TLS index @idx.
923 * Return value: %TRUE on success, %FALSE otherwise.
925 gboolean TlsSetValue(guint32 idx, gpointer value)
930 g_message ("%s: setting key %d to %p", __func__, idx, value);
933 MONO_SPIN_LOCK (TLS_spinlock);
935 if(TLS_used[idx]==FALSE) {
937 g_message ("%s: key %d unused", __func__, idx);
940 MONO_SPIN_UNLOCK (TLS_spinlock);
945 ret=pthread_setspecific(TLS_keys[idx], value);
948 g_message ("%s: pthread_setspecific error: %s", __func__,
952 MONO_SPIN_UNLOCK (TLS_spinlock);
957 MONO_SPIN_UNLOCK (TLS_spinlock);
964 * @ms: The time in milliseconds to suspend for
965 * @alertable: if TRUE, the wait can be interrupted by an APC call
967 * Suspends execution of the current thread for @ms milliseconds. A
968 * value of zero causes the thread to relinquish its time slice. A
969 * value of %INFINITE causes an infinite delay.
971 guint32 SleepEx(guint32 ms, gboolean alertable)
973 struct timespec req, rem;
976 gpointer current_thread = NULL;
979 g_message("%s: Sleeping for %d ms", __func__, ms);
983 current_thread = _wapi_thread_handle_from_id (pthread_self ());
984 if (current_thread == NULL) {
985 SetLastError (ERROR_INVALID_HANDLE);
989 if (_wapi_thread_apc_pending (current_thread)) {
990 _wapi_thread_dispatch_apc_queue (current_thread);
991 return WAIT_IO_COMPLETION;
1000 /* FIXME: check for INFINITE and sleep forever */
1001 ms_quot = ms / 1000;
1005 req.tv_nsec=ms_rem*1000000;
1008 ret=nanosleep(&req, &rem);
1010 if (alertable && _wapi_thread_apc_pending (current_thread)) {
1011 _wapi_thread_dispatch_apc_queue (current_thread);
1012 return WAIT_IO_COMPLETION;
1016 /* Sleep interrupted with rem time remaining */
1018 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
1020 g_message("%s: Still got %d ms to go", __func__, rems);
1029 void Sleep(guint32 ms)
1034 static mono_mutex_t apc_mutex = MONO_MUTEX_INITIALIZER;
1036 gboolean _wapi_thread_cur_apc_pending (void)
1038 gpointer thread = _wapi_thread_handle_from_id (pthread_self ());
1040 if (thread == NULL) {
1041 SetLastError (ERROR_INVALID_HANDLE);
1045 return(_wapi_thread_apc_pending (thread));
1048 static void _wapi_thread_queue_apc (struct _WapiHandle_thread *thread,
1049 guint32 (*apc_callback)(gpointer),
1055 if (thread->owner_pid != _wapi_getpid ()) {
1059 apc = (ApcInfo *)g_new (ApcInfo, 1);
1060 apc->callback = apc_callback;
1063 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
1064 (void *)&apc_mutex);
1065 thr_ret = mono_mutex_lock (&apc_mutex);
1066 g_assert (thr_ret == 0);
1068 thread->apc_queue = g_slist_append (thread->apc_queue, apc);
1070 thr_ret = mono_mutex_unlock (&apc_mutex);
1071 g_assert (thr_ret == 0);
1072 pthread_cleanup_pop (0);
1075 gboolean _wapi_thread_apc_pending (gpointer handle)
1077 struct _WapiHandle_thread *thread;
1080 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1081 (gpointer *)&thread);
1084 /* This might happen at process shutdown, as all
1085 * thread handles are forcibly closed. If a thread
1086 * still has an alertable wait the final
1087 * _wapi_thread_apc_pending check will probably fail
1088 * to find the handle
1090 g_warning ("%s: error looking up thread handle %p", __func__,
1096 if (thread->owner_pid != _wapi_getpid ()) {
1100 return(thread->apc_queue != NULL);
1103 gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
1105 struct _WapiHandle_thread *thread;
1111 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1112 (gpointer *)&thread);
1114 g_warning ("%s: error looking up thread handle %p", __func__,
1119 if (thread->owner_pid != _wapi_getpid ()) {
1123 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
1124 (void *)&apc_mutex);
1125 thr_ret = mono_mutex_lock (&apc_mutex);
1126 g_assert (thr_ret == 0);
1128 list = thread->apc_queue;
1129 thread->apc_queue = NULL;
1131 thr_ret = mono_mutex_unlock (&apc_mutex);
1132 g_assert (thr_ret == 0);
1133 pthread_cleanup_pop (0);
1135 while (list != NULL) {
1136 apc = (ApcInfo *)list->data;
1137 apc->callback (apc->param);
1139 list = g_slist_next (list);
1141 g_slist_free (list);
1146 guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle,
1149 struct _WapiHandle_thread *thread_handle;
1152 ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1153 (gpointer *)&thread_handle);
1155 g_warning ("%s: error looking up thread handle %p", __func__,
1160 _wapi_thread_queue_apc (thread_handle, apc_callback, param);
1164 void _wapi_thread_own_mutex (gpointer mutex)
1166 struct _WapiHandle_thread *thread_handle;
1170 thread = _wapi_thread_handle_from_id (pthread_self ());
1171 if (thread == NULL) {
1172 g_warning ("%s: error looking up thread by ID", __func__);
1176 ok = _wapi_lookup_handle (thread, WAPI_HANDLE_THREAD,
1177 (gpointer *)&thread_handle);
1179 g_warning ("%s: error looking up thread handle %p", __func__,
1184 _wapi_handle_ref (mutex);
1186 g_ptr_array_add (thread_handle->owned_mutexes, mutex);
1189 void _wapi_thread_disown_mutex (gpointer mutex)
1191 struct _WapiHandle_thread *thread_handle;
1195 thread = _wapi_thread_handle_from_id (pthread_self ());
1196 if (thread == NULL) {
1197 g_warning ("%s: error looking up thread by ID", __func__);
1201 ok = _wapi_lookup_handle (thread, WAPI_HANDLE_THREAD,
1202 (gpointer *)&thread_handle);
1204 g_warning ("%s: error looking up thread handle %p", __func__,
1209 _wapi_handle_unref (mutex);
1211 g_ptr_array_remove (thread_handle->owned_mutexes, mutex);