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