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"
24 #include <mono/io-layer/wapi.h>
25 #include <mono/io-layer/wapi-private.h>
26 #include <mono/io-layer/timed-thread.h>
27 #include <mono/io-layer/handles-private.h>
28 #include <mono/io-layer/misc-private.h>
29 #include <mono/io-layer/mono-mutex.h>
30 #include <mono/io-layer/thread-private.h>
31 #include <mono/io-layer/mono-spinlock.h>
37 /* Hash threads with tids. I thought of using TLS for this, but that
38 * would have to set the data in the new thread, which is more hassle
40 static mono_once_t thread_hash_once = MONO_ONCE_INIT;
41 static mono_mutex_t thread_hash_mutex = MONO_MUTEX_INITIALIZER;
42 static GHashTable *thread_hash=NULL;
45 static MonoGHashTable *tls_gc_hash = NULL;
48 static void thread_close_private (gpointer handle);
49 static void thread_own (gpointer handle);
51 struct _WapiHandleOps _wapi_thread_ops = {
52 NULL, /* close_shared */
53 thread_close_private, /* close_private */
59 static mono_once_t thread_ops_once=MONO_ONCE_INIT;
61 #ifdef WITH_INCLUDED_LIBGC
62 static void gc_init (void);
65 static void thread_ops_init (void)
67 _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD,
68 WAPI_HANDLE_CAP_WAIT);
70 #ifdef WITH_INCLUDED_LIBGC
75 static void thread_close_private (gpointer handle)
77 struct _WapiHandlePrivate_thread *thread_handle;
80 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, NULL,
81 (gpointer *)&thread_handle);
83 g_warning (G_GNUC_PRETTY_FUNCTION
84 ": error looking up thread handle %p", handle);
89 g_message(G_GNUC_PRETTY_FUNCTION
90 ": closing thread handle %p with thread %p id %ld",
91 handle, thread_handle->thread,
92 thread_handle->thread->id);
95 if(thread_handle->thread!=NULL) {
96 _wapi_timed_thread_destroy (thread_handle->thread);
100 static void thread_own (gpointer handle)
102 struct _WapiHandle_thread *thread_handle;
103 struct _WapiHandlePrivate_thread *thread_private_handle;
106 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
107 (gpointer *)&thread_handle,
108 (gpointer *)&thread_private_handle);
110 g_warning (G_GNUC_PRETTY_FUNCTION
111 ": error looking up thread handle %p", handle);
115 if(thread_private_handle->joined==FALSE) {
116 _wapi_timed_thread_join (thread_private_handle->thread, NULL,
118 thread_private_handle->joined=TRUE;
122 static void thread_exit(guint32 exitstatus, gpointer handle)
124 struct _WapiHandle_thread *thread_handle;
125 struct _WapiHandlePrivate_thread *thread_private_handle;
129 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
130 (gpointer *)&thread_handle,
131 (gpointer *)&thread_private_handle);
133 g_warning (G_GNUC_PRETTY_FUNCTION
134 ": error looking up thread handle %p", handle);
138 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
140 thr_ret = _wapi_handle_lock_handle (handle);
141 g_assert (thr_ret == 0);
144 g_message (G_GNUC_PRETTY_FUNCTION
145 ": Recording thread handle %p exit status", handle);
148 thread_handle->exitstatus=exitstatus;
149 thread_handle->state=THREAD_STATE_EXITED;
150 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
152 thr_ret = _wapi_handle_unlock_handle (handle);
153 g_assert (thr_ret == 0);
154 pthread_cleanup_pop (0);
157 g_message(G_GNUC_PRETTY_FUNCTION
158 ": Recording thread handle %p id %ld status as %d",
159 handle, thread_private_handle->thread->id, exitstatus);
162 /* Remove this thread from the hash */
163 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
164 (void *)&thread_hash_mutex);
165 thr_ret = mono_mutex_lock(&thread_hash_mutex);
166 g_assert (thr_ret == 0);
168 g_hash_table_remove(thread_hash, &thread_private_handle->thread->id);
170 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
171 g_assert (thr_ret == 0);
172 pthread_cleanup_pop (0);
174 /* The thread is no longer active, so unref it */
175 _wapi_handle_unref (handle);
178 static void thread_hash_init(void)
180 thread_hash=g_hash_table_new(g_int_hash, g_int_equal);
185 * @security: Ignored for now.
186 * @stacksize: the size in bytes of the new thread's stack. Use 0 to
187 * default to the normal stack size. (Ignored for now).
188 * @start: The function that the new thread should start with
189 * @param: The parameter to give to @start.
190 * @create: If 0, the new thread is ready to run immediately. If
191 * %CREATE_SUSPENDED, the new thread will be in the suspended state,
192 * requiring a ResumeThread() call to continue running.
193 * @tid: If non-NULL, the ID of the new thread is stored here.
195 * Creates a new threading handle.
197 * Return value: a new handle, or NULL
199 gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize,
200 WapiThreadStart start, gpointer param, guint32 create,
203 struct _WapiHandle_thread *thread_handle;
204 struct _WapiHandlePrivate_thread *thread_private_handle;
211 gpointer ct_ret = NULL;
213 mono_once(&thread_hash_once, thread_hash_init);
214 mono_once (&thread_ops_once, thread_ops_init);
220 handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
221 if(handle==_WAPI_HANDLE_INVALID) {
222 g_warning (G_GNUC_PRETTY_FUNCTION
223 ": error creating thread handle");
227 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
229 thr_ret = _wapi_handle_lock_handle (handle);
230 g_assert (thr_ret == 0);
232 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
233 (gpointer *)&thread_handle,
234 (gpointer *)&thread_private_handle);
236 g_warning (G_GNUC_PRETTY_FUNCTION
237 ": error looking up thread handle %p", handle);
241 /* Hold a reference while the thread is active, because we use
242 * the handle to store thread exit information
244 _wapi_handle_ref (handle);
246 thread_handle->state=THREAD_STATE_START;
248 /* Lock around the thread create, so that the new thread cant
249 * race us to look up the thread handle in GetCurrentThread()
251 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
252 (void *)&thread_hash_mutex);
253 thr_ret = mono_mutex_lock(&thread_hash_mutex);
254 g_assert (thr_ret == 0);
256 /* Set a 2M stack size. This is the default on Linux, but BSD
257 * needs it. (The original bug report from Martin Dvorak <md@9ll.cz>
258 * set the size to 2M-4k. I don't know why it's short by 4k, so
259 * I'm leaving it as 2M until I'm told differently.)
261 thr_ret = pthread_attr_init(&attr);
262 g_assert (thr_ret == 0);
264 /* defaults of 2Mb for 32bits and 4Mb for 64bits */
266 stacksize = (SIZEOF_VOID_P / 2) * 1024 *1024;
268 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
269 thr_ret = pthread_attr_setstacksize(&attr, stacksize);
270 g_assert (thr_ret == 0);
273 ret=_wapi_timed_thread_create(&thread_private_handle->thread, &attr,
274 create, start, thread_exit, param,
278 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
281 /* Two, because of the reference we took above */
283 goto thread_hash_cleanup;
287 g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
291 g_message(G_GNUC_PRETTY_FUNCTION
292 ": Started thread handle %p thread %p ID %ld", handle,
293 thread_private_handle->thread,
294 thread_private_handle->thread->id);
298 #ifdef PTHREAD_POINTER_ID
299 *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
301 *tid=thread_private_handle->thread->id;
306 thr_ret = mono_mutex_unlock (&thread_hash_mutex);
307 g_assert (thr_ret == 0);
308 pthread_cleanup_pop (0);
311 thr_ret = _wapi_handle_unlock_handle (handle);
312 g_assert (thr_ret == 0);
313 pthread_cleanup_pop (0);
315 /* Must not call _wapi_handle_unref() with the handle already
318 for (i = 0; i < unrefs; i++) {
319 _wapi_handle_unref (handle);
325 gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 tid)
330 mono_once(&thread_hash_once, thread_hash_init);
331 mono_once (&thread_ops_once, thread_ops_init);
334 g_message (G_GNUC_PRETTY_FUNCTION ": looking up thread %d", tid);
337 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
338 (void *)&thread_hash_mutex);
339 thr_ret = mono_mutex_lock(&thread_hash_mutex);
340 g_assert (thr_ret == 0);
342 ret=g_hash_table_lookup(thread_hash, &tid);
344 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
345 g_assert (thr_ret == 0);
346 pthread_cleanup_pop (0);
349 _wapi_handle_ref (ret);
353 g_message (G_GNUC_PRETTY_FUNCTION ": returning thread handle %p", ret);
361 * @exitcode: Sets the thread's exit code, which can be read from
362 * another thread with GetExitCodeThread().
364 * Terminates the calling thread. A thread can also exit by returning
365 * from its start function. When the last thread in a process
366 * terminates, the process itself terminates.
368 void ExitThread(guint32 exitcode)
370 _wapi_timed_thread_exit(exitcode);
375 * @handle: The thread handle to query
376 * @exitcode: The thread @handle exit code is stored here
378 * Finds the exit code of @handle, and stores it in @exitcode. If the
379 * thread @handle is still running, the value stored is %STILL_ACTIVE.
381 * Return value: %TRUE, or %FALSE on error.
383 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
385 struct _WapiHandle_thread *thread_handle;
386 struct _WapiHandlePrivate_thread *thread_private_handle;
389 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
390 (gpointer *)&thread_handle,
391 (gpointer *)&thread_private_handle);
393 g_warning (G_GNUC_PRETTY_FUNCTION
394 ": error looking up thread handle %p", handle);
399 g_message(G_GNUC_PRETTY_FUNCTION
400 ": Finding exit status for thread handle %p id %ld", handle,
401 thread_private_handle->thread->id);
406 g_message(G_GNUC_PRETTY_FUNCTION
407 ": Nowhere to store exit code");
412 if(thread_handle->state!=THREAD_STATE_EXITED) {
414 g_message(G_GNUC_PRETTY_FUNCTION
415 ": Thread still active (state %d, exited is %d)",
416 thread_handle->state, THREAD_STATE_EXITED);
418 *exitcode=STILL_ACTIVE;
422 *exitcode=thread_handle->exitstatus;
428 * GetCurrentThreadId:
430 * Looks up the thread ID of the current thread. This ID can be
431 * passed to OpenThread() to create a new handle on this thread.
433 * Return value: the thread ID.
435 guint32 GetCurrentThreadId(void)
437 pthread_t tid=pthread_self();
439 #ifdef PTHREAD_POINTER_ID
440 return(GPOINTER_TO_UINT(tid));
446 static gpointer thread_attach(guint32 *tid)
448 struct _WapiHandle_thread *thread_handle;
449 struct _WapiHandlePrivate_thread *thread_private_handle;
455 gpointer ta_ret = NULL;
457 mono_once(&thread_hash_once, thread_hash_init);
458 mono_once (&thread_ops_once, thread_ops_init);
460 handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
461 if(handle==_WAPI_HANDLE_INVALID) {
462 g_warning (G_GNUC_PRETTY_FUNCTION
463 ": error creating thread handle");
467 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
469 thr_ret = _wapi_handle_lock_handle (handle);
470 g_assert (thr_ret == 0);
472 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
473 (gpointer *)&thread_handle,
474 (gpointer *)&thread_private_handle);
476 g_warning (G_GNUC_PRETTY_FUNCTION
477 ": error looking up thread handle %p", handle);
481 /* Hold a reference while the thread is active, because we use
482 * the handle to store thread exit information
484 _wapi_handle_ref (handle);
486 thread_handle->state=THREAD_STATE_START;
488 /* Lock around the thread create, so that the new thread cant
489 * race us to look up the thread handle in GetCurrentThread()
491 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
492 (void *)&thread_hash_mutex);
493 thr_ret = mono_mutex_lock(&thread_hash_mutex);
494 g_assert (thr_ret == 0);
496 ret=_wapi_timed_thread_attach(&thread_private_handle->thread,
497 thread_exit, handle);
500 g_message(G_GNUC_PRETTY_FUNCTION ": Thread attach error: %s",
503 /* Two, because of the reference we took above */
506 goto thread_hash_cleanup;
510 g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
514 g_message(G_GNUC_PRETTY_FUNCTION
515 ": Attached thread handle %p thread %p ID %ld", handle,
516 thread_private_handle->thread,
517 thread_private_handle->thread->id);
521 #ifdef PTHREAD_POINTER_ID
522 *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
524 *tid=thread_private_handle->thread->id;
529 thr_ret = mono_mutex_unlock (&thread_hash_mutex);
530 g_assert (thr_ret == 0);
531 pthread_cleanup_pop (0);
534 thr_ret = _wapi_handle_unlock_handle (handle);
535 g_assert (thr_ret == 0);
536 pthread_cleanup_pop (0);
538 /* Must not call _wapi_handle_unref() with the handle already
541 for (i = 0; i < unrefs; i++) {
542 _wapi_handle_unref (handle);
551 * Looks up the handle associated with the current thread. Under
552 * Windows this is a pseudohandle, and must be duplicated with
553 * DuplicateHandle() for some operations.
555 * Return value: The current thread handle, or %NULL on failure.
556 * (Unknown whether Windows has a possible failure here. It may be
557 * necessary to implement the pseudohandle-constant behaviour).
559 gpointer GetCurrentThread(void)
565 mono_once(&thread_hash_once, thread_hash_init);
566 mono_once (&thread_ops_once, thread_ops_init);
568 tid=GetCurrentThreadId();
570 pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
571 (void *)&thread_hash_mutex);
572 thr_ret = mono_mutex_lock(&thread_hash_mutex);
573 g_assert (thr_ret == 0);
575 ret=g_hash_table_lookup(thread_hash, &tid);
577 thr_ret = mono_mutex_unlock(&thread_hash_mutex);
578 g_assert (thr_ret == 0);
579 pthread_cleanup_pop (0);
582 ret = thread_attach (NULL);
590 * @handle: the thread handle to resume
592 * Decrements the suspend count of thread @handle. A thread can only
593 * run if its suspend count is zero.
595 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
597 guint32 ResumeThread(gpointer handle)
599 struct _WapiHandle_thread *thread_handle;
600 struct _WapiHandlePrivate_thread *thread_private_handle;
603 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
604 (gpointer *)&thread_handle,
605 (gpointer *)&thread_private_handle);
607 g_warning (G_GNUC_PRETTY_FUNCTION
608 ": error looking up thread handle %p", handle);
612 #ifdef WITH_INCLUDED_LIBGC
613 if (thread_private_handle->thread->suspend_count <= 1)
614 _wapi_timed_thread_resume (thread_private_handle->thread);
616 return --thread_private_handle->thread->suspend_count;
618 /* This is still a kludge that only copes with starting a
619 * thread that was suspended on create, so don't bother with
620 * the suspend count crap yet
622 _wapi_timed_thread_resume (thread_private_handle->thread);
629 * @handle: the thread handle to suspend
631 * Increments the suspend count of thread @handle. A thread can only
632 * run if its suspend count is zero.
634 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
636 guint32 SuspendThread(gpointer handle)
638 #ifdef WITH_INCLUDED_LIBGC
639 struct _WapiHandle_thread *thread_handle;
640 struct _WapiHandlePrivate_thread *thread_private_handle;
644 current = GetCurrentThread ();
645 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
646 (gpointer *)&thread_handle,
647 (gpointer *)&thread_private_handle);
649 g_warning (G_GNUC_PRETTY_FUNCTION
650 ": error looking up thread handle %p", handle);
654 if (!thread_private_handle->thread->suspend_count) {
655 if (handle == current)
656 _wapi_timed_thread_suspend (thread_private_handle->thread);
658 pthread_kill (thread_private_handle->thread->id, SIGPWR);
659 MONO_SEM_WAIT (&thread_private_handle->thread->suspended_sem);
663 return thread_private_handle->thread->suspend_count++;
670 * We assume here that TLS_MINIMUM_AVAILABLE is less than
671 * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
674 * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
675 * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
679 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
680 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
681 static guint32 TLS_spinlock=0;
686 * Allocates a Thread Local Storage (TLS) index. Any thread in the
687 * same process can use this index to store and retrieve values that
688 * are local to that thread.
690 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
693 guint32 TlsAlloc(void)
698 MONO_SPIN_LOCK (TLS_spinlock);
700 for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
701 if(TLS_used[i]==FALSE) {
703 thr_ret = pthread_key_create(&TLS_keys[i], NULL);
704 g_assert (thr_ret == 0);
706 MONO_SPIN_UNLOCK (TLS_spinlock);
709 g_message (G_GNUC_PRETTY_FUNCTION ": returning key %d",
717 MONO_SPIN_UNLOCK (TLS_spinlock);
720 g_message (G_GNUC_PRETTY_FUNCTION ": out of indices");
724 return(TLS_OUT_OF_INDEXES);
727 #define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
731 * @idx: The TLS index to free
733 * Releases a TLS index, making it available for reuse. This call
734 * will delete any TLS data stored under index @idx in all threads.
736 * Return value: %TRUE on success, %FALSE otherwise.
738 gboolean TlsFree(guint32 idx)
743 g_message (G_GNUC_PRETTY_FUNCTION ": freeing key %d", idx);
746 MONO_SPIN_LOCK (TLS_spinlock);
748 if(TLS_used[idx]==FALSE) {
749 MONO_SPIN_UNLOCK (TLS_spinlock);
755 thr_ret = pthread_key_delete(TLS_keys[idx]);
756 g_assert (thr_ret == 0);
759 mono_g_hash_table_remove (tls_gc_hash, MAKE_GC_ID (idx));
762 MONO_SPIN_UNLOCK (TLS_spinlock);
769 * @idx: The TLS index to retrieve
771 * Retrieves the TLS data stored under index @idx.
773 * Return value: The value stored in the TLS index @idx in the current
774 * thread, or %NULL on error. As %NULL can be a valid return value,
775 * in this case GetLastError() returns %ERROR_SUCCESS.
777 gpointer TlsGetValue(guint32 idx)
782 g_message (G_GNUC_PRETTY_FUNCTION ": looking up key %d", idx);
785 ret=pthread_getspecific(TLS_keys[idx]);
788 g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", ret);
796 * @idx: The TLS index to store
797 * @value: The value to store under index @idx
799 * Stores @value at TLS index @idx.
801 * Return value: %TRUE on success, %FALSE otherwise.
803 gboolean TlsSetValue(guint32 idx, gpointer value)
808 g_message (G_GNUC_PRETTY_FUNCTION ": setting key %d to %p", idx,
812 MONO_SPIN_LOCK (TLS_spinlock);
814 if(TLS_used[idx]==FALSE) {
816 g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
819 MONO_SPIN_UNLOCK (TLS_spinlock);
824 ret=pthread_setspecific(TLS_keys[idx], value);
827 g_message (G_GNUC_PRETTY_FUNCTION
828 ": pthread_setspecific error: %s", strerror (ret));
831 MONO_SPIN_UNLOCK (TLS_spinlock);
838 tls_gc_hash = mono_g_hash_table_new(g_direct_hash, g_direct_equal);
839 mono_g_hash_table_insert (tls_gc_hash, MAKE_GC_ID (idx), value);
842 MONO_SPIN_UNLOCK (TLS_spinlock);
849 * @ms: The time in milliseconds to suspend for
851 * Suspends execution of the current thread for @ms milliseconds. A
852 * value of zero causes the thread to relinquish its time slice. A
853 * value of %INFINITE causes an infinite delay.
855 void Sleep(guint32 ms)
857 struct timespec req, rem;
862 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
870 /* FIXME: check for INFINITE and sleep forever */
875 req.tv_nsec=ms_rem*1000000;
878 ret=nanosleep(&req, &rem);
880 /* Sleep interrupted with rem time remaining */
882 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
884 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
892 /* FIXME: implement alertable */
893 void SleepEx(guint32 ms, gboolean alertable)
895 if(alertable==TRUE) {
896 g_warning(G_GNUC_PRETTY_FUNCTION ": alertable not implemented");
903 BindIoCompletionCallback (gpointer handle,
904 WapiOverlappedCB callback,
909 type = _wapi_handle_type (handle);
910 if (type == WAPI_HANDLE_FILE || type == WAPI_HANDLE_PIPE)
911 return _wapi_io_add_callback (handle, callback, flags);
913 SetLastError (ERROR_NOT_SUPPORTED);
917 guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle,
920 struct _WapiHandle_thread *thread_handle;
921 struct _WapiHandlePrivate_thread *thread_private_handle;
924 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
925 (gpointer *)&thread_handle,
926 (gpointer *)&thread_private_handle);
928 g_warning (G_GNUC_PRETTY_FUNCTION
929 ": error looking up thread handle %p", handle);
933 _wapi_timed_thread_queue_apc (thread_private_handle->thread,
934 apc_callback, param);
938 gboolean _wapi_thread_cur_apc_pending (void)
940 return _wapi_thread_apc_pending (GetCurrentThread ());
943 gboolean _wapi_thread_apc_pending (gpointer handle)
945 struct _WapiHandle_thread *thread_handle;
946 struct _WapiHandlePrivate_thread *thread_private_handle;
949 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
950 (gpointer *)&thread_handle,
951 (gpointer *)&thread_private_handle);
953 g_warning (G_GNUC_PRETTY_FUNCTION
954 ": error looking up thread handle %p", handle);
958 return _wapi_timed_thread_apc_pending (thread_private_handle->thread);
961 gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
963 struct _WapiHandle_thread *thread_handle;
964 struct _WapiHandlePrivate_thread *thread_private_handle;
967 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
968 (gpointer *)&thread_handle,
969 (gpointer *)&thread_private_handle);
971 g_warning (G_GNUC_PRETTY_FUNCTION
972 ": error looking up thread handle %p", handle);
976 _wapi_timed_thread_dispatch_apc_queue (thread_private_handle->thread);
982 #ifdef WITH_INCLUDED_LIBGC
984 static void GC_suspend_handler (int sig)
986 struct _WapiHandle_thread *thread_handle;
987 struct _WapiHandlePrivate_thread *thread_private_handle;
991 handle = GetCurrentThread ();
992 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
993 (gpointer *)&thread_handle,
994 (gpointer *)&thread_private_handle);
996 g_warning (G_GNUC_PRETTY_FUNCTION
997 ": error looking up thread handle %p", handle);
1001 thread_private_handle->thread->stack_ptr = &ok;
1002 MONO_SEM_POST (&thread_private_handle->thread->suspended_sem);
1004 _wapi_timed_thread_suspend (thread_private_handle->thread);
1006 thread_private_handle->thread->stack_ptr = NULL;
1009 static void gc_init (void)
1011 struct sigaction act;
1013 act.sa_handler = GC_suspend_handler;
1014 g_assert (sigaction (SIGPWR, &act, NULL) == 0);
1017 void mono_wapi_push_thread_stack (gpointer handle, gpointer stack_ptr)
1019 struct _WapiHandle_thread *thread_handle;
1020 struct _WapiHandlePrivate_thread *thread_private_handle;
1023 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1024 (gpointer *)&thread_handle,
1025 (gpointer *)&thread_private_handle);
1027 g_warning (G_GNUC_PRETTY_FUNCTION
1028 ": error looking up thread handle %p", handle);
1032 GC_push_all_stack (thread_private_handle->thread->stack_ptr, stack_ptr);
1035 #endif /* WITH_INCLUDED_LIBGC */