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"
23 #include <sys/types.h>
26 #include <mono/io-layer/wapi.h>
27 #include <mono/io-layer/wapi-private.h>
28 #include <mono/io-layer/timed-thread.h>
29 #include <mono/io-layer/handles-private.h>
30 #include <mono/io-layer/misc-private.h>
31 #include <mono/io-layer/mono-mutex.h>
32 #include <mono/io-layer/thread-private.h>
33 #include <mono/io-layer/mono-spinlock.h>
34 #include <mono/io-layer/mutex-private.h>
36 #if HAVE_VALGRIND_MEMCHECK_H
37 #include <valgrind/memcheck.h>
44 /* Hash threads with tids. I thought of using TLS for this, but that
45 * would have to set the data in the new thread, which is more hassle
47 static mono_once_t thread_hash_once = MONO_ONCE_INIT;
48 static mono_mutex_t thread_hash_mutex = MONO_MUTEX_INITIALIZER;
49 static GHashTable *thread_hash=NULL;
52 static MonoGHashTable *tls_gc_hash = NULL;
55 static void thread_close_private (gpointer handle);
56 static void thread_own (gpointer handle);
58 struct _WapiHandleOps _wapi_thread_ops = {
59 NULL, /* close_shared */
60 thread_close_private, /* close_private */
66 static mono_once_t thread_ops_once=MONO_ONCE_INIT;
68 #ifdef WITH_INCLUDED_LIBGC
69 static void gc_init (void);
72 static void thread_ops_init (void)
74 _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD,
75 WAPI_HANDLE_CAP_WAIT);
77 #ifdef WITH_INCLUDED_LIBGC
82 static void thread_close_private (gpointer handle)
84 struct _WapiHandlePrivate_thread *thread_handle;
87 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, NULL,
88 (gpointer *)&thread_handle);
90 g_warning (G_GNUC_PRETTY_FUNCTION
91 ": error looking up thread handle %p", handle);
96 g_message(G_GNUC_PRETTY_FUNCTION
97 ": closing thread handle %p with thread %p id %ld",
98 handle, thread_handle->thread,
99 thread_handle->thread->id);
102 if(thread_handle->thread!=NULL) {
103 _wapi_timed_thread_destroy (thread_handle->thread);
107 static void thread_own (gpointer handle)
109 struct _WapiHandle_thread *thread_handle;
110 struct _WapiHandlePrivate_thread *thread_private_handle;
113 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
114 (gpointer *)&thread_handle,
115 (gpointer *)&thread_private_handle);
117 g_warning (G_GNUC_PRETTY_FUNCTION
118 ": error looking up thread handle %p", handle);
122 if(thread_private_handle->joined==FALSE) {
123 _wapi_timed_thread_join (thread_private_handle->thread, NULL,
125 thread_private_handle->joined=TRUE;
129 static void thread_exit(guint32 exitstatus, gpointer handle)
131 struct _WapiHandle_thread *thread_handle;
132 struct _WapiHandlePrivate_thread *thread_private_handle;
136 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
137 (gpointer *)&thread_handle,
138 (gpointer *)&thread_private_handle);
140 g_warning (G_GNUC_PRETTY_FUNCTION
141 ": error looking up thread handle %p", handle);
145 _wapi_mutex_check_abandoned (getpid (),
146 thread_private_handle->thread->id);
148 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
150 thr_ret = _wapi_handle_lock_handle (handle);
151 g_assert (thr_ret == 0);
154 g_message (G_GNUC_PRETTY_FUNCTION
155 ": Recording thread handle %p exit status", handle);
158 thread_handle->exitstatus=exitstatus;
159 thread_handle->state=THREAD_STATE_EXITED;
160 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
162 thr_ret = _wapi_handle_unlock_handle (handle);
163 g_assert (thr_ret == 0);
164 pthread_cleanup_pop (0);
167 g_message(G_GNUC_PRETTY_FUNCTION
168 ": Recording thread handle %p id %ld status as %d",
169 handle, thread_private_handle->thread->id, exitstatus);
172 /* Remove this thread from the hash */
173 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
174 (void *)&thread_hash_mutex);
175 thr_ret = mono_mutex_lock(&thread_hash_mutex);
176 g_assert (thr_ret == 0);
178 g_hash_table_remove(thread_hash, &thread_private_handle->thread->id);
180 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
181 g_assert (thr_ret == 0);
182 pthread_cleanup_pop (0);
184 /* The thread is no longer active, so unref it */
185 _wapi_handle_unref (handle);
188 static void thread_hash_init(void)
190 thread_hash=g_hash_table_new(g_int_hash, g_int_equal);
195 * @security: Ignored for now.
196 * @stacksize: the size in bytes of the new thread's stack. Use 0 to
197 * default to the normal stack size. (Ignored for now).
198 * @start: The function that the new thread should start with
199 * @param: The parameter to give to @start.
200 * @create: If 0, the new thread is ready to run immediately. If
201 * %CREATE_SUSPENDED, the new thread will be in the suspended state,
202 * requiring a ResumeThread() call to continue running.
203 * @tid: If non-NULL, the ID of the new thread is stored here.
205 * Creates a new threading handle.
207 * Return value: a new handle, or NULL
209 gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize,
210 WapiThreadStart start, gpointer param, guint32 create,
213 struct _WapiHandle_thread *thread_handle;
214 struct _WapiHandlePrivate_thread *thread_private_handle;
221 gpointer ct_ret = NULL;
223 mono_once(&thread_hash_once, thread_hash_init);
224 mono_once (&thread_ops_once, thread_ops_init);
230 handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
231 if(handle==_WAPI_HANDLE_INVALID) {
232 g_warning (G_GNUC_PRETTY_FUNCTION
233 ": error creating thread handle");
237 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
239 thr_ret = _wapi_handle_lock_handle (handle);
240 g_assert (thr_ret == 0);
242 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
243 (gpointer *)&thread_handle,
244 (gpointer *)&thread_private_handle);
246 g_warning (G_GNUC_PRETTY_FUNCTION
247 ": error looking up thread handle %p", handle);
251 /* Hold a reference while the thread is active, because we use
252 * the handle to store thread exit information
254 _wapi_handle_ref (handle);
256 thread_handle->state=THREAD_STATE_START;
258 /* Lock around the thread create, so that the new thread cant
259 * race us to look up the thread handle in GetCurrentThread()
261 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
262 (void *)&thread_hash_mutex);
263 thr_ret = mono_mutex_lock(&thread_hash_mutex);
264 g_assert (thr_ret == 0);
266 /* Set a 2M stack size. This is the default on Linux, but BSD
267 * needs it. (The original bug report from Martin Dvorak <md@9ll.cz>
268 * set the size to 2M-4k. I don't know why it's short by 4k, so
269 * I'm leaving it as 2M until I'm told differently.)
271 thr_ret = pthread_attr_init(&attr);
272 g_assert (thr_ret == 0);
274 /* defaults of 2Mb for 32bits and 4Mb for 64bits */
276 #if HAVE_VALGRIND_MEMCHECK_H
277 if (RUNNING_ON_VALGRIND)
280 stacksize = (SIZEOF_VOID_P / 2) * 1024 * 1024;
282 stacksize = (SIZEOF_VOID_P / 2) * 1024 * 1024;
287 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
288 thr_ret = pthread_attr_setstacksize(&attr, stacksize);
289 g_assert (thr_ret == 0);
292 ret=_wapi_timed_thread_create(&thread_private_handle->thread, &attr,
293 create, start, thread_exit, param,
297 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
300 /* Two, because of the reference we took above */
302 goto thread_hash_cleanup;
306 g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
310 g_message(G_GNUC_PRETTY_FUNCTION
311 ": Started thread handle %p thread %p ID %ld", handle,
312 thread_private_handle->thread,
313 thread_private_handle->thread->id);
317 #ifdef PTHREAD_POINTER_ID
318 *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
320 *tid=thread_private_handle->thread->id;
325 thr_ret = mono_mutex_unlock (&thread_hash_mutex);
326 g_assert (thr_ret == 0);
327 pthread_cleanup_pop (0);
330 thr_ret = _wapi_handle_unlock_handle (handle);
331 g_assert (thr_ret == 0);
332 pthread_cleanup_pop (0);
334 /* Must not call _wapi_handle_unref() with the handle already
337 for (i = 0; i < unrefs; i++) {
338 _wapi_handle_unref (handle);
344 gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 tid)
349 mono_once(&thread_hash_once, thread_hash_init);
350 mono_once (&thread_ops_once, thread_ops_init);
353 g_message (G_GNUC_PRETTY_FUNCTION ": looking up thread %d", tid);
356 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
357 (void *)&thread_hash_mutex);
358 thr_ret = mono_mutex_lock(&thread_hash_mutex);
359 g_assert (thr_ret == 0);
361 ret=g_hash_table_lookup(thread_hash, &tid);
363 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
364 g_assert (thr_ret == 0);
365 pthread_cleanup_pop (0);
368 _wapi_handle_ref (ret);
372 g_message (G_GNUC_PRETTY_FUNCTION ": returning thread handle %p", ret);
380 * @exitcode: Sets the thread's exit code, which can be read from
381 * another thread with GetExitCodeThread().
383 * Terminates the calling thread. A thread can also exit by returning
384 * from its start function. When the last thread in a process
385 * terminates, the process itself terminates.
387 void ExitThread(guint32 exitcode)
389 _wapi_timed_thread_exit(exitcode);
394 * @handle: The thread handle to query
395 * @exitcode: The thread @handle exit code is stored here
397 * Finds the exit code of @handle, and stores it in @exitcode. If the
398 * thread @handle is still running, the value stored is %STILL_ACTIVE.
400 * Return value: %TRUE, or %FALSE on error.
402 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
404 struct _WapiHandle_thread *thread_handle;
405 struct _WapiHandlePrivate_thread *thread_private_handle;
408 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
409 (gpointer *)&thread_handle,
410 (gpointer *)&thread_private_handle);
412 g_warning (G_GNUC_PRETTY_FUNCTION
413 ": error looking up thread handle %p", handle);
418 g_message(G_GNUC_PRETTY_FUNCTION
419 ": Finding exit status for thread handle %p id %ld", handle,
420 thread_private_handle->thread->id);
425 g_message(G_GNUC_PRETTY_FUNCTION
426 ": Nowhere to store exit code");
431 if(thread_handle->state!=THREAD_STATE_EXITED) {
433 g_message(G_GNUC_PRETTY_FUNCTION
434 ": Thread still active (state %d, exited is %d)",
435 thread_handle->state, THREAD_STATE_EXITED);
437 *exitcode=STILL_ACTIVE;
441 *exitcode=thread_handle->exitstatus;
447 * GetCurrentThreadId:
449 * Looks up the thread ID of the current thread. This ID can be
450 * passed to OpenThread() to create a new handle on this thread.
452 * Return value: the thread ID.
454 guint32 GetCurrentThreadId(void)
456 pthread_t tid=pthread_self();
458 #ifdef PTHREAD_POINTER_ID
459 return(GPOINTER_TO_UINT(tid));
465 static gpointer thread_attach(guint32 *tid)
467 struct _WapiHandle_thread *thread_handle;
468 struct _WapiHandlePrivate_thread *thread_private_handle;
474 gpointer ta_ret = NULL;
476 mono_once(&thread_hash_once, thread_hash_init);
477 mono_once (&thread_ops_once, thread_ops_init);
479 handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
480 if(handle==_WAPI_HANDLE_INVALID) {
481 g_warning (G_GNUC_PRETTY_FUNCTION
482 ": error creating thread handle");
486 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
488 thr_ret = _wapi_handle_lock_handle (handle);
489 g_assert (thr_ret == 0);
491 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
492 (gpointer *)&thread_handle,
493 (gpointer *)&thread_private_handle);
495 g_warning (G_GNUC_PRETTY_FUNCTION
496 ": error looking up thread handle %p", handle);
500 /* Hold a reference while the thread is active, because we use
501 * the handle to store thread exit information
503 _wapi_handle_ref (handle);
505 thread_handle->state=THREAD_STATE_START;
507 /* Lock around the thread create, so that the new thread cant
508 * race us to look up the thread handle in GetCurrentThread()
510 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
511 (void *)&thread_hash_mutex);
512 thr_ret = mono_mutex_lock(&thread_hash_mutex);
513 g_assert (thr_ret == 0);
515 ret=_wapi_timed_thread_attach(&thread_private_handle->thread,
516 thread_exit, handle);
519 g_message(G_GNUC_PRETTY_FUNCTION ": Thread attach error: %s",
522 /* Two, because of the reference we took above */
525 goto thread_hash_cleanup;
529 g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
533 g_message(G_GNUC_PRETTY_FUNCTION
534 ": Attached thread handle %p thread %p ID %ld", handle,
535 thread_private_handle->thread,
536 thread_private_handle->thread->id);
540 #ifdef PTHREAD_POINTER_ID
541 *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
543 *tid=thread_private_handle->thread->id;
548 thr_ret = mono_mutex_unlock (&thread_hash_mutex);
549 g_assert (thr_ret == 0);
550 pthread_cleanup_pop (0);
553 thr_ret = _wapi_handle_unlock_handle (handle);
554 g_assert (thr_ret == 0);
555 pthread_cleanup_pop (0);
557 /* Must not call _wapi_handle_unref() with the handle already
560 for (i = 0; i < unrefs; i++) {
561 _wapi_handle_unref (handle);
570 * Looks up the handle associated with the current thread. Under
571 * Windows this is a pseudohandle, and must be duplicated with
572 * DuplicateHandle() for some operations.
574 * Return value: The current thread handle, or %NULL on failure.
575 * (Unknown whether Windows has a possible failure here. It may be
576 * necessary to implement the pseudohandle-constant behaviour).
578 gpointer GetCurrentThread(void)
584 mono_once(&thread_hash_once, thread_hash_init);
585 mono_once (&thread_ops_once, thread_ops_init);
587 tid=GetCurrentThreadId();
589 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
590 (void *)&thread_hash_mutex);
591 thr_ret = mono_mutex_lock(&thread_hash_mutex);
592 g_assert (thr_ret == 0);
594 ret=g_hash_table_lookup(thread_hash, &tid);
596 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
597 g_assert (thr_ret == 0);
598 pthread_cleanup_pop (0);
601 ret = thread_attach (NULL);
609 * @handle: the thread handle to resume
611 * Decrements the suspend count of thread @handle. A thread can only
612 * run if its suspend count is zero.
614 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
616 guint32 ResumeThread(gpointer handle)
618 struct _WapiHandle_thread *thread_handle;
619 struct _WapiHandlePrivate_thread *thread_private_handle;
622 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
623 (gpointer *)&thread_handle,
624 (gpointer *)&thread_private_handle);
626 g_warning (G_GNUC_PRETTY_FUNCTION
627 ": error looking up thread handle %p", handle);
631 #ifdef WITH_INCLUDED_LIBGC
632 if (thread_private_handle->thread->suspend_count <= 1)
633 _wapi_timed_thread_resume (thread_private_handle->thread);
635 return --thread_private_handle->thread->suspend_count;
637 /* This is still a kludge that only copes with starting a
638 * thread that was suspended on create, so don't bother with
639 * the suspend count crap yet
641 _wapi_timed_thread_resume (thread_private_handle->thread);
648 * @handle: the thread handle to suspend
650 * Increments the suspend count of thread @handle. A thread can only
651 * run if its suspend count is zero.
653 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
655 guint32 SuspendThread(gpointer handle)
657 #ifdef WITH_INCLUDED_LIBGC
658 struct _WapiHandle_thread *thread_handle;
659 struct _WapiHandlePrivate_thread *thread_private_handle;
663 current = GetCurrentThread ();
664 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
665 (gpointer *)&thread_handle,
666 (gpointer *)&thread_private_handle);
668 g_warning (G_GNUC_PRETTY_FUNCTION
669 ": error looking up thread handle %p", handle);
673 if (!thread_private_handle->thread->suspend_count) {
674 if (handle == current)
675 _wapi_timed_thread_suspend (thread_private_handle->thread);
677 pthread_kill (thread_private_handle->thread->id, SIGPWR);
678 while (MONO_SEM_WAIT (&thread_private_handle->thread->suspended_sem) != 0) {
679 if (errno != EINTR) {
686 return thread_private_handle->thread->suspend_count++;
693 * We assume here that TLS_MINIMUM_AVAILABLE is less than
694 * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
697 * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
698 * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
702 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
703 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
704 static guint32 TLS_spinlock=0;
709 * Allocates a Thread Local Storage (TLS) index. Any thread in the
710 * same process can use this index to store and retrieve values that
711 * are local to that thread.
713 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
716 guint32 TlsAlloc(void)
721 MONO_SPIN_LOCK (TLS_spinlock);
723 for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
724 if(TLS_used[i]==FALSE) {
726 thr_ret = pthread_key_create(&TLS_keys[i], NULL);
727 g_assert (thr_ret == 0);
729 MONO_SPIN_UNLOCK (TLS_spinlock);
732 g_message (G_GNUC_PRETTY_FUNCTION ": returning key %d",
740 MONO_SPIN_UNLOCK (TLS_spinlock);
743 g_message (G_GNUC_PRETTY_FUNCTION ": out of indices");
747 return(TLS_OUT_OF_INDEXES);
750 #define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
754 * @idx: The TLS index to free
756 * Releases a TLS index, making it available for reuse. This call
757 * will delete any TLS data stored under index @idx in all threads.
759 * Return value: %TRUE on success, %FALSE otherwise.
761 gboolean TlsFree(guint32 idx)
766 g_message (G_GNUC_PRETTY_FUNCTION ": freeing key %d", idx);
769 MONO_SPIN_LOCK (TLS_spinlock);
771 if(TLS_used[idx]==FALSE) {
772 MONO_SPIN_UNLOCK (TLS_spinlock);
778 thr_ret = pthread_key_delete(TLS_keys[idx]);
779 g_assert (thr_ret == 0);
782 mono_g_hash_table_remove (tls_gc_hash, MAKE_GC_ID (idx));
785 MONO_SPIN_UNLOCK (TLS_spinlock);
792 * @idx: The TLS index to retrieve
794 * Retrieves the TLS data stored under index @idx.
796 * Return value: The value stored in the TLS index @idx in the current
797 * thread, or %NULL on error. As %NULL can be a valid return value,
798 * in this case GetLastError() returns %ERROR_SUCCESS.
800 gpointer TlsGetValue(guint32 idx)
805 g_message (G_GNUC_PRETTY_FUNCTION ": looking up key %d", idx);
808 ret=pthread_getspecific(TLS_keys[idx]);
811 g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", ret);
819 * @idx: The TLS index to store
820 * @value: The value to store under index @idx
822 * Stores @value at TLS index @idx.
824 * Return value: %TRUE on success, %FALSE otherwise.
826 gboolean TlsSetValue(guint32 idx, gpointer value)
831 g_message (G_GNUC_PRETTY_FUNCTION ": setting key %d to %p", idx,
835 MONO_SPIN_LOCK (TLS_spinlock);
837 if(TLS_used[idx]==FALSE) {
839 g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
842 MONO_SPIN_UNLOCK (TLS_spinlock);
847 ret=pthread_setspecific(TLS_keys[idx], value);
850 g_message (G_GNUC_PRETTY_FUNCTION
851 ": pthread_setspecific error: %s", strerror (ret));
854 MONO_SPIN_UNLOCK (TLS_spinlock);
861 tls_gc_hash = mono_g_hash_table_new(g_direct_hash, g_direct_equal);
862 mono_g_hash_table_insert (tls_gc_hash, MAKE_GC_ID (idx), value);
865 MONO_SPIN_UNLOCK (TLS_spinlock);
872 * @ms: The time in milliseconds to suspend for
873 * @alertable: if TRUE, the wait can be interrupted by an APC call
875 * Suspends execution of the current thread for @ms milliseconds. A
876 * value of zero causes the thread to relinquish its time slice. A
877 * value of %INFINITE causes an infinite delay.
879 guint32 SleepEx(guint32 ms, gboolean alertable)
881 struct timespec req, rem;
884 gpointer current_thread = NULL;
887 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
891 current_thread = GetCurrentThread ();
892 if (_wapi_thread_apc_pending (current_thread)) {
893 _wapi_thread_dispatch_apc_queue (current_thread);
894 return WAIT_IO_COMPLETION;
903 /* FIXME: check for INFINITE and sleep forever */
908 req.tv_nsec=ms_rem*1000000;
911 ret=nanosleep(&req, &rem);
913 if (alertable && _wapi_thread_apc_pending (current_thread)) {
914 _wapi_thread_dispatch_apc_queue (current_thread);
915 return WAIT_IO_COMPLETION;
919 /* Sleep interrupted with rem time remaining */
921 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
923 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
933 void Sleep(guint32 ms)
939 BindIoCompletionCallback (gpointer handle,
940 WapiOverlappedCB callback,
945 type = _wapi_handle_type (handle);
946 if (type == WAPI_HANDLE_FILE || type == WAPI_HANDLE_PIPE)
947 return _wapi_io_add_callback (handle, callback, flags);
949 SetLastError (ERROR_NOT_SUPPORTED);
953 guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle,
956 struct _WapiHandle_thread *thread_handle;
957 struct _WapiHandlePrivate_thread *thread_private_handle;
960 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
961 (gpointer *)&thread_handle,
962 (gpointer *)&thread_private_handle);
964 g_warning (G_GNUC_PRETTY_FUNCTION
965 ": error looking up thread handle %p", handle);
969 _wapi_timed_thread_queue_apc (thread_private_handle->thread,
970 apc_callback, param);
974 gboolean _wapi_thread_cur_apc_pending (void)
976 return _wapi_thread_apc_pending (GetCurrentThread ());
979 gboolean _wapi_thread_apc_pending (gpointer handle)
981 struct _WapiHandle_thread *thread_handle;
982 struct _WapiHandlePrivate_thread *thread_private_handle;
985 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
986 (gpointer *)&thread_handle,
987 (gpointer *)&thread_private_handle);
989 g_warning (G_GNUC_PRETTY_FUNCTION
990 ": error looking up thread handle %p", handle);
994 return _wapi_timed_thread_apc_pending (thread_private_handle->thread);
997 gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
999 struct _WapiHandle_thread *thread_handle;
1000 struct _WapiHandlePrivate_thread *thread_private_handle;
1003 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1004 (gpointer *)&thread_handle,
1005 (gpointer *)&thread_private_handle);
1007 g_warning (G_GNUC_PRETTY_FUNCTION
1008 ": error looking up thread handle %p", handle);
1012 _wapi_timed_thread_dispatch_apc_queue (thread_private_handle->thread);
1018 #ifdef WITH_INCLUDED_LIBGC
1020 static void GC_suspend_handler (int sig)
1022 struct _WapiHandle_thread *thread_handle;
1023 struct _WapiHandlePrivate_thread *thread_private_handle;
1027 handle = GetCurrentThread ();
1028 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1029 (gpointer *)&thread_handle,
1030 (gpointer *)&thread_private_handle);
1032 g_warning (G_GNUC_PRETTY_FUNCTION
1033 ": error looking up thread handle %p", handle);
1037 thread_private_handle->thread->stack_ptr = &ok;
1038 MONO_SEM_POST (&thread_private_handle->thread->suspended_sem);
1040 _wapi_timed_thread_suspend (thread_private_handle->thread);
1042 thread_private_handle->thread->stack_ptr = NULL;
1045 static void gc_init (void)
1047 struct sigaction act;
1049 act.sa_handler = GC_suspend_handler;
1050 g_assert (sigaction (SIGPWR, &act, NULL) == 0);
1053 void mono_wapi_push_thread_stack (gpointer handle, gpointer stack_ptr)
1055 struct _WapiHandle_thread *thread_handle;
1056 struct _WapiHandlePrivate_thread *thread_private_handle;
1059 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1060 (gpointer *)&thread_handle,
1061 (gpointer *)&thread_private_handle);
1063 g_warning (G_GNUC_PRETTY_FUNCTION
1064 ": error looking up thread handle %p", handle);
1068 GC_push_all_stack (thread_private_handle->thread->stack_ptr, stack_ptr);
1071 #endif /* WITH_INCLUDED_LIBGC */