2005-09-26 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / io-layer / threads.c
1 /*
2  * threads.c:  Thread handles
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2002 Ximian, Inc.
8  */
9
10 #include <config.h>
11 #include <stdio.h>
12 #include <glib.h>
13 #include <string.h>
14 #include <pthread.h>
15 #include <signal.h>
16 #include <sched.h>
17 #include <sys/time.h>
18 #include <errno.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21
22 #include <mono/io-layer/wapi.h>
23 #include <mono/io-layer/wapi-private.h>
24 #include <mono/io-layer/timed-thread.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>
31
32 #if HAVE_VALGRIND_MEMCHECK_H
33 #include <valgrind/memcheck.h>
34 #endif
35
36 #undef DEBUG
37 #undef TLS_DEBUG
38
39
40 /* Hash threads with tids. I thought of using TLS for this, but that
41  * would have to set the data in the new thread, which is more hassle
42  */
43 static mono_once_t thread_hash_once = MONO_ONCE_INIT;
44 static mono_mutex_t thread_hash_mutex = MONO_MUTEX_INITIALIZER;
45 static GHashTable *thread_hash=NULL;
46
47 static void thread_close (gpointer handle, gpointer data);
48 static gboolean thread_own (gpointer handle);
49
50 struct _WapiHandleOps _wapi_thread_ops = {
51         thread_close,                   /* close */
52         NULL,                           /* signal */
53         thread_own,                     /* own */
54         NULL,                           /* is_owned */
55 };
56
57 static mono_once_t thread_ops_once=MONO_ONCE_INIT;
58
59 #ifdef WITH_INCLUDED_LIBGC
60 static void gc_init (void);
61 #endif
62
63 static void thread_ops_init (void)
64 {
65         _wapi_handle_register_capabilities (WAPI_HANDLE_THREAD,
66                                             WAPI_HANDLE_CAP_WAIT);
67
68 #ifdef WITH_INCLUDED_LIBGC
69         gc_init ();
70 #endif
71 }
72
73 static void thread_close (gpointer handle, gpointer data)
74 {
75         struct _WapiHandle_thread *thread_handle = (struct _WapiHandle_thread *)data;
76
77 #ifdef DEBUG
78         g_message ("%s: closing thread handle %p", __func__, handle);
79 #endif
80
81         g_ptr_array_free (thread_handle->owned_mutexes, TRUE);
82 }
83
84 static gboolean thread_own (gpointer handle)
85 {
86         struct _WapiHandle_thread *thread_handle;
87         gboolean ok;
88         
89 #ifdef DEBUG
90         g_message ("%s: owning thread handle %p", __func__, handle);
91 #endif
92
93         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
94                                   (gpointer *)&thread_handle);
95         if (ok == FALSE) {
96                 g_warning ("%s: error looking up thread handle %p", __func__,
97                            handle);
98                 return(FALSE);
99         }
100         
101         if (thread_handle->joined == FALSE) {
102                 _wapi_timed_thread_join (thread_handle->thread, NULL, NULL);
103                 thread_handle->joined = TRUE;
104         }
105
106         return(TRUE);
107 }
108
109 static void thread_exit(guint32 exitstatus, gpointer handle)
110 {
111         struct _WapiHandle_thread *thread_handle;
112         gboolean ok;
113         int thr_ret;
114         int i;
115         
116         pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
117                               handle);
118         thr_ret = _wapi_handle_lock_handle (handle);
119         g_assert (thr_ret == 0);
120         
121         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
122                                   (gpointer *)&thread_handle);
123         if (ok == FALSE) {
124                 g_warning ("%s: error looking up thread handle %p", __func__,
125                            handle);
126                 return;
127         }
128         
129         for (i = 0; i < thread_handle->owned_mutexes->len; i++) {
130                 _wapi_mutex_abandon (g_ptr_array_index (thread_handle->owned_mutexes, i), getpid (), thread_handle->thread->id);
131         }
132
133 #ifdef DEBUG
134         g_message ("%s: Recording thread handle %p exit status", __func__,
135                    handle);
136 #endif
137         
138         thread_handle->exitstatus = exitstatus;
139         thread_handle->state = THREAD_STATE_EXITED;
140
141         _wapi_handle_set_signal_state (handle, TRUE, TRUE);
142
143         thr_ret = _wapi_handle_unlock_handle (handle);
144         g_assert (thr_ret == 0);
145         pthread_cleanup_pop (0);
146         
147 #ifdef DEBUG
148         g_message("%s: Recording thread handle %p id %ld status as %d",
149                   __func__, handle, thread_handle->thread->id, exitstatus);
150 #endif
151
152         /* Remove this thread from the hash */
153         pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
154                               (void *)&thread_hash_mutex);
155         thr_ret = mono_mutex_lock(&thread_hash_mutex);
156         g_assert (thr_ret == 0);
157         
158         g_hash_table_remove (thread_hash, (gpointer)(thread_handle->thread->id));
159
160         thr_ret = mono_mutex_unlock(&thread_hash_mutex);
161         g_assert (thr_ret == 0);
162         pthread_cleanup_pop (0);
163
164         /* The thread is no longer active, so unref it */
165         _wapi_handle_unref (handle);
166 }
167
168 static void thread_hash_init(void)
169 {
170         thread_hash = g_hash_table_new (NULL, NULL);
171 }
172
173 /**
174  * CreateThread:
175  * @security: Ignored for now.
176  * @stacksize: the size in bytes of the new thread's stack. Use 0 to
177  * default to the normal stack size. (Ignored for now).
178  * @start: The function that the new thread should start with
179  * @param: The parameter to give to @start.
180  * @create: If 0, the new thread is ready to run immediately.  If
181  * %CREATE_SUSPENDED, the new thread will be in the suspended state,
182  * requiring a ResumeThread() call to continue running.
183  * @tid: If non-NULL, the ID of the new thread is stored here.  NB
184  * this is defined as a DWORD (ie 32bit) in the MS API, but we need to
185  * cope with 64 bit IDs for s390x and amd64.
186  *
187  * Creates a new threading handle.
188  *
189  * Return value: a new handle, or NULL
190  */
191 gpointer CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize,
192                       WapiThreadStart start, gpointer param, guint32 create,
193                       gsize *tid) 
194 {
195         struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
196         pthread_attr_t attr;
197         gpointer handle;
198         gboolean ok;
199         int ret;
200         int thr_ret;
201         int i, unrefs = 0;
202         gpointer ct_ret = NULL;
203         
204         mono_once (&thread_hash_once, thread_hash_init);
205         mono_once (&thread_ops_once, thread_ops_init);
206         
207         if (start == NULL) {
208                 return(NULL);
209         }
210
211         thread_handle.state = THREAD_STATE_START;
212         thread_handle.owner_pid = getpid ();
213         thread_handle.owned_mutexes = g_ptr_array_new ();
214         
215         handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle);
216         if (handle == _WAPI_HANDLE_INVALID) {
217                 g_warning ("%s: error creating thread handle", __func__);
218                 SetLastError (ERROR_GEN_FAILURE);
219                 
220                 return (NULL);
221         }
222
223         pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
224                               handle);
225         thr_ret = _wapi_handle_lock_handle (handle);
226         g_assert (thr_ret == 0);
227         
228         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
229                                   (gpointer *)&thread_handle_p);
230         if (ok == FALSE) {
231                 g_warning ("%s: error looking up thread handle %p", __func__,
232                            handle);
233                 SetLastError (ERROR_GEN_FAILURE);
234                 
235                 goto cleanup;
236         }
237
238         /* Hold a reference while the thread is active, because we use
239          * the handle to store thread exit information
240          */
241         _wapi_handle_ref (handle);
242         
243         /* Lock around the thread create, so that the new thread cant
244          * race us to look up the thread handle in GetCurrentThread()
245          */
246         pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
247                               (void *)&thread_hash_mutex);
248         thr_ret = mono_mutex_lock(&thread_hash_mutex);
249         g_assert (thr_ret == 0);
250         
251         /* Set a 2M stack size.  This is the default on Linux, but BSD
252          * needs it.  (The original bug report from Martin Dvorak <md@9ll.cz>
253          * set the size to 2M-4k.  I don't know why it's short by 4k, so
254          * I'm leaving it as 2M until I'm told differently.)
255          */
256         thr_ret = pthread_attr_init(&attr);
257         g_assert (thr_ret == 0);
258         
259         /* defaults of 2Mb for 32bits and 4Mb for 64bits */
260         /* temporarily changed to use 1 MB: this allows more threads
261          * to be used, as well as using less virtual memory and so
262          * more is available for the GC heap.
263          */
264         if (stacksize == 0){
265 #if HAVE_VALGRIND_MEMCHECK_H
266                 if (RUNNING_ON_VALGRIND) {
267                         stacksize = 1 << 20;
268                 } else {
269                         stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
270                 }
271 #else
272                 stacksize = (SIZEOF_VOID_P / 4) * 1024 * 1024;
273 #endif
274         }
275
276 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
277         thr_ret = pthread_attr_setstacksize(&attr, stacksize);
278         g_assert (thr_ret == 0);
279 #endif
280
281         ret = _wapi_timed_thread_create (&thread_handle_p->thread, &attr,
282                                          create, start, thread_exit, param,
283                                          handle);
284         if (ret != 0) {
285 #ifdef DEBUG
286                 g_message ("%s: Thread create error: %s", __func__,
287                            strerror(ret));
288 #endif
289
290                 /* Two, because of the reference we took above */
291                 unrefs = 2;
292                 
293                 goto thread_hash_cleanup;
294         }
295         ct_ret = handle;
296         
297         g_hash_table_insert (thread_hash,
298                              (gpointer)(thread_handle_p->thread->id),
299                              handle);
300         
301 #ifdef DEBUG
302         g_message("%s: Started thread handle %p thread %p ID %ld", __func__,
303                   handle, thread_handle_p->thread,
304                   thread_handle_p->thread->id);
305 #endif
306         
307         if (tid != NULL) {
308 #ifdef PTHREAD_POINTER_ID
309                 /* Don't use GPOINTER_TO_UINT here, it can't cope with
310                  * sizeof(void *) > sizeof(uint) when a cast to uint
311                  * would overflow
312                  */
313                 *tid = (gsize)(thread_handle_p->thread->id);
314 #else
315                 *tid = thread_handle_p->thread->id;
316 #endif
317         }
318
319 thread_hash_cleanup:
320         thr_ret = mono_mutex_unlock (&thread_hash_mutex);
321         g_assert (thr_ret == 0);
322         pthread_cleanup_pop (0);
323
324 cleanup:
325         thr_ret = _wapi_handle_unlock_handle (handle);
326         g_assert (thr_ret == 0);
327         pthread_cleanup_pop (0);
328         
329         /* Must not call _wapi_handle_unref() with the handle already
330          * locked
331          */
332         for (i = 0; i < unrefs; i++) {
333                 _wapi_handle_unref (handle);
334         }
335         
336         return(ct_ret);
337 }
338
339 gpointer _wapi_thread_handle_from_id (pthread_t tid)
340 {
341         gpointer ret=NULL;
342         int thr_ret;
343
344         pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
345                               (void *)&thread_hash_mutex);
346         thr_ret = mono_mutex_lock(&thread_hash_mutex);
347         g_assert (thr_ret == 0);
348         
349         ret = g_hash_table_lookup (thread_hash, (gpointer)(tid));
350
351         thr_ret = mono_mutex_unlock(&thread_hash_mutex);
352         g_assert (thr_ret == 0);
353         pthread_cleanup_pop (0);
354
355         return(ret);
356 }
357
358 /* NB tid is 32bit in MS API, but we need 64bit on amd64 and s390x
359  * (and probably others)
360  */
361 gpointer OpenThread (guint32 access G_GNUC_UNUSED, gboolean inherit G_GNUC_UNUSED, gsize tid)
362 {
363         gpointer ret=NULL;
364         
365         mono_once (&thread_hash_once, thread_hash_init);
366         mono_once (&thread_ops_once, thread_ops_init);
367         
368 #ifdef DEBUG
369         g_message ("%s: looking up thread %"G_GSIZE_FORMAT, __func__, tid);
370 #endif
371
372         ret = _wapi_thread_handle_from_id ((pthread_t)tid);
373         if(ret!=NULL) {
374                 _wapi_handle_ref (ret);
375         }
376         
377 #ifdef DEBUG
378         g_message ("%s: returning thread handle %p", __func__, ret);
379 #endif
380         
381         return(ret);
382 }
383
384 /**
385  * ExitThread:
386  * @exitcode: Sets the thread's exit code, which can be read from
387  * another thread with GetExitCodeThread().
388  *
389  * Terminates the calling thread.  A thread can also exit by returning
390  * from its start function. When the last thread in a process
391  * terminates, the process itself terminates.
392  */
393 void ExitThread(guint32 exitcode)
394 {
395         _wapi_timed_thread_exit(exitcode);
396 }
397
398 /**
399  * GetExitCodeThread:
400  * @handle: The thread handle to query
401  * @exitcode: The thread @handle exit code is stored here
402  *
403  * Finds the exit code of @handle, and stores it in @exitcode.  If the
404  * thread @handle is still running, the value stored is %STILL_ACTIVE.
405  *
406  * Return value: %TRUE, or %FALSE on error.
407  */
408 gboolean GetExitCodeThread(gpointer handle, guint32 *exitcode)
409 {
410         struct _WapiHandle_thread *thread_handle;
411         gboolean ok;
412         
413         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
414                                   (gpointer *)&thread_handle);
415         if (ok == FALSE) {
416                 g_warning ("%s: error looking up thread handle %p", __func__,
417                            handle);
418                 return (FALSE);
419         }
420         
421 #ifdef DEBUG
422         g_message ("%s: Finding exit status for thread handle %p id %ld",
423                    __func__, handle, thread_handle->thread->id);
424 #endif
425
426         if (exitcode == NULL) {
427 #ifdef DEBUG
428                 g_message ("%s: Nowhere to store exit code", __func__);
429 #endif
430                 return(FALSE);
431         }
432         
433         if (thread_handle->state != THREAD_STATE_EXITED) {
434 #ifdef DEBUG
435                 g_message ("%s: Thread still active (state %d, exited is %d)",
436                            __func__, thread_handle->state,
437                            THREAD_STATE_EXITED);
438 #endif
439                 *exitcode = STILL_ACTIVE;
440                 return(TRUE);
441         }
442         
443         *exitcode = thread_handle->exitstatus;
444         
445         return(TRUE);
446 }
447
448 /**
449  * GetCurrentThreadId:
450  *
451  * Looks up the thread ID of the current thread.  This ID can be
452  * passed to OpenThread() to create a new handle on this thread.
453  *
454  * Return value: the thread ID.  NB this is defined as DWORD (ie 32
455  * bit) in the MS API, but we need to cope with 64 bit IDs for s390x
456  * and amd64.  This doesn't really break the API, it just embraces and
457  * extends it on 64bit platforms :)
458  */
459 gsize GetCurrentThreadId(void)
460 {
461         pthread_t tid = pthread_self();
462         
463 #ifdef PTHREAD_POINTER_ID
464         /* Don't use GPOINTER_TO_UINT here, it can't cope with
465          * sizeof(void *) > sizeof(uint) when a cast to uint would
466          * overflow
467          */
468         return((gsize)tid);
469 #else
470         return(tid);
471 #endif
472 }
473
474 static gpointer thread_attach(gsize *tid)
475 {
476         struct _WapiHandle_thread thread_handle = {0}, *thread_handle_p;
477         gpointer handle;
478         gboolean ok;
479         int ret;
480         int thr_ret;
481         int i, unrefs = 0;
482         gpointer ta_ret = NULL;
483         
484         mono_once (&thread_hash_once, thread_hash_init);
485         mono_once (&thread_ops_once, thread_ops_init);
486
487         thread_handle.state = THREAD_STATE_START;
488         thread_handle.owner_pid = getpid ();
489         thread_handle.owned_mutexes = g_ptr_array_new ();
490
491         handle = _wapi_handle_new (WAPI_HANDLE_THREAD, &thread_handle);
492         if (handle == _WAPI_HANDLE_INVALID) {
493                 g_warning ("%s: error creating thread handle", __func__);
494                 
495                 SetLastError (ERROR_GEN_FAILURE);
496                 return (NULL);
497         }
498
499         pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
500                               handle);
501         thr_ret = _wapi_handle_lock_handle (handle);
502         g_assert (thr_ret == 0);
503         
504         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
505                                   (gpointer *)&thread_handle_p);
506         if (ok == FALSE) {
507                 g_warning ("%s: error looking up thread handle %p", __func__,
508                            handle);
509                 
510                 SetLastError (ERROR_GEN_FAILURE);
511                 goto cleanup;
512         }
513
514         /* Hold a reference while the thread is active, because we use
515          * the handle to store thread exit information
516          */
517         _wapi_handle_ref (handle);
518         
519         /* Lock around the thread create, so that the new thread cant
520          * race us to look up the thread handle in GetCurrentThread()
521          */
522         pthread_cleanup_push ((void(*)(void *))mono_mutex_unlock_in_cleanup,
523                               (void *)&thread_hash_mutex);
524         thr_ret = mono_mutex_lock(&thread_hash_mutex);
525         g_assert (thr_ret == 0);
526
527         ret = _wapi_timed_thread_attach (&thread_handle_p->thread, thread_exit,
528                                          handle);
529         if (ret != 0) {
530 #ifdef DEBUG
531                 g_message ("%s: Thread attach error: %s", __func__,
532                            strerror(ret));
533 #endif
534
535                 /* Two, because of the reference we took above */
536                 unrefs = 2;
537                 
538                 goto thread_hash_cleanup;
539         }
540         ta_ret = handle;
541         
542         g_hash_table_insert (thread_hash,
543                              (gpointer)(thread_handle_p->thread->id),
544                              handle);
545
546 #ifdef DEBUG
547         g_message("%s: Attached thread handle %p thread %p ID %ld", __func__,
548                   handle, thread_handle_p->thread,
549                   thread_handle_p->thread->id);
550 #endif
551
552         if (tid != NULL) {
553 #ifdef PTHREAD_POINTER_ID
554                 /* Don't use GPOINTER_TO_UINT here, it can't cope with
555                  * sizeof(void *) > sizeof(uint) when a cast to uint
556                  * would overflow
557                  */
558                 *tid = (gsize)(thread_handle_p->thread->id);
559 #else
560                 *tid = thread_handle_p->thread->id;
561 #endif
562         }
563
564 thread_hash_cleanup:
565         thr_ret = mono_mutex_unlock (&thread_hash_mutex);
566         g_assert (thr_ret == 0);
567         pthread_cleanup_pop (0);
568
569 cleanup:
570         thr_ret = _wapi_handle_unlock_handle (handle);
571         g_assert (thr_ret == 0);
572         pthread_cleanup_pop (0);
573         
574         /* Must not call _wapi_handle_unref() with the handle already
575          * locked
576          */
577         for (i = 0; i < unrefs; i++) {
578                 _wapi_handle_unref (handle);
579         }
580         
581         return(ta_ret);
582 }
583
584 /**
585  * GetCurrentThread:
586  *
587  * Looks up the handle associated with the current thread.  Under
588  * Windows this is a pseudohandle, and must be duplicated with
589  * DuplicateHandle() for some operations.
590  *
591  * Return value: The current thread handle, or %NULL on failure.
592  * (Unknown whether Windows has a possible failure here.  It may be
593  * necessary to implement the pseudohandle-constant behaviour).
594  */
595 gpointer GetCurrentThread(void)
596 {
597         gpointer ret=NULL;
598         gsize tid;
599         
600         mono_once(&thread_hash_once, thread_hash_init);
601         mono_once (&thread_ops_once, thread_ops_init);
602         
603         tid = GetCurrentThreadId();
604         
605         ret = _wapi_thread_handle_from_id ((pthread_t)tid);
606         if (!ret) {
607                 ret = thread_attach (NULL);
608         }
609
610         return(ret);
611 }
612
613 /**
614  * ResumeThread:
615  * @handle: the thread handle to resume
616  *
617  * Decrements the suspend count of thread @handle. A thread can only
618  * run if its suspend count is zero.
619  *
620  * Return value: the previous suspend count, or 0xFFFFFFFF on error.
621  */
622 guint32 ResumeThread(gpointer handle)
623 {
624         struct _WapiHandle_thread *thread_handle;
625         gboolean ok;
626         
627         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
628                                   (gpointer *)&thread_handle);
629         if (ok == FALSE) {
630                 g_warning ("%s: error looking up thread handle %p", __func__,
631                            handle);
632                 
633                 return (0xFFFFFFFF);
634         }
635         
636         if (thread_handle->thread == NULL) {
637                 return(0xFFFFFFFF);
638         }
639
640 #ifdef WITH_INCLUDED_LIBGC
641         if (thread_handle->thread->suspend_count <= 1)
642                 _wapi_timed_thread_resume (thread_handle->thread);
643         
644         return (--thread_handle->thread->suspend_count));
645 #else
646         /* This is still a kludge that only copes with starting a
647          * thread that was suspended on create, so don't bother with
648          * the suspend count crap yet
649          */
650         _wapi_timed_thread_resume (thread_handle->thread);
651         return(0xFFFFFFFF);
652 #endif
653 }
654
655 /**
656  * SuspendThread:
657  * @handle: the thread handle to suspend
658  *
659  * Increments the suspend count of thread @handle. A thread can only
660  * run if its suspend count is zero.
661  *
662  * Return value: the previous suspend count, or 0xFFFFFFFF on error.
663  */
664 guint32 SuspendThread(gpointer handle)
665 {
666 #ifdef WITH_INCLUDED_LIBGC
667         struct _WapiHandle_thread *thread_handle;
668         gpointer current;
669         gboolean ok;
670
671         current = GetCurrentThread ();
672         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
673                                   (gpointer *)&thread_handle);
674         if (ok == FALSE) {
675                 g_warning ("%s: error looking up thread handle %p", __func__,
676                            handle);
677                 return (0xFFFFFFFF);
678         }
679         
680         if (thread_handle->thread == NULL) {
681                 return(0xFFFFFFFF);
682         }
683
684         if (!thread_handle->thread->suspend_count) {
685                 if (handle == current)
686                         _wapi_timed_thread_suspend (thread_handle->thread);
687                 else {
688                         pthread_kill (thread_handle->thread->id, SIGPWR);
689                         while (MONO_SEM_WAIT (&thread_handle->thread->suspended_sem) != 0) {
690                                 if (errno != EINTR) {
691                                         return(0xFFFFFFFF);
692                                 }
693                         }
694                 }
695         }
696
697         return (thread_handle->thread->suspend_count++);
698 #else
699         return(0xFFFFFFFF);
700 #endif
701 }
702
703 /*
704  * We assume here that TLS_MINIMUM_AVAILABLE is less than
705  * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
706  * library usage.
707  *
708  * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
709  * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
710  * fine.
711  */
712
713 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
714 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
715 static guint32 TLS_spinlock=0;
716
717 guint32
718 mono_pthread_key_for_tls (guint32 idx)
719 {
720         return (guint32)TLS_keys [idx];
721 }
722
723 /**
724  * TlsAlloc:
725  *
726  * Allocates a Thread Local Storage (TLS) index.  Any thread in the
727  * same process can use this index to store and retrieve values that
728  * are local to that thread.
729  *
730  * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
731  * is available.
732  */
733 guint32 TlsAlloc(void)
734 {
735         guint32 i;
736         int thr_ret;
737         
738         MONO_SPIN_LOCK (TLS_spinlock);
739         
740         for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
741                 if(TLS_used[i]==FALSE) {
742                         TLS_used[i]=TRUE;
743                         thr_ret = pthread_key_create(&TLS_keys[i], NULL);
744                         g_assert (thr_ret == 0);
745
746                         MONO_SPIN_UNLOCK (TLS_spinlock);
747         
748 #ifdef TLS_DEBUG
749                         g_message ("%s: returning key %d", __func__, i);
750 #endif
751                         
752                         return(i);
753                 }
754         }
755
756         MONO_SPIN_UNLOCK (TLS_spinlock);
757         
758 #ifdef TLS_DEBUG
759         g_message ("%s: out of indices", __func__);
760 #endif
761                         
762         
763         return(TLS_OUT_OF_INDEXES);
764 }
765
766 #define MAKE_GC_ID(idx) (GUINT_TO_POINTER((idx)|(GetCurrentThreadId()<<8)))
767
768 /**
769  * TlsFree:
770  * @idx: The TLS index to free
771  *
772  * Releases a TLS index, making it available for reuse.  This call
773  * will delete any TLS data stored under index @idx in all threads.
774  *
775  * Return value: %TRUE on success, %FALSE otherwise.
776  */
777 gboolean TlsFree(guint32 idx)
778 {
779         int thr_ret;
780         
781 #ifdef TLS_DEBUG
782         g_message ("%s: freeing key %d", __func__, idx);
783 #endif
784
785         MONO_SPIN_LOCK (TLS_spinlock);
786         
787         if(TLS_used[idx]==FALSE) {
788                 MONO_SPIN_UNLOCK (TLS_spinlock);
789
790                 return(FALSE);
791         }
792         
793         TLS_used[idx]=FALSE;
794         thr_ret = pthread_key_delete(TLS_keys[idx]);
795         g_assert (thr_ret == 0);
796         
797         MONO_SPIN_UNLOCK (TLS_spinlock);
798         
799         return(TRUE);
800 }
801
802 /**
803  * TlsGetValue:
804  * @idx: The TLS index to retrieve
805  *
806  * Retrieves the TLS data stored under index @idx.
807  *
808  * Return value: The value stored in the TLS index @idx in the current
809  * thread, or %NULL on error.  As %NULL can be a valid return value,
810  * in this case GetLastError() returns %ERROR_SUCCESS.
811  */
812 gpointer TlsGetValue(guint32 idx)
813 {
814         gpointer ret;
815         
816 #ifdef TLS_DEBUG
817         g_message ("%s: looking up key %d", __func__, idx);
818 #endif
819         
820         ret=pthread_getspecific(TLS_keys[idx]);
821
822 #ifdef TLS_DEBUG
823         g_message ("%s: returning %p", __func__, ret);
824 #endif
825         
826         return(ret);
827 }
828
829 /**
830  * TlsSetValue:
831  * @idx: The TLS index to store
832  * @value: The value to store under index @idx
833  *
834  * Stores @value at TLS index @idx.
835  *
836  * Return value: %TRUE on success, %FALSE otherwise.
837  */
838 gboolean TlsSetValue(guint32 idx, gpointer value)
839 {
840         int ret;
841
842 #ifdef TLS_DEBUG
843         g_message ("%s: setting key %d to %p", __func__, idx, value);
844 #endif
845         
846         MONO_SPIN_LOCK (TLS_spinlock);
847         
848         if(TLS_used[idx]==FALSE) {
849 #ifdef TLS_DEBUG
850                 g_message ("%s: key %d unused", __func__, idx);
851 #endif
852
853                 MONO_SPIN_UNLOCK (TLS_spinlock);
854
855                 return(FALSE);
856         }
857         
858         ret=pthread_setspecific(TLS_keys[idx], value);
859         if(ret!=0) {
860 #ifdef TLS_DEBUG
861                 g_message ("%s: pthread_setspecific error: %s", __func__,
862                            strerror (ret));
863 #endif
864
865                 MONO_SPIN_UNLOCK (TLS_spinlock);
866
867                 return(FALSE);
868         }
869         
870         MONO_SPIN_UNLOCK (TLS_spinlock);
871         
872         return(TRUE);
873 }
874
875 /**
876  * SleepEx:
877  * @ms: The time in milliseconds to suspend for
878  * @alertable: if TRUE, the wait can be interrupted by an APC call
879  *
880  * Suspends execution of the current thread for @ms milliseconds.  A
881  * value of zero causes the thread to relinquish its time slice.  A
882  * value of %INFINITE causes an infinite delay.
883  */
884 guint32 SleepEx(guint32 ms, gboolean alertable)
885 {
886         struct timespec req, rem;
887         int ms_quot, ms_rem;
888         int ret;
889         gpointer current_thread = NULL;
890         
891 #ifdef DEBUG
892         g_message("%s: Sleeping for %d ms", __func__, ms);
893 #endif
894
895         if (alertable) {
896                 current_thread = GetCurrentThread ();
897                 if (_wapi_thread_apc_pending (current_thread)) {
898                         _wapi_thread_dispatch_apc_queue (current_thread);
899                         return WAIT_IO_COMPLETION;
900                 }
901         }
902         
903         if(ms==0) {
904                 sched_yield();
905                 return 0;
906         }
907         
908         /* FIXME: check for INFINITE and sleep forever */
909         ms_quot = ms / 1000;
910         ms_rem = ms % 1000;
911         
912         req.tv_sec=ms_quot;
913         req.tv_nsec=ms_rem*1000000;
914         
915 again:
916         ret=nanosleep(&req, &rem);
917
918         if (alertable && _wapi_thread_apc_pending (current_thread)) {
919                 _wapi_thread_dispatch_apc_queue (current_thread);
920                 return WAIT_IO_COMPLETION;
921         }
922         
923         if(ret==-1) {
924                 /* Sleep interrupted with rem time remaining */
925 #ifdef DEBUG
926                 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
927                 
928                 g_message("%s: Still got %d ms to go", __func__, rems);
929 #endif
930                 req=rem;
931                 goto again;
932         }
933
934         return 0;
935 }
936
937 void Sleep(guint32 ms)
938 {
939         SleepEx(ms, FALSE);
940 }
941
942 guint32 QueueUserAPC (WapiApcProc apc_callback, gpointer handle, 
943                                         gpointer param)
944 {
945         struct _WapiHandle_thread *thread_handle;
946         gboolean ok;
947         
948         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
949                                   (gpointer *)&thread_handle);
950         if (ok == FALSE) {
951                 g_warning ("%s: error looking up thread handle %p", __func__,
952                            handle);
953                 return (0);
954         }
955         
956         _wapi_timed_thread_queue_apc (thread_handle->thread, apc_callback,
957                                       param);
958         return(1);
959 }
960
961 gboolean _wapi_thread_cur_apc_pending (void)
962 {
963         return(_wapi_thread_apc_pending (GetCurrentThread ()));
964 }
965
966 gboolean _wapi_thread_apc_pending (gpointer handle)
967 {
968         struct _WapiHandle_thread *thread_handle;
969         gboolean ok;
970         
971         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
972                                   (gpointer *)&thread_handle);
973         if (ok == FALSE) {
974                 g_warning ("%s: error looking up thread handle %p", __func__,
975                            handle);
976                 return (FALSE);
977         }
978         
979         return(_wapi_timed_thread_apc_pending (thread_handle->thread));
980 }
981
982 gboolean _wapi_thread_dispatch_apc_queue (gpointer handle)
983 {
984         struct _WapiHandle_thread *thread_handle;
985         gboolean ok;
986         
987         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
988                                   (gpointer *)&thread_handle);
989         if (ok == FALSE) {
990                 g_warning ("%s: error looking up thread handle %p", __func__,
991                            handle);
992                 return (0);
993         }
994         
995         _wapi_timed_thread_dispatch_apc_queue (thread_handle->thread);
996         return(1);
997 }
998
999 void _wapi_thread_own_mutex (pthread_t tid, gpointer mutex)
1000 {
1001         struct _WapiHandle_thread *thread_handle;
1002         gboolean ok;
1003         gpointer thread;
1004
1005         thread = _wapi_thread_handle_from_id (tid);
1006         if (thread == NULL) {
1007                 g_warning ("%s: error looking up thread by ID", __func__);
1008                 return;
1009         }
1010
1011         ok = _wapi_lookup_handle (thread, WAPI_HANDLE_THREAD,
1012                                   (gpointer *)&thread_handle);
1013         if (ok == FALSE) {
1014                 g_warning ("%s: error looking up thread handle %p", __func__,
1015                            thread);
1016                 return;
1017         }
1018
1019         g_ptr_array_add (thread_handle->owned_mutexes, mutex);
1020 }
1021
1022 void _wapi_thread_disown_mutex (pthread_t tid, gpointer mutex)
1023 {
1024         struct _WapiHandle_thread *thread_handle;
1025         gboolean ok;
1026         gpointer thread;
1027
1028         thread = _wapi_thread_handle_from_id (tid);
1029         if (thread == NULL) {
1030                 g_warning ("%s: error looking up thread by ID", __func__);
1031                 return;
1032         }
1033
1034         ok = _wapi_lookup_handle (thread, WAPI_HANDLE_THREAD,
1035                                   (gpointer *)&thread_handle);
1036         if (ok == FALSE) {
1037                 g_warning ("%s: error looking up thread handle %p", __func__,
1038                            thread);
1039                 return;
1040         }
1041
1042         g_ptr_array_remove (thread_handle->owned_mutexes, mutex);
1043 }
1044
1045
1046
1047 #ifdef WITH_INCLUDED_LIBGC
1048
1049 static void GC_suspend_handler (int sig)
1050 {
1051         struct _WapiHandle_thread *thread_handle;
1052         gpointer handle;
1053         gboolean ok;
1054
1055         handle = GetCurrentThread ();
1056         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1057                                   (gpointer *)&thread_handle);
1058         if (ok == FALSE) {
1059                 g_warning ("%s: error looking up thread handle %p", __func__,
1060                            handle);
1061                 return;
1062         }
1063         
1064         thread_handle->thread->stack_ptr = &ok;
1065         MONO_SEM_POST (&thread_handle->thread->suspended_sem);
1066
1067         _wapi_timed_thread_suspend (thread_handle->thread);
1068
1069         thread_handle->thread->stack_ptr = NULL;
1070 }
1071
1072 static void gc_init (void)
1073 {
1074         struct sigaction act;
1075
1076         act.sa_handler = GC_suspend_handler;
1077         g_assert (sigaction (SIGPWR, &act, NULL) == 0);
1078 }
1079
1080 void mono_wapi_push_thread_stack (gpointer handle, gpointer stack_ptr)
1081 {
1082         struct _WapiHandle_thread *thread_handle;
1083         gboolean ok;
1084         
1085         ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
1086                                   (gpointer *)&thread_handle);
1087         if (ok == FALSE) {
1088                 g_warning ("%s: error looking up thread handle %p", __func__,
1089                            handle);
1090                 return;
1091         }
1092         
1093         GC_push_all_stack (thread_handle->thread->stack_ptr, stack_ptr);
1094 }
1095
1096 #endif /* WITH_INCLUDED_LIBGC */