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 <mono/io-layer/wapi.h>
24 #include <mono/io-layer/wapi-private.h>
25 #include <mono/io-layer/timed-thread.h>
26 #include <mono/io-layer/handles-private.h>
27 #include <mono/io-layer/misc-private.h>
28 #include <mono/io-layer/mono-mutex.h>
29 #include <mono/io-layer/thread-private.h>
30 #include <mono/io-layer/mono-spinlock.h>
34 #undef TLS_PTHREAD_MUTEX
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;
128 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
129 (gpointer *)&thread_handle,
130 (gpointer *)&thread_private_handle);
132 g_warning (G_GNUC_PRETTY_FUNCTION
133 ": error looking up thread handle %p", handle);
137 _wapi_handle_lock_handle (handle);
140 g_message (G_GNUC_PRETTY_FUNCTION
141 ": Recording thread handle %p exit status", handle);
144 thread_handle->exitstatus=exitstatus;
145 thread_handle->state=THREAD_STATE_EXITED;
146 _wapi_handle_set_signal_state (handle, TRUE, TRUE);
148 _wapi_handle_unlock_handle (handle);
151 g_message(G_GNUC_PRETTY_FUNCTION
152 ": Recording thread handle %p id %ld status as %d",
153 handle, thread_private_handle->thread->id, exitstatus);
156 /* Remove this thread from the hash */
157 mono_mutex_lock(&thread_hash_mutex);
158 g_hash_table_remove(thread_hash, &thread_private_handle->thread->id);
159 mono_mutex_unlock(&thread_hash_mutex);
161 /* The thread is no longer active, so unref it */
162 _wapi_handle_unref (handle);
165 static void thread_hash_init(void)
167 thread_hash=g_hash_table_new(g_int_hash, g_int_equal);
172 * @security: Ignored for now.
173 * @stacksize: the size in bytes of the new thread's stack. Use 0 to
174 * default to the normal stack size. (Ignored for now).
175 * @start: The function that the new thread should start with
176 * @param: The parameter to give to @start.
177 * @create: If 0, the new thread is ready to run immediately. If
178 * %CREATE_SUSPENDED, the new thread will be in the suspended state,
179 * requiring a ResumeThread() call to continue running.
180 * @tid: If non-NULL, the ID of the new thread is stored here.
182 * Creates a new threading handle.
184 * Return value: a new handle, or NULL
186 gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize,
187 WapiThreadStart start, gpointer param, guint32 create,
190 struct _WapiHandle_thread *thread_handle;
191 struct _WapiHandlePrivate_thread *thread_private_handle;
197 mono_once(&thread_hash_once, thread_hash_init);
198 mono_once (&thread_ops_once, thread_ops_init);
204 handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
205 if(handle==_WAPI_HANDLE_INVALID) {
206 g_warning (G_GNUC_PRETTY_FUNCTION
207 ": error creating thread handle");
211 _wapi_handle_lock_handle (handle);
213 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
214 (gpointer *)&thread_handle,
215 (gpointer *)&thread_private_handle);
217 g_warning (G_GNUC_PRETTY_FUNCTION
218 ": error looking up thread handle %p", handle);
219 _wapi_handle_unlock_handle (handle);
223 /* Hold a reference while the thread is active, because we use
224 * the handle to store thread exit information
226 _wapi_handle_ref (handle);
228 thread_handle->state=THREAD_STATE_START;
230 /* Lock around the thread create, so that the new thread cant
231 * race us to look up the thread handle in GetCurrentThread()
233 mono_mutex_lock(&thread_hash_mutex);
235 /* Set a 2M stack size. This is the default on Linux, but BSD
236 * needs it. (The original bug report from Martin Dvorak <md@9ll.cz>
237 * set the size to 2M-4k. I don't know why it's short by 4k, so
238 * I'm leaving it as 2M until I'm told differently.)
240 pthread_attr_init(&attr);
241 /* defaults of 2Mb for 32bits and 4Mb for 64bits */
243 stacksize = (SIZEOF_VOID_P / 2) * 1024 *1024;
245 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
246 pthread_attr_setstacksize(&attr, stacksize);
249 ret=_wapi_timed_thread_create(&thread_private_handle->thread, &attr,
250 create, start, thread_exit, param,
254 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
257 mono_mutex_unlock(&thread_hash_mutex);
258 _wapi_handle_unlock_handle (handle);
259 _wapi_handle_unref (handle);
261 /* And again, because of the reference we took above */
262 _wapi_handle_unref (handle);
266 g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
268 mono_mutex_unlock(&thread_hash_mutex);
271 g_message(G_GNUC_PRETTY_FUNCTION
272 ": Started thread handle %p thread %p ID %ld", handle,
273 thread_private_handle->thread,
274 thread_private_handle->thread->id);
278 #ifdef PTHREAD_POINTER_ID
279 *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
281 *tid=thread_private_handle->thread->id;
285 _wapi_handle_unlock_handle (handle);
290 gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, guint32 tid)
294 mono_once(&thread_hash_once, thread_hash_init);
295 mono_once (&thread_ops_once, thread_ops_init);
298 g_message (G_GNUC_PRETTY_FUNCTION ": looking up thread %d", tid);
301 mono_mutex_lock(&thread_hash_mutex);
303 ret=g_hash_table_lookup(thread_hash, &tid);
304 mono_mutex_unlock(&thread_hash_mutex);
307 _wapi_handle_ref (ret);
311 g_message (G_GNUC_PRETTY_FUNCTION ": returning thread handle %p", ret);
319 * @exitcode: Sets the thread's exit code, which can be read from
320 * another thread with GetExitCodeThread().
322 * Terminates the calling thread. A thread can also exit by returning
323 * from its start function. When the last thread in a process
324 * terminates, the process itself terminates.
326 void ExitThread(guint32 exitcode)
328 _wapi_timed_thread_exit(exitcode);
333 * @handle: The thread handle to query
334 * @exitcode: The thread @handle exit code is stored here
336 * Finds the exit code of @handle, and stores it in @exitcode. If the
337 * thread @handle is still running, the value stored is %STILL_ACTIVE.
339 * Return value: %TRUE, or %FALSE on error.
341 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
343 struct _WapiHandle_thread *thread_handle;
344 struct _WapiHandlePrivate_thread *thread_private_handle;
347 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
348 (gpointer *)&thread_handle,
349 (gpointer *)&thread_private_handle);
351 g_warning (G_GNUC_PRETTY_FUNCTION
352 ": error looking up thread handle %p", handle);
357 g_message(G_GNUC_PRETTY_FUNCTION
358 ": Finding exit status for thread handle %p id %ld", handle,
359 thread_private_handle->thread->id);
364 g_message(G_GNUC_PRETTY_FUNCTION
365 ": Nowhere to store exit code");
370 if(thread_handle->state!=THREAD_STATE_EXITED) {
372 g_message(G_GNUC_PRETTY_FUNCTION
373 ": Thread still active (state %d, exited is %d)",
374 thread_handle->state, THREAD_STATE_EXITED);
376 *exitcode=STILL_ACTIVE;
380 *exitcode=thread_handle->exitstatus;
386 * GetCurrentThreadId:
388 * Looks up the thread ID of the current thread. This ID can be
389 * passed to OpenThread() to create a new handle on this thread.
391 * Return value: the thread ID.
393 guint32 GetCurrentThreadId(void)
395 pthread_t tid=pthread_self();
397 #ifdef PTHREAD_POINTER_ID
398 return(GPOINTER_TO_UINT(tid));
404 static gpointer thread_attach(guint32 *tid)
406 struct _WapiHandle_thread *thread_handle;
407 struct _WapiHandlePrivate_thread *thread_private_handle;
412 mono_once(&thread_hash_once, thread_hash_init);
413 mono_once (&thread_ops_once, thread_ops_init);
415 handle=_wapi_handle_new (WAPI_HANDLE_THREAD);
416 if(handle==_WAPI_HANDLE_INVALID) {
417 g_warning (G_GNUC_PRETTY_FUNCTION
418 ": error creating thread handle");
422 _wapi_handle_lock_handle (handle);
424 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
425 (gpointer *)&thread_handle,
426 (gpointer *)&thread_private_handle);
428 g_warning (G_GNUC_PRETTY_FUNCTION
429 ": error looking up thread handle %p", handle);
430 _wapi_handle_unlock_handle (handle);
434 /* Hold a reference while the thread is active, because we use
435 * the handle to store thread exit information
437 _wapi_handle_ref (handle);
439 thread_handle->state=THREAD_STATE_START;
441 /* Lock around the thread create, so that the new thread cant
442 * race us to look up the thread handle in GetCurrentThread()
444 mono_mutex_lock(&thread_hash_mutex);
446 ret=_wapi_timed_thread_attach(&thread_private_handle->thread,
447 thread_exit, handle);
450 g_message(G_GNUC_PRETTY_FUNCTION ": Thread attach error: %s",
453 mono_mutex_unlock(&thread_hash_mutex);
454 _wapi_handle_unlock_handle (handle);
455 _wapi_handle_unref (handle);
457 /* And again, because of the reference we took above */
458 _wapi_handle_unref (handle);
462 g_hash_table_insert(thread_hash, &thread_private_handle->thread->id,
464 mono_mutex_unlock(&thread_hash_mutex);
467 g_message(G_GNUC_PRETTY_FUNCTION
468 ": Attached thread handle %p thread %p ID %ld", handle,
469 thread_private_handle->thread,
470 thread_private_handle->thread->id);
474 #ifdef PTHREAD_POINTER_ID
475 *tid=GPOINTER_TO_UINT(thread_private_handle->thread->id);
477 *tid=thread_private_handle->thread->id;
481 _wapi_handle_unlock_handle (handle);
489 * Looks up the handle associated with the current thread. Under
490 * Windows this is a pseudohandle, and must be duplicated with
491 * DuplicateHandle() for some operations.
493 * Return value: The current thread handle, or %NULL on failure.
494 * (Unknown whether Windows has a possible failure here. It may be
495 * necessary to implement the pseudohandle-constant behaviour).
497 gpointer GetCurrentThread(void)
502 mono_once(&thread_hash_once, thread_hash_init);
503 mono_once (&thread_ops_once, thread_ops_init);
505 tid=GetCurrentThreadId();
507 mono_mutex_lock(&thread_hash_mutex);
509 ret=g_hash_table_lookup(thread_hash, &tid);
510 mono_mutex_unlock(&thread_hash_mutex);
513 ret = thread_attach (NULL);
521 * @handle: the thread handle to resume
523 * Decrements the suspend count of thread @handle. A thread can only
524 * run if its suspend count is zero.
526 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
528 guint32 ResumeThread(gpointer handle)
530 struct _WapiHandle_thread *thread_handle;
531 struct _WapiHandlePrivate_thread *thread_private_handle;
534 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
535 (gpointer *)&thread_handle,
536 (gpointer *)&thread_private_handle);
538 g_warning (G_GNUC_PRETTY_FUNCTION
539 ": error looking up thread handle %p", handle);
543 #ifdef WITH_INCLUDED_LIBGC
544 if (thread_private_handle->thread->suspend_count <= 1)
545 _wapi_timed_thread_resume (thread_private_handle->thread);
547 return --thread_private_handle->thread->suspend_count;
549 /* This is still a kludge that only copes with starting a
550 * thread that was suspended on create, so don't bother with
551 * the suspend count crap yet
553 _wapi_timed_thread_resume (thread_private_handle->thread);
560 * @handle: the thread handle to suspend
562 * Increments the suspend count of thread @handle. A thread can only
563 * run if its suspend count is zero.
565 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
567 guint32 SuspendThread(gpointer handle)
569 #ifdef WITH_INCLUDED_LIBGC
570 struct _WapiHandle_thread *thread_handle;
571 struct _WapiHandlePrivate_thread *thread_private_handle;
575 current = GetCurrentThread ();
576 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
577 (gpointer *)&thread_handle,
578 (gpointer *)&thread_private_handle);
580 g_warning (G_GNUC_PRETTY_FUNCTION
581 ": error looking up thread handle %p", handle);
585 if (!thread_private_handle->thread->suspend_count) {
586 if (handle == current)
587 _wapi_timed_thread_suspend (thread_private_handle->thread);
589 pthread_kill (thread_private_handle->thread->id, SIGPWR);
590 sem_wait (&thread_private_handle->thread->suspended_sem);
594 return thread_private_handle->thread->suspend_count++;
601 * We assume here that TLS_MINIMUM_AVAILABLE is less than
602 * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
605 * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
606 * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
610 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
611 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
612 #ifdef TLS_PTHREAD_MUTEX
613 static mono_mutex_t TLS_mutex=MONO_MUTEX_INITIALIZER;
615 static guint32 TLS_spinlock=0;
621 * Allocates a Thread Local Storage (TLS) index. Any thread in the
622 * same process can use this index to store and retrieve values that
623 * are local to that thread.
625 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
628 guint32 TlsAlloc(void)
632 #ifdef TLS_PTHREAD_MUTEX
633 mono_mutex_lock(&TLS_mutex);
635 MONO_SPIN_LOCK (TLS_spinlock);
638 for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
639 if(TLS_used[i]==FALSE) {
641 pthread_key_create(&TLS_keys[i], NULL);
643 #ifdef TLS_PTHREAD_MUTEX
644 mono_mutex_unlock(&TLS_mutex);
646 MONO_SPIN_UNLOCK (TLS_spinlock);
650 g_message (G_GNUC_PRETTY_FUNCTION ": returning key %d",
658 #ifdef TLS_PTHREAD_MUTEX
659 mono_mutex_unlock(&TLS_mutex);
661 MONO_SPIN_UNLOCK (TLS_spinlock);
665 g_message (G_GNUC_PRETTY_FUNCTION ": out of indices");
669 return(TLS_OUT_OF_INDEXES);
672 #define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
676 * @idx: The TLS index to free
678 * Releases a TLS index, making it available for reuse. This call
679 * will delete any TLS data stored under index @idx in all threads.
681 * Return value: %TRUE on success, %FALSE otherwise.
683 gboolean TlsFree(guint32 idx)
686 g_message (G_GNUC_PRETTY_FUNCTION ": freeing key %d", idx);
689 #ifdef TLS_PTHREAD_MUTEX
690 mono_mutex_lock(&TLS_mutex);
692 MONO_SPIN_LOCK (TLS_spinlock);
695 if(TLS_used[idx]==FALSE) {
696 #ifdef TLS_PTHREAD_MUTEX
697 mono_mutex_unlock(&TLS_mutex);
699 MONO_SPIN_UNLOCK (TLS_spinlock);
705 pthread_key_delete(TLS_keys[idx]);
708 mono_g_hash_table_remove (tls_gc_hash, MAKE_GC_ID (idx));
711 #ifdef TLS_PTHREAD_MUTEX
712 mono_mutex_unlock(&TLS_mutex);
714 MONO_SPIN_UNLOCK (TLS_spinlock);
722 * @idx: The TLS index to retrieve
724 * Retrieves the TLS data stored under index @idx.
726 * Return value: The value stored in the TLS index @idx in the current
727 * thread, or %NULL on error. As %NULL can be a valid return value,
728 * in this case GetLastError() returns %ERROR_SUCCESS.
730 gpointer TlsGetValue(guint32 idx)
735 g_message (G_GNUC_PRETTY_FUNCTION ": looking up key %d", idx);
738 ret=pthread_getspecific(TLS_keys[idx]);
741 g_message (G_GNUC_PRETTY_FUNCTION ": returning %p", ret);
749 * @idx: The TLS index to store
750 * @value: The value to store under index @idx
752 * Stores @value at TLS index @idx.
754 * Return value: %TRUE on success, %FALSE otherwise.
756 gboolean TlsSetValue(guint32 idx, gpointer value)
761 g_message (G_GNUC_PRETTY_FUNCTION ": setting key %d to %p", idx,
765 #ifdef TLS_PTHREAD_MUTEX
766 mono_mutex_lock(&TLS_mutex);
768 MONO_SPIN_LOCK (TLS_spinlock);
771 if(TLS_used[idx]==FALSE) {
773 g_message (G_GNUC_PRETTY_FUNCTION ": key %d unused", idx);
776 #ifdef TLS_PTHREAD_MUTEX
777 mono_mutex_unlock(&TLS_mutex);
779 MONO_SPIN_UNLOCK (TLS_spinlock);
784 ret=pthread_setspecific(TLS_keys[idx], value);
787 g_message (G_GNUC_PRETTY_FUNCTION
788 ": pthread_setspecific error: %s", strerror (ret));
791 #ifdef TLS_PTHREAD_MUTEX
792 mono_mutex_unlock(&TLS_mutex);
794 MONO_SPIN_UNLOCK (TLS_spinlock);
801 tls_gc_hash = mono_g_hash_table_new(g_direct_hash, g_direct_equal);
802 mono_g_hash_table_insert (tls_gc_hash, MAKE_GC_ID (idx), value);
805 #ifdef TLS_PTHREAD_MUTEX
806 mono_mutex_unlock(&TLS_mutex);
808 MONO_SPIN_UNLOCK (TLS_spinlock);
816 * @ms: The time in milliseconds to suspend for
818 * Suspends execution of the current thread for @ms milliseconds. A
819 * value of zero causes the thread to relinquish its time slice. A
820 * value of %INFINITE causes an infinite delay.
822 void Sleep(guint32 ms)
824 struct timespec req, rem;
829 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
837 /* FIXME: check for INFINITE and sleep forever */
838 divvy=div((int)ms, 1000);
840 req.tv_sec=divvy.quot;
841 req.tv_nsec=divvy.rem*1000000;
844 ret=nanosleep(&req, &rem);
846 /* Sleep interrupted with rem time remaining */
848 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
850 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
858 /* FIXME: implement alertable */
859 void SleepEx(guint32 ms, gboolean alertable)
861 if(alertable==TRUE) {
862 g_warning(G_GNUC_PRETTY_FUNCTION ": alertable not implemented");
868 #ifdef WITH_INCLUDED_LIBGC
870 static void GC_suspend_handler (int sig)
872 struct _WapiHandle_thread *thread_handle;
873 struct _WapiHandlePrivate_thread *thread_private_handle;
877 handle = GetCurrentThread ();
878 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
879 (gpointer *)&thread_handle,
880 (gpointer *)&thread_private_handle);
882 g_warning (G_GNUC_PRETTY_FUNCTION
883 ": error looking up thread handle %p", handle);
887 thread_private_handle->thread->stack_ptr = &ok;
888 sem_post (&thread_private_handle->thread->suspended_sem);
890 _wapi_timed_thread_suspend (thread_private_handle->thread);
892 thread_private_handle->thread->stack_ptr = NULL;
895 static void gc_init (void)
897 struct sigaction act;
899 act.sa_handler = GC_suspend_handler;
900 g_assert (sigaction (SIGPWR, &act, NULL) == 0);
903 void mono_wapi_push_thread_stack (gpointer handle, gpointer stack_ptr)
905 struct _WapiHandle_thread *thread_handle;
906 struct _WapiHandlePrivate_thread *thread_private_handle;
909 ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
910 (gpointer *)&thread_handle,
911 (gpointer *)&thread_private_handle);
913 g_warning (G_GNUC_PRETTY_FUNCTION
914 ": error looking up thread handle %p", handle);
918 GC_push_all_stack (thread_private_handle->thread->stack_ptr, stack_ptr);
921 #endif /* WITH_INCLUDED_LIBGC */