2002-04-16 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mono / io-layer / threads.c
1 #include <config.h>
2 #if HAVE_BOEHM_GC
3 #include <gc/gc.h>
4 #include "mono/utils/mono-hash.h"
5 #endif
6 #include <glib.h>
7 #include <string.h>
8 #include <pthread.h>
9 #include <sched.h>
10 #include <sys/time.h>
11 #include <errno.h>
12
13 #include "mono/io-layer/wapi.h"
14 #include "wapi-private.h"
15 #include "timed-thread.h"
16 #include "wait-private.h"
17 #include "handles-private.h"
18 #include "misc-private.h"
19
20 #include "mono-mutex.h"
21
22 #undef DEBUG
23
24 typedef enum {
25         THREAD_STATE_START,
26         THREAD_STATE_EXITED,
27 } WapiThreadState;
28
29 struct _WapiHandle_thread
30 {
31         WapiHandle handle;
32         WapiThreadState state;
33         TimedThread *thread;
34         guint32 exitstatus;
35 };
36
37 static mono_mutex_t thread_signal_mutex = MONO_MUTEX_INITIALIZER;
38 static pthread_cond_t thread_signal_cond = PTHREAD_COND_INITIALIZER;
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 pthread_once_t thread_hash_once = PTHREAD_ONCE_INIT;
44 static mono_mutex_t thread_hash_mutex = MONO_MUTEX_INITIALIZER;
45 static GHashTable *thread_hash=NULL;
46
47 #if HAVE_BOEHM_GC
48 static MonoGHashTable *tls_gc_hash = NULL;
49 #endif
50
51 static void thread_close(WapiHandle *handle);
52 static gboolean thread_wait(WapiHandle *handle, WapiHandle *signal,
53                             guint32 ms);
54 static guint32 thread_wait_multiple(gpointer data);
55
56 static struct _WapiHandleOps thread_ops = {
57         thread_close,                   /* close */
58         NULL,                           /* getfiletype */
59         NULL,                           /* readfile */
60         NULL,                           /* writefile */
61         NULL,                           /* flushfile */
62         NULL,                           /* seek */
63         NULL,                           /* setendoffile */
64         NULL,                           /* getfilesize */
65         NULL,                           /* getfiletime */
66         NULL,                           /* setfiletime */
67         thread_wait,                    /* wait */
68         thread_wait_multiple,           /* wait_multiple */
69         NULL,                           /* signal */
70 };
71
72 static void thread_close(WapiHandle *handle)
73 {
74         struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
75         
76 #ifdef DEBUG
77         g_message(G_GNUC_PRETTY_FUNCTION
78                   ": closing thread handle %p with thread %p id %ld",
79                   thread_handle, thread_handle->thread,
80                   thread_handle->thread->id);
81 #endif
82
83         g_free(thread_handle->thread);
84 }
85
86 static gboolean thread_wait(WapiHandle *handle, WapiHandle *signal, guint32 ms)
87 {
88         struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
89         int ret;
90         
91         /* A thread can never become unsignalled after it was
92          * signalled, so we can signal this handle now without
93          * worrying about lost wakeups
94          */
95         if(signal!=NULL) {
96                 signal->ops->signal(signal);
97         }
98         
99         if(handle->signalled==TRUE) {
100                 /* Already signalled, so return straight away */
101 #ifdef DEBUG
102                 g_message(G_GNUC_PRETTY_FUNCTION ": thread handle %p already signalled, returning now", handle);
103 #endif
104
105                 return(TRUE);
106         }
107
108 #ifdef DEBUG
109         g_message(G_GNUC_PRETTY_FUNCTION
110                   ": waiting for %d ms for thread handle %p with id %ld", ms,
111                   thread_handle, thread_handle->thread->id);
112 #endif
113
114         if(ms==INFINITE) {
115                 ret=_wapi_timed_thread_join(thread_handle->thread, NULL, NULL);
116         } else {
117                 struct timespec timeout;
118
119                 _wapi_calc_timeout(&timeout, ms);
120         
121                 ret=_wapi_timed_thread_join(thread_handle->thread, &timeout,
122                                             NULL);
123         }
124         
125         if(ret==0) {
126                 /* Thread joined */
127                 return(TRUE);
128         } else {
129                 /* ret might be ETIMEDOUT for timeout, or other for error */
130                 return(FALSE);
131         }
132 }
133
134 static guint32 thread_wait_multiple(gpointer data)
135 {
136         WaitQueueItem *item=(WaitQueueItem *)data;
137         int ret;
138         guint32 numhandles, count;
139         struct timespec timeout;
140         
141         numhandles=item->handles[WAPI_HANDLE_THREAD]->len;
142         
143 #ifdef DEBUG
144         g_message(G_GNUC_PRETTY_FUNCTION
145                   ": waiting on %d thread handles for %d ms", numhandles,
146                   item->timeout);
147 #endif
148
149         /* First, check if any of the handles are already
150          * signalled. If waitall is specified we only return if all
151          * handles have been signalled.
152          */
153         count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
154
155 #ifdef DEBUG
156         g_message(G_GNUC_PRETTY_FUNCTION
157                   ": Preliminary check found %d handles signalled", count);
158 #endif
159
160         if((item->waitall==TRUE && count==numhandles) || 
161            (item->waitall==FALSE && count>0)) {
162                 goto success;
163         }
164         
165         /* OK, we need to wait for some */
166         if(item->timeout!=INFINITE) {
167                 _wapi_calc_timeout(&timeout, item->timeout);
168         }
169         
170         /* We can restart from here without resetting the timeout,
171          * because it is calculated from absolute time, not an offset
172          */
173 again:
174         mono_mutex_lock(&thread_signal_mutex);
175         if(item->timeout==INFINITE) {
176                 ret=mono_cond_wait(&thread_signal_cond,
177                                       &thread_signal_mutex);
178         } else {
179                 ret=mono_cond_timedwait(&thread_signal_cond,
180                                            &thread_signal_mutex,
181                                            &timeout);
182         }
183         mono_mutex_unlock(&thread_signal_mutex);
184
185         if(ret==ETIMEDOUT) {
186                 /* Check signalled state here, just in case a thread
187                  * exited between the first check and the cond wait.
188                  * We return the number of signalled handles, which
189                  * may be fewer than the total.
190                  */
191 #ifdef DEBUG
192                 g_message(G_GNUC_PRETTY_FUNCTION ": Wait timed out");
193 #endif
194
195                 count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
196                 goto success;
197         }
198         
199 #ifdef DEBUG
200         g_message(G_GNUC_PRETTY_FUNCTION ": Thread exited, checking status");
201 #endif
202
203         /* Another thread exited, so see if it was one we are
204          * interested in
205          */
206         count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
207
208 #ifdef DEBUG
209         g_message(G_GNUC_PRETTY_FUNCTION
210                   ": Check after thread exit found %d handles signalled",
211                   count);
212 #endif
213
214         if((item->waitall==TRUE && count==numhandles) ||
215            (item->waitall==FALSE && count>0)) {
216                 goto success;
217         }
218
219         /* Either we have waitall set with more handles to wait for,
220          * or the thread that exited wasn't interesting to us
221          */
222 #ifdef DEBUG
223         g_message(G_GNUC_PRETTY_FUNCTION ": Waiting a bit longer");
224 #endif
225
226         goto again;
227
228 success:
229         item->waited[WAPI_HANDLE_THREAD]=TRUE;
230         item->waitcount[WAPI_HANDLE_THREAD]=count;
231         
232         return(count);
233 }
234
235 static void thread_exit(guint32 exitstatus, gpointer userdata)
236 {
237         struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)userdata;
238
239         thread_handle->exitstatus=exitstatus;
240         thread_handle->state=THREAD_STATE_EXITED;
241         thread_handle->handle.signalled=TRUE;
242         
243 #ifdef DEBUG
244         g_message(G_GNUC_PRETTY_FUNCTION
245                   ": Recording thread handle %p id %ld status as %d",
246                   thread_handle, thread_handle->thread->id, exitstatus);
247 #endif
248
249         /* Remove this thread from the hash */
250         mono_mutex_lock(&thread_hash_mutex);
251         g_hash_table_remove(thread_hash, &thread_handle->thread->id);
252         mono_mutex_unlock(&thread_hash_mutex);
253         
254         /* Signal any thread waiting on thread exit */
255         mono_mutex_lock(&thread_signal_mutex);
256         pthread_cond_broadcast(&thread_signal_cond);
257         mono_mutex_unlock(&thread_signal_mutex);
258 }
259
260 static void thread_hash_init(void)
261 {
262         thread_hash=g_hash_table_new(g_int_hash, g_int_equal);
263 }
264
265 /**
266  * CreateThread:
267  * @security: Ignored for now.
268  * @stacksize: the size in bytes of the new thread's stack. Use 0 to
269  * default to the normal stack size. (Ignored for now).
270  * @start: The function that the new thread should start with
271  * @param: The parameter to give to @start.
272  * @create: If 0, the new thread is ready to run immediately.  If
273  * %CREATE_SUSPENDED, the new thread will be in the suspended state,
274  * requiring a ResumeThread() call to continue running.
275  * @tid: If non-NULL, the ID of the new thread is stored here.
276  *
277  * Creates a new threading handle.
278  *
279  * Return value: a new handle, or NULL
280  */
281 WapiHandle *CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize G_GNUC_UNUSED,
282                          WapiThreadStart start, gpointer param, guint32 create G_GNUC_UNUSED,
283                          guint32 *tid) 
284 {
285         struct _WapiHandle_thread *thread_handle;
286         WapiHandle *handle;
287         int ret;
288         
289         pthread_once(&thread_hash_once, thread_hash_init);
290         
291         if(start==NULL) {
292                 return(NULL);
293         }
294         
295         thread_handle=(struct _WapiHandle_thread *)g_new0(struct _WapiHandle_thread, 1);
296
297         handle=(WapiHandle *)thread_handle;
298         _WAPI_HANDLE_INIT(handle, WAPI_HANDLE_THREAD, thread_ops);
299
300         thread_handle->state=THREAD_STATE_START;
301         
302         /* Lock around the thread create, so that the new thread cant
303          * race us to look up the thread handle in GetCurrentThread()
304          */
305         mono_mutex_lock(&thread_hash_mutex);
306         
307         ret=_wapi_timed_thread_create(&thread_handle->thread, NULL, start,
308                                       thread_exit, param, thread_handle);
309         if(ret!=0) {
310 #ifdef DEBUG
311                 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
312                           strerror(ret));
313 #endif
314                 mono_mutex_unlock(&thread_hash_mutex);
315                 g_free(thread_handle);
316                 return(NULL);
317         }
318
319         g_hash_table_insert(thread_hash, &thread_handle->thread->id,
320                             thread_handle);
321         mono_mutex_unlock(&thread_hash_mutex);
322         
323 #ifdef DEBUG
324         g_message(G_GNUC_PRETTY_FUNCTION
325                   ": Started thread handle %p thread %p ID %ld", thread_handle,
326                   thread_handle->thread, thread_handle->thread->id);
327 #endif
328         
329         if(tid!=NULL) {
330                 *tid=thread_handle->thread->id;
331         }
332         
333         return(handle);
334 }
335
336 /**
337  * ExitThread:
338  * @exitcode: Sets the thread's exit code, which can be read from
339  * another thread with GetExitCodeThread().
340  *
341  * Terminates the calling thread.  A thread can also exit by returning
342  * from its start function. When the last thread in a process
343  * terminates, the process itself terminates.
344  */
345 void ExitThread(guint32 exitcode)
346 {
347         _wapi_timed_thread_exit(exitcode);
348 }
349
350 /**
351  * GetExitCodeThread:
352  * @handle: The thread handle to query
353  * @exitcode: The thread @handle exit code is stored here
354  *
355  * Finds the exit code of @handle, and stores it in @exitcode.  If the
356  * thread @handle is still running, the value stored is %STILL_ACTIVE.
357  *
358  * Return value: %TRUE, or %FALSE on error.
359  */
360 gboolean GetExitCodeThread(WapiHandle *handle, guint32 *exitcode)
361 {
362         struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
363         
364 #ifdef DEBUG
365         g_message(G_GNUC_PRETTY_FUNCTION
366                   ": Finding exit status for thread handle %p id %ld", handle,
367                   thread_handle->thread->id);
368 #endif
369
370         if(exitcode==NULL) {
371 #ifdef DEBUG
372                 g_message(G_GNUC_PRETTY_FUNCTION
373                           ": Nowhere to store exit code");
374 #endif
375                 return(FALSE);
376         }
377         
378         if(thread_handle->state!=THREAD_STATE_EXITED) {
379 #ifdef DEBUG
380                 g_message(G_GNUC_PRETTY_FUNCTION
381                           ": Thread still active (state %d, exited is %d)",
382                           thread_handle->state, THREAD_STATE_EXITED);
383 #endif
384                 *exitcode=STILL_ACTIVE;
385                 return(TRUE);
386         }
387         
388         *exitcode=thread_handle->exitstatus;
389         
390         return(TRUE);
391 }
392
393 /**
394  * GetCurrentThreadId:
395  *
396  * Looks up the thread ID of the current thread.  This ID can be
397  * passed to OpenThread() to create a new handle on this thread.
398  *
399  * Return value: the thread ID.
400  */
401 guint32 GetCurrentThreadId(void)
402 {
403         pthread_t tid=pthread_self();
404         
405         return(tid);
406 }
407
408 /**
409  * GetCurrentThread:
410  *
411  * Looks up the handle associated with the current thread.  Under
412  * Windows this is a pseudohandle, and must be duplicated with
413  * DuplicateHandle() for some operations.
414  *
415  * Return value: The current thread handle, or %NULL on failure.
416  * (Unknown whether Windows has a possible failure here.  It may be
417  * necessary to implement the pseudohandle-constant behaviour).
418  */
419 WapiHandle *GetCurrentThread(void)
420 {
421         WapiHandle *ret=NULL;
422         guint32 tid;
423         
424         tid=GetCurrentThreadId();
425         
426         mono_mutex_lock(&thread_hash_mutex);
427
428         ret=g_hash_table_lookup(thread_hash, &tid);
429         
430         mono_mutex_unlock(&thread_hash_mutex);
431         
432         return(ret);
433 }
434
435 /**
436  * ResumeThread:
437  * @handle: the thread handle to resume
438  *
439  * Decrements the suspend count of thread @handle. A thread can only
440  * run if its suspend count is zero.
441  *
442  * Return value: the previous suspend count, or 0xFFFFFFFF on error.
443  */
444 guint32 ResumeThread(WapiHandle *handle G_GNUC_UNUSED)
445 {
446         return(0xFFFFFFFF);
447 }
448
449 /**
450  * SuspendThread:
451  * @handle: the thread handle to suspend
452  *
453  * Increments the suspend count of thread @handle. A thread can only
454  * run if its suspend count is zero.
455  *
456  * Return value: the previous suspend count, or 0xFFFFFFFF on error.
457  */
458 guint32 SuspendThread(WapiHandle *handle G_GNUC_UNUSED)
459 {
460         return(0xFFFFFFFF);
461 }
462
463 /*
464  * We assume here that TLS_MINIMUM_AVAILABLE is less than
465  * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
466  * library usage.
467  *
468  * Currently TLS_MINIMUM_AVAILABLE is 64 and _POSIX_THREAD_KEYS_MAX
469  * (the minimum value for PTHREAD_KEYS_MAX) is 128, so we should be
470  * fine.
471  */
472
473 static pthread_key_t TLS_keys[TLS_MINIMUM_AVAILABLE];
474 static gboolean TLS_used[TLS_MINIMUM_AVAILABLE]={FALSE};
475 static mono_mutex_t TLS_mutex=MONO_MUTEX_INITIALIZER;
476
477 /**
478  * TlsAlloc:
479  *
480  * Allocates a Thread Local Storage (TLS) index.  Any thread in the
481  * same process can use this index to store and retrieve values that
482  * are local to that thread.
483  *
484  * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
485  * is available.
486  */
487 guint32 TlsAlloc(void)
488 {
489         guint32 i;
490         
491         mono_mutex_lock(&TLS_mutex);
492         
493         for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
494                 if(TLS_used[i]==FALSE) {
495                         TLS_used[i]=TRUE;
496                         pthread_key_create(&TLS_keys[i], NULL);
497
498                         mono_mutex_unlock(&TLS_mutex);
499                         
500                         return(i);
501                 }
502         }
503
504         mono_mutex_unlock(&TLS_mutex);
505         
506         return(TLS_OUT_OF_INDEXES);
507 }
508
509 /**
510  * TlsFree:
511  * @idx: The TLS index to free
512  *
513  * Releases a TLS index, making it available for reuse.  This call
514  * will delete any TLS data stored under index @idx in all threads.
515  *
516  * Return value: %TRUE on success, %FALSE otherwise.
517  */
518 gboolean TlsFree(guint32 idx)
519 {
520         mono_mutex_lock(&TLS_mutex);
521         
522         if(TLS_used[idx]==FALSE) {
523                 mono_mutex_unlock(&TLS_mutex);
524                 return(FALSE);
525         }
526         
527         TLS_used[idx]=FALSE;
528         pthread_key_delete(TLS_keys[idx]);
529         
530 #if HAVE_BOEHM_GC
531         mono_g_hash_table_remove (tls_gc_hash, GUINT_TO_POINTER (idx));
532 #endif
533         mono_mutex_unlock(&TLS_mutex);
534         
535         return(TRUE);
536 }
537
538 /**
539  * TlsGetValue:
540  * @idx: The TLS index to retrieve
541  *
542  * Retrieves the TLS data stored under index @idx.
543  *
544  * Return value: The value stored in the TLS index @idx in the current
545  * thread, or %NULL on error.  As %NULL can be a valid return value,
546  * in this case GetLastError() returns %ERROR_SUCCESS.
547  */
548 gpointer TlsGetValue(guint32 idx)
549 {
550         gpointer ret;
551         
552         mono_mutex_lock(&TLS_mutex);
553         
554         if(TLS_used[idx]==FALSE) {
555                 mono_mutex_unlock(&TLS_mutex);
556                 return(NULL);
557         }
558         
559         ret=pthread_getspecific(TLS_keys[idx]);
560         
561         mono_mutex_unlock(&TLS_mutex);
562         
563         return(ret);
564 }
565
566 /**
567  * TlsSetValue:
568  * @idx: The TLS index to store
569  * @value: The value to store under index @idx
570  *
571  * Stores @value at TLS index @idx.
572  *
573  * Return value: %TRUE on success, %FALSE otherwise.
574  */
575 gboolean TlsSetValue(guint32 idx, gpointer value)
576 {
577         int ret;
578         
579         mono_mutex_lock(&TLS_mutex);
580         
581         if(TLS_used[idx]==FALSE) {
582                 mono_mutex_unlock(&TLS_mutex);
583                 return(FALSE);
584         }
585         
586         ret=pthread_setspecific(TLS_keys[idx], value);
587         if(ret!=0) {
588                 mono_mutex_unlock(&TLS_mutex);
589                 return(FALSE);
590         }
591         
592 #if HAVE_BOEHM_GC
593         if (!tls_gc_hash)
594                 tls_gc_hash = mono_g_hash_table_new(g_direct_hash, g_direct_equal);
595         /* FIXME: index needs to encode the thread id, too */
596         mono_g_hash_table_insert (tls_gc_hash, GUINT_TO_POINTER (idx), value);
597 #endif
598         mono_mutex_unlock(&TLS_mutex);
599         
600         return(TRUE);
601 }
602
603 /**
604  * Sleep:
605  * @ms: The time in milliseconds to suspend for
606  *
607  * Suspends execution of the current thread for @ms milliseconds.  A
608  * value of zero causes the thread to relinquish its time slice.  A
609  * value of %INFINITE causes an infinite delay.
610  */
611 void Sleep(guint32 ms)
612 {
613         struct timespec req, rem;
614         div_t divvy;
615         int ret;
616         
617 #ifdef DEBUG
618         g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
619 #endif
620
621         if(ms==0) {
622                 sched_yield();
623                 return;
624         }
625         
626         /* FIXME: check for INFINITE and sleep forever */
627         divvy=div((int)ms, 1000);
628         
629         req.tv_sec=divvy.quot;
630         req.tv_nsec=divvy.rem*1000000;
631         
632 again:
633         ret=nanosleep(&req, &rem);
634         if(ret==-1) {
635                 /* Sleep interrupted with rem time remaining */
636 #ifdef DEBUG
637                 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
638                 
639                 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
640                           rems);
641 #endif
642                 req=rem;
643                 goto again;
644         }
645 }
646
647 /* FIXME: implement alertable */
648 void SleepEx(guint32 ms, gboolean alertable)
649 {
650         if(alertable==TRUE) {
651                 g_warning(G_GNUC_PRETTY_FUNCTION ": alertable not implemented");
652         }
653         
654         Sleep(ms);
655 }