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 thread_handle->thread=NULL;
105 static void thread_own (gpointer handle)
107 struct _WapiHandle_thread *thread_handle;
108 struct _WapiHandlePrivate_thread *thread_private_handle;
111 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
112 (gpointer *)&thread_handle,
113 (gpointer *)&thread_private_handle);
115 g_warning (G_GNUC_PRETTY_FUNCTION
116 ": error looking up thread handle %p", handle);
120 if(thread_private_handle->joined==FALSE) {
121 _wapi_timed_thread_join (thread_private_handle->thread, NULL,
123 thread_private_handle->joined=TRUE;
127 static void thread_exit(guint32 exitstatus, gpointer handle)
129 struct _WapiHandle_thread *thread_handle;
130 struct _WapiHandlePrivate_thread *thread_private_handle;
134 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
135 (gpointer *)&thread_handle,
136 (gpointer *)&thread_private_handle);
138 g_warning (G_GNUC_PRETTY_FUNCTION
139 ": error looking up thread handle %p", handle);
143 _wapi_mutex_check_abandoned (getpid (),
144 thread_private_handle->thread->id);
146 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
148 thr_ret = _wapi_handle_lock_handle (handle);
149 g_assert (thr_ret == 0);
152 g_message (G_GNUC_PRETTY_FUNCTION
153 ": Recording thread handle %p exit status", handle);
156 thread_handle->exitstatus=exitstatus;
157 thread_handle->state=THREAD_STATE_EXITED;
158 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
160 thr_ret = _wapi_handle_unlock_handle (handle);
161 g_assert (thr_ret == 0);
162 pthread_cleanup_pop (0);
165 g_message(G_GNUC_PRETTY_FUNCTION
166 ": Recording thread handle %p id %ld status as %d",
167 handle, thread_private_handle->thread->id, exitstatus);
170 /* Remove this thread from the hash */
171 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
172 (void *)&thread_hash_mutex);
173 thr_ret = mono_mutex_lock(&thread_hash_mutex);
174 g_assert (thr_ret == 0);
176 g_hash_table_remove(thread_hash, &thread_private_handle->thread->id);
178 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
179 g_assert (thr_ret == 0);
180 pthread_cleanup_pop (0);
182 /* The thread is no longer active, so unref it */
183 _wapi_handle_unref (handle);
186 static void thread_hash_init(void)
188 thread_hash=g_hash_table_new(g_int_hash, g_int_equal);
193 * @security: Ignored for now.
194 * @stacksize: the size in bytes of the new thread's stack. Use 0 to
195 * default to the normal stack size. (Ignored for now).
196 * @start: The function that the new thread should start with
197 * @param: The parameter to give to @start.
198 * @create: If 0, the new thread is ready to run immediately. If
199 * %CREATE_SUSPENDED, the new thread will be in the suspended state,
200 * requiring a ResumeThread() call to continue running.
201 * @tid: If non-NULL, the ID of the new thread is stored here.
203 * Creates a new threading handle.
205 * Return value: a new handle, or NULL
207 gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize,
208 WapiThreadStart start, gpointer param, guint32 create,
211 struct _WapiHandle_thread *thread_handle;
212 struct _WapiHandlePrivate_thread *thread_private_handle;
219 gpointer ct_ret = NULL;
221 mono_once(&thread_hash_once, thread_hash_init);
222 mono_once (&thread_ops_once, thread_ops_init);
228 handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
229 if(handle==_WAPI_HANDLE_INVALID) {
230 g_warning (G_GNUC_PRETTY_FUNCTION
231 ": error creating thread handle");
235 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
237 thr_ret = _wapi_handle_lock_handle (handle);
238 g_assert (thr_ret == 0);
240 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
241 (gpointer *)&thread_handle,
242 (gpointer *)&thread_private_handle);
244 g_warning (G_GNUC_PRETTY_FUNCTION
245 ": error looking up thread handle %p", handle);
249 /* Hold a reference while the thread is active, because we use
250 * the handle to store thread exit information
252 _wapi_handle_ref (handle);
254 thread_handle->state=THREAD_STATE_START;
256 /* Lock around the thread create, so that the new thread cant
257 * race us to look up the thread handle in GetCurrentThread()
259 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
260 (void *)&thread_hash_mutex);
261 thr_ret = mono_mutex_lock(&thread_hash_mutex);
262 g_assert (thr_ret == 0);
264 /* Set a 2M stack size. This is the default on Linux, but BSD
265 * needs it. (The original bug report from Martin Dvorak <md@9ll.cz>
266 * set the size to 2M-4k. I don't know why it's short by 4k, so
267 * I'm leaving it as 2M until I'm told differently.)
269 thr_ret = pthread_attr_init(&attr);
270 g_assert (thr_ret == 0);
272 /* defaults of 2Mb for 32bits and 4Mb for 64bits */
274 #if HAVE_VALGRIND_MEMCHECK_H
275 if (RUNNING_ON_VALGRIND)
278 stacksize = (SIZEOF_VOID_P / 2) * 1024 * 1024;
280 stacksize = (SIZEOF_VOID_P / 2) * 1024 * 1024;
285 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
286 thr_ret = pthread_attr_setstacksize(&attr, stacksize);
287 g_assert (thr_ret == 0);
290 ret=_wapi_timed_thread_create(&thread_private_handle->thread, &attr,
291 create, start, thread_exit, param,
295 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
298 /* Two, because of the reference we took above */
300 goto thread_hash_cleanup;
304 g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
308 g_message(G_GNUC_PRETTY_FUNCTION
309 ": Started thread handle %p thread %p ID %ld", handle,
310 thread_private_handle->thread,
311 thread_private_handle->thread->id);
315 #ifdef PTHREAD_POINTER_ID
316 *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
318 *tid=thread_private_handle->thread->id;
323 thr_ret = mono_mutex_unlock (&thread_hash_mutex);
324 g_assert (thr_ret == 0);
325 pthread_cleanup_pop (0);
328 thr_ret = _wapi_handle_unlock_handle (handle);
329 g_assert (thr_ret == 0);
330 pthread_cleanup_pop (0);
332 /* Must not call _wapi_handle_unref() with the handle already
335 for (i = 0; i < unrefs; i++) {
336 _wapi_handle_unref (handle);
342 gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 tid)
347 mono_once(&thread_hash_once, thread_hash_init);
348 mono_once (&thread_ops_once, thread_ops_init);
351 g_message (G_GNUC_PRETTY_FUNCTION ": looking up thread %d", tid);
354 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
355 (void *)&thread_hash_mutex);
356 thr_ret = mono_mutex_lock(&thread_hash_mutex);
357 g_assert (thr_ret == 0);
359 ret=g_hash_table_lookup(thread_hash, &tid);
361 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
362 g_assert (thr_ret == 0);
363 pthread_cleanup_pop (0);
366 _wapi_handle_ref (ret);
370 g_message (G_GNUC_PRETTY_FUNCTION ": returning thread handle %p", ret);
378 * @exitcode: Sets the thread's exit code, which can be read from
379 * another thread with GetExitCodeThread().
381 * Terminates the calling thread. A thread can also exit by returning
382 * from its start function. When the last thread in a process
383 * terminates, the process itself terminates.
385 void ExitThread(guint32 exitcode)
387 _wapi_timed_thread_exit(exitcode);
392 * @handle: The thread handle to query
393 * @exitcode: The thread @handle exit code is stored here
395 * Finds the exit code of @handle, and stores it in @exitcode. If the
396 * thread @handle is still running, the value stored is %STILL_ACTIVE.
398 * Return value: %TRUE, or %FALSE on error.
400 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
402 struct _WapiHandle_thread *thread_handle;
403 struct _WapiHandlePrivate_thread *thread_private_handle;
406 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
407 (gpointer *)&thread_handle,
408 (gpointer *)&thread_private_handle);
410 g_warning (G_GNUC_PRETTY_FUNCTION
411 ": error looking up thread handle %p", handle);
416 g_message(G_GNUC_PRETTY_FUNCTION
417 ": Finding exit status for thread handle %p id %ld", handle,
418 thread_private_handle->thread->id);
423 g_message(G_GNUC_PRETTY_FUNCTION
424 ": Nowhere to store exit code");
429 if(thread_handle->state!=THREAD_STATE_EXITED) {
431 g_message(G_GNUC_PRETTY_FUNCTION
432 ": Thread still active (state %d, exited is %d)",
433 thread_handle->state, THREAD_STATE_EXITED);
435 *exitcode=STILL_ACTIVE;
439 *exitcode=thread_handle->exitstatus;
445 * GetCurrentThreadId:
447 * Looks up the thread ID of the current thread. This ID can be
448 * passed to OpenThread() to create a new handle on this thread.
450 * Return value: the thread ID.
452 guint32 GetCurrentThreadId(void)
454 pthread_t tid=pthread_self();
456 #ifdef PTHREAD_POINTER_ID
457 return(GPOINTER_TO_UINT(tid));
463 static gpointer thread_attach(guint32 *tid)
465 struct _WapiHandle_thread *thread_handle;
466 struct _WapiHandlePrivate_thread *thread_private_handle;
472 gpointer ta_ret = NULL;
474 mono_once(&thread_hash_once, thread_hash_init);
475 mono_once (&thread_ops_once, thread_ops_init);
477 handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
478 if(handle==_WAPI_HANDLE_INVALID) {
479 g_warning (G_GNUC_PRETTY_FUNCTION
480 ": error creating thread handle");
484 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
486 thr_ret = _wapi_handle_lock_handle (handle);
487 g_assert (thr_ret == 0);
489 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
490 (gpointer *)&thread_handle,
491 (gpointer *)&thread_private_handle);
493 g_warning (G_GNUC_PRETTY_FUNCTION
494 ": error looking up thread handle %p", handle);
498 /* Hold a reference while the thread is active, because we use
499 * the handle to store thread exit information
501 _wapi_handle_ref (handle);
503 thread_handle->state=THREAD_STATE_START;
505 /* Lock around the thread create, so that the new thread cant
506 * race us to look up the thread handle in GetCurrentThread()
508 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
509 (void *)&thread_hash_mutex);
510 thr_ret = mono_mutex_lock(&thread_hash_mutex);
511 g_assert (thr_ret == 0);
513 ret=_wapi_timed_thread_attach(&thread_private_handle->thread,
514 thread_exit, handle);
517 g_message(G_GNUC_PRETTY_FUNCTION ": Thread attach error: %s",
520 /* Two, because of the reference we took above */
523 goto thread_hash_cleanup;
527 g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
531 g_message(G_GNUC_PRETTY_FUNCTION
532 ": Attached thread handle %p thread %p ID %ld", handle,
533 thread_private_handle->thread,
534 thread_private_handle->thread->id);
538 #ifdef PTHREAD_POINTER_ID
539 *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
541 *tid=thread_private_handle->thread->id;
546 thr_ret = mono_mutex_unlock (&thread_hash_mutex);
547 g_assert (thr_ret == 0);
548 pthread_cleanup_pop (0);
551 thr_ret = _wapi_handle_unlock_handle (handle);
552 g_assert (thr_ret == 0);
553 pthread_cleanup_pop (0);
555 /* Must not call _wapi_handle_unref() with the handle already
558 for (i = 0; i < unrefs; i++) {
559 _wapi_handle_unref (handle);
568 * Looks up the handle associated with the current thread. Under
569 * Windows this is a pseudohandle, and must be duplicated with
570 * DuplicateHandle() for some operations.
572 * Return value: The current thread handle, or %NULL on failure.
573 * (Unknown whether Windows has a possible failure here. It may be
574 * necessary to implement the pseudohandle-constant behaviour).
576 gpointer GetCurrentThread(void)
582 mono_once(&thread_hash_once, thread_hash_init);
583 mono_once (&thread_ops_once, thread_ops_init);
585 tid=GetCurrentThreadId();
587 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
588 (void *)&thread_hash_mutex);
589 thr_ret = mono_mutex_lock(&thread_hash_mutex);
590 g_assert (thr_ret == 0);
592 ret=g_hash_table_lookup(thread_hash, &tid);
594 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
595 g_assert (thr_ret == 0);
596 pthread_cleanup_pop (0);
599 ret = thread_attach (NULL);
607 * @handle: the thread handle to resume
609 * Decrements the suspend count of thread @handle. A thread can only
610 * run if its suspend count is zero.
612 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
614 guint32 ResumeThread(gpointer handle)
616 struct _WapiHandle_thread *thread_handle;
617 struct _WapiHandlePrivate_thread *thread_private_handle;
620 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
621 (gpointer *)&thread_handle,
622 (gpointer *)&thread_private_handle);
624 g_warning (G_GNUC_PRETTY_FUNCTION
625 ": error looking up thread handle %p", handle);
629 if (thread_private_handle->thread == NULL) {
633 #ifdef WITH_INCLUDED_LIBGC
634 if (thread_private_handle->thread->suspend_count <= 1)
635 _wapi_timed_thread_resume (thread_private_handle->thread);
637 return --thread_private_handle->thread->suspend_count;
639 /* This is still a kludge that only copes with starting a
640 * thread that was suspended on create, so don't bother with
641 * the suspend count crap yet
643 _wapi_timed_thread_resume (thread_private_handle->thread);
650 * @handle: the thread handle to suspend
652 * Increments the suspend count of thread @handle. A thread can only
653 * run if its suspend count is zero.
655 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
657 guint32 SuspendThread(gpointer handle)
659 #ifdef WITH_INCLUDED_LIBGC
660 struct _WapiHandle_thread *thread_handle;
661 struct _WapiHandlePrivate_thread *thread_private_handle;
665 current = GetCurrentThread ();
666 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
667 (gpointer *)&thread_handle,
668 (gpointer *)&thread_private_handle);
670 g_warning (G_GNUC_PRETTY_FUNCTION
671 ": error looking up thread handle %p", handle);
675 if (thread_private_handle->thread == NULL) {
679 if (!thread_private_handle->thread->suspend_count) {
680 if (handle == current)
681 _wapi_timed_thread_suspend (thread_private_handle->thread);
683 pthread_kill (thread_private_handle->thread->id, SIGPWR);
684 while (MONO_SEM_WAIT (&thread_private_handle->thread->suspended_sem) != 0) {
685 if (errno != EINTR) {
692 return thread_private_handle->thread->suspend_count++;
699 * We assume here that TLS_MINIMUM_AVAILABLE is less than
700 * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
703 * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
704 * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
708 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
709 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
710 static guint32 TLS_spinlock=0;
715 * Allocates a Thread Local Storage (TLS) index. Any thread in the
716 * same process can use this index to store and retrieve values that
717 * are local to that thread.
719 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
722 guint32 TlsAlloc(void)
727 MONO_SPIN_LOCK (TLS_spinlock);
729 for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
730 if(TLS_used[i]==FALSE) {
732 thr_ret = pthread_key_create(&TLS_keys[i], NULL);
733 g_assert (thr_ret == 0);
735 MONO_SPIN_UNLOCK (TLS_spinlock);
738 g_message (G_GNUC_PRETTY_FUNCTION ": returning key %d",
746 MONO_SPIN_UNLOCK (TLS_spinlock);
749 g_message (G_GNUC_PRETTY_FUNCTION ": out of indices");
753 return(TLS_OUT_OF_INDEXES);
756 #define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
760 * @idx: The TLS index to free
762 * Releases a TLS index, making it available for reuse. This call
763 * will delete any TLS data stored under index @idx in all threads.
765 * Return value: %TRUE on success, %FALSE otherwise.
767 gboolean TlsFree(guint32 idx)
772 g_message (G_GNUC_PRETTY_FUNCTION ": freeing key %d", idx);
775 MONO_SPIN_LOCK (TLS_spinlock);
777 if(TLS_used[idx]==FALSE) {
778 MONO_SPIN_UNLOCK (TLS_spinlock);
784 thr_ret = pthread_key_delete(TLS_keys[idx]);
785 g_assert (thr_ret == 0);
788 mono_g_hash_table_remove (tls_gc_hash, MAKE_GC_ID (idx));
791 MONO_SPIN_UNLOCK (TLS_spinlock);
798 * @idx: The TLS index to retrieve
800 * Retrieves the TLS data stored under index @idx.
802 * Return value: The value stored in the TLS index @idx in the current
803 * thread, or %NULL on error. As %NULL can be a valid return value,
804 * in this case GetLastError() returns %ERROR_SUCCESS.
806 gpointer TlsGetValue(guint32 idx)
811 g_message (G_GNUC_PRETTY_FUNCTION ": looking up key %d", idx);
814 ret=pthread_getspecific(TLS_keys[idx]);
817 g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", ret);
825 * @idx: The TLS index to store
826 * @value: The value to store under index @idx
828 * Stores @value at TLS index @idx.
830 * Return value: %TRUE on success, %FALSE otherwise.
832 gboolean TlsSetValue(guint32 idx, gpointer value)
837 g_message (G_GNUC_PRETTY_FUNCTION ": setting key %d to %p", idx,
841 MONO_SPIN_LOCK (TLS_spinlock);
843 if(TLS_used[idx]==FALSE) {
845 g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
848 MONO_SPIN_UNLOCK (TLS_spinlock);
853 ret=pthread_setspecific(TLS_keys[idx], value);
856 g_message (G_GNUC_PRETTY_FUNCTION
857 ": pthread_setspecific error: %s", strerror (ret));
860 MONO_SPIN_UNLOCK (TLS_spinlock);
867 tls_gc_hash = mono_g_hash_table_new(g_direct_hash, g_direct_equal);
868 mono_g_hash_table_insert (tls_gc_hash, MAKE_GC_ID (idx), value);
871 MONO_SPIN_UNLOCK (TLS_spinlock);
878 * @ms: The time in milliseconds to suspend for
879 * @alertable: if TRUE, the wait can be interrupted by an APC call
881 * Suspends execution of the current thread for @ms milliseconds. A
882 * value of zero causes the thread to relinquish its time slice. A
883 * value of %INFINITE causes an infinite delay.
885 guint32 SleepEx(guint32 ms, gboolean alertable)
887 struct timespec req, rem;
890 gpointer current_thread = NULL;
893 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
897 current_thread = GetCurrentThread ();
898 if (_wapi_thread_apc_pending (current_thread)) {
899 _wapi_thread_dispatch_apc_queue (current_thread);
900 return WAIT_IO_COMPLETION;
909 /* FIXME: check for INFINITE and sleep forever */
914 req.tv_nsec=ms_rem*1000000;
917 ret=nanosleep(&req, &rem);
919 if (alertable && _wapi_thread_apc_pending (current_thread)) {
920 _wapi_thread_dispatch_apc_queue (current_thread);
921 return WAIT_IO_COMPLETION;
925 /* Sleep interrupted with rem time remaining */
927 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
929 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
939 void Sleep(guint32 ms)
945 BindIoCompletionCallback (gpointer handle,
946 WapiOverlappedCB callback,
951 type = _wapi_handle_type (handle);
952 if (type == WAPI_HANDLE_FILE || type == WAPI_HANDLE_PIPE)
953 return _wapi_io_add_callback (handle, callback, flags);
955 SetLastError (ERROR_NOT_SUPPORTED);
959 guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle,
962 struct _WapiHandle_thread *thread_handle;
963 struct _WapiHandlePrivate_thread *thread_private_handle;
966 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
967 (gpointer *)&thread_handle,
968 (gpointer *)&thread_private_handle);
970 g_warning (G_GNUC_PRETTY_FUNCTION
971 ": error looking up thread handle %p", handle);
975 _wapi_timed_thread_queue_apc (thread_private_handle->thread,
976 apc_callback, param);
980 gboolean _wapi_thread_cur_apc_pending (void)
982 return _wapi_thread_apc_pending (GetCurrentThread ());
985 gboolean _wapi_thread_apc_pending (gpointer handle)
987 struct _WapiHandle_thread *thread_handle;
988 struct _WapiHandlePrivate_thread *thread_private_handle;
991 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
992 (gpointer *)&thread_handle,
993 (gpointer *)&thread_private_handle);
995 g_warning (G_GNUC_PRETTY_FUNCTION
996 ": error looking up thread handle %p", handle);
1000 return _wapi_timed_thread_apc_pending (thread_private_handle->thread);
1003 gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
1005 struct _WapiHandle_thread *thread_handle;
1006 struct _WapiHandlePrivate_thread *thread_private_handle;
1009 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1010 (gpointer *)&thread_handle,
1011 (gpointer *)&thread_private_handle);
1013 g_warning (G_GNUC_PRETTY_FUNCTION
1014 ": error looking up thread handle %p", handle);
1018 _wapi_timed_thread_dispatch_apc_queue (thread_private_handle->thread);
1024 #ifdef WITH_INCLUDED_LIBGC
1026 static void GC_suspend_handler (int sig)
1028 struct _WapiHandle_thread *thread_handle;
1029 struct _WapiHandlePrivate_thread *thread_private_handle;
1033 handle = GetCurrentThread ();
1034 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1035 (gpointer *)&thread_handle,
1036 (gpointer *)&thread_private_handle);
1038 g_warning (G_GNUC_PRETTY_FUNCTION
1039 ": error looking up thread handle %p", handle);
1043 thread_private_handle->thread->stack_ptr = &ok;
1044 MONO_SEM_POST (&thread_private_handle->thread->suspended_sem);
1046 _wapi_timed_thread_suspend (thread_private_handle->thread);
1048 thread_private_handle->thread->stack_ptr = NULL;
1051 static void gc_init (void)
1053 struct sigaction act;
1055 act.sa_handler = GC_suspend_handler;
1056 g_assert (sigaction (SIGPWR, &act, NULL) == 0);
1059 void mono_wapi_push_thread_stack (gpointer handle, gpointer stack_ptr)
1061 struct _WapiHandle_thread *thread_handle;
1062 struct _WapiHandlePrivate_thread *thread_private_handle;
1065 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1066 (gpointer *)&thread_handle,
1067 (gpointer *)&thread_private_handle);
1069 g_warning (G_GNUC_PRETTY_FUNCTION
1070 ": error looking up thread handle %p", handle);
1074 GC_push_all_stack (thread_private_handle->thread->stack_ptr, stack_ptr);
1077 #endif /* WITH_INCLUDED_LIBGC */