f8ca4d8538dfdf9d248106fe588173306477d4ff
[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
15 #include "pthread-compat.h"
16
17 #define DEBUG
18
19 typedef enum {
20         THREAD_STATE_START,
21         THREAD_STATE_EXITED,
22 } WapiThreadState;
23
24 struct _WapiHandle_thread
25 {
26         WapiHandle handle;
27         WapiThreadState state;
28         TimedThread *thread;
29         guint32 exitstatus;
30 };
31
32 static pthread_mutex_t thread_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
33 static pthread_cond_t thread_signal_cond = PTHREAD_COND_INITIALIZER;
34
35 static void thread_close(WapiHandle *handle);
36 static gboolean thread_wait(WapiHandle *handle, guint32 ms);
37 static guint32 thread_wait_multiple(gpointer data);
38
39 static struct _WapiHandleOps thread_ops = {
40         thread_close,                   /* close */
41         NULL,                           /* getfiletype */
42         NULL,                           /* readfile */
43         NULL,                           /* writefile */
44         NULL,                           /* seek */
45         NULL,                           /* setendoffile */
46         NULL,                           /* getfilesize */
47         thread_wait,                    /* wait */
48         thread_wait_multiple,           /* wait_multiple */
49 };
50
51 static void thread_close(WapiHandle *handle)
52 {
53         struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
54         
55 #ifdef DEBUG
56         g_message(G_GNUC_PRETTY_FUNCTION
57                   ": closing thread handle %p with thread %p id %ld",
58                   thread_handle, thread_handle->thread,
59                   thread_handle->thread->id);
60 #endif
61
62         g_free(thread_handle->thread);
63 }
64
65 static gboolean thread_wait(WapiHandle *handle, guint32 ms)
66 {
67         struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
68         int ret;
69         
70         if(handle->signalled) {
71                 /* Already signalled, so return straight away */
72 #ifdef DEBUG
73                 g_message(G_GNUC_PRETTY_FUNCTION ": thread handle %p already signalled, returning now", handle);
74 #endif
75
76                 return(TRUE);
77         }
78
79 #ifdef DEBUG
80         g_message(G_GNUC_PRETTY_FUNCTION
81                   ": waiting for %d ms for thread handle %p with id %ld", ms,
82                   thread_handle, thread_handle->thread->id);
83 #endif
84
85         if(ms==INFINITE) {
86                 ret=_wapi_timed_thread_join(thread_handle->thread, NULL, NULL);
87         } else {
88                 struct timespec timeout;
89                 struct timeval now;
90                 div_t divvy;
91
92                 divvy=div((int)ms, 1000);
93                 gettimeofday(&now, NULL);
94         
95                 timeout.tv_sec=now.tv_sec+divvy.quot;
96                 timeout.tv_nsec=(now.tv_usec+divvy.rem)*1000;
97         
98                 ret=_wapi_timed_thread_join(thread_handle->thread, &timeout,
99                                             NULL);
100         }
101         
102         if(ret==0) {
103                 /* Thread joined */
104                 return(TRUE);
105         } else {
106                 /* ret might be ETIMEDOUT for timeout, or other for error */
107                 return(FALSE);
108         }
109 }
110
111 static guint32 thread_wait_multiple(gpointer data)
112 {
113         WaitQueueItem *item=(WaitQueueItem *)data;
114         int ret;
115         guint32 numhandles, count;
116         struct timespec timeout;
117         struct timeval now;
118         div_t divvy;
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->handles[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                 divvy=div((int)item->timeout, 1000);
147                 gettimeofday(&now, NULL);
148         
149                 timeout.tv_sec=now.tv_sec+divvy.quot;
150                 timeout.tv_nsec=(now.tv_usec+divvy.rem)*1000;
151         }
152         
153         /* We can restart from here without resetting the timeout,
154          * because it is calculated from absolute time, not an offset
155          */
156 again:
157         pthread_mutex_lock(&thread_signal_mutex);
158         if(item->timeout==INFINITE) {
159                 ret=pthread_cond_wait(&thread_signal_cond,
160                                       &thread_signal_mutex);
161         } else {
162                 ret=pthread_cond_timedwait(&thread_signal_cond,
163                                            &thread_signal_mutex,
164                                            &timeout);
165         }
166         pthread_mutex_unlock(&thread_signal_mutex);
167
168         if(ret==ETIMEDOUT) {
169                 /* Check signalled state here, just in case a thread
170                  * exited between the first check and the cond wait.
171                  * We return the number of signalled handles, which
172                  * may be fewer than the total.
173                  */
174 #ifdef DEBUG
175                 g_message(G_GNUC_PRETTY_FUNCTION ": Wait timed out");
176 #endif
177
178                 count=_wapi_handle_count_signalled(
179                         item->handles[WAPI_HANDLE_THREAD]);
180                 goto success;
181         }
182         
183 #ifdef DEBUG
184         g_message(G_GNUC_PRETTY_FUNCTION ": Thread exited, checking status");
185 #endif
186
187         /* Another thread exited, so see if it was one we are
188          * interested in
189          */
190         count=_wapi_handle_count_signalled(item->handles[WAPI_HANDLE_THREAD]);
191
192 #ifdef DEBUG
193         g_message(G_GNUC_PRETTY_FUNCTION
194                   ": Check after thread exit found %d handles signalled",
195                   count);
196 #endif
197
198         if((item->waitall==TRUE && count==numhandles) ||
199            (item->waitall==FALSE && count>0)) {
200                 goto success;
201         }
202
203         /* Either we have waitall set with more handles to wait for,
204          * or the thread that exited wasn't interesting to us
205          */
206 #ifdef DEBUG
207         g_message(G_GNUC_PRETTY_FUNCTION ": Waiting a bit longer");
208 #endif
209
210         goto again;
211
212 success:
213         item->waited[WAPI_HANDLE_THREAD]=TRUE;
214         item->waitcount[WAPI_HANDLE_THREAD]=count;
215         
216         return(count);
217 }
218
219 static void thread_exit(guint32 exitstatus, gpointer userdata)
220 {
221         struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)userdata;
222
223         thread_handle->exitstatus=exitstatus;
224         thread_handle->state=THREAD_STATE_EXITED;
225         thread_handle->handle.signalled=TRUE;
226         
227 #ifdef DEBUG
228         g_message(G_GNUC_PRETTY_FUNCTION
229                   ": Recording thread handle %p status as %d", thread_handle,
230                   exitstatus);
231 #endif
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 /**
240  * CreateThread:
241  * @security: Ignored for now.
242  * @stacksize: the size in bytes of the new thread's stack. Use 0 to
243  * default to the normal stack size. (Ignored for now).
244  * @start: The function that the new thread should start with
245  * @param: The parameter to give to @start.
246  * @create: If 0, the new thread is ready to run immediately.  If
247  * %CREATE_SUSPENDED, the new thread will be in the suspended state,
248  * requiring a ResumeThread() call to continue running.
249  * @tid: If non-NULL, the ID of the new thread is stored here.
250  *
251  * Creates a new threading handle.
252  *
253  * Return value: a new handle, or NULL
254  */
255 WapiHandle *CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize G_GNUC_UNUSED,
256                          WapiThreadStart start, gpointer param, guint32 create G_GNUC_UNUSED,
257                          guint32 *tid) 
258 {
259         struct _WapiHandle_thread *thread_handle;
260         WapiHandle *handle;
261         int ret;
262         
263         if(start==NULL) {
264                 return(NULL);
265         }
266         
267         thread_handle=(struct _WapiHandle_thread *)g_new0(struct _WapiHandle_thread, 1);
268         thread_handle->state=THREAD_STATE_START;
269         
270         ret=_wapi_timed_thread_create(&thread_handle->thread, NULL, start,
271                                       thread_exit, param, thread_handle);
272         if(ret!=0) {
273 #ifdef DEBUG
274                 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
275                           strerror(ret));
276 #endif
277                 g_free(thread_handle);
278                 return(NULL);
279         }
280
281         handle=(WapiHandle *)thread_handle;
282         _WAPI_HANDLE_INIT(handle, WAPI_HANDLE_THREAD, thread_ops);
283         
284 #ifdef DEBUG
285         g_message(G_GNUC_PRETTY_FUNCTION
286                   ": Started thread handle %p thread %p ID %ld", thread_handle,
287                   thread_handle->thread, thread_handle->thread->id);
288 #endif
289         
290         if(tid!=NULL) {
291                 *tid=thread_handle->thread->id;
292         }
293         
294         return(handle);
295 }
296
297 /**
298  * ExitThread:
299  * @exitcode: Sets the thread's exit code, which can be read from
300  * another thread with GetExitCodeThread().
301  *
302  * Terminates the calling thread.  A thread can also exit by returning
303  * from its start function. When the last thread in a process
304  * terminates, the process itself terminates.
305  */
306 void ExitThread(guint32 exitcode)
307 {
308         _wapi_timed_thread_exit(exitcode);
309 }
310
311 /**
312  * GetExitCodeThread:
313  * @handle: The thread handle to query
314  * @exitcode: The thread @handle exit code is stored here
315  *
316  * Finds the exit code of @handle, and stores it in @exitcode.  If the
317  * thread @handle is still running, the value stored is %STILL_ACTIVE.
318  *
319  * Return value: %TRUE, or %FALSE on error.
320  */
321 gboolean GetExitCodeThread(WapiHandle *handle, guint32 *exitcode)
322 {
323         struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
324         
325 #ifdef DEBUG
326         g_message(G_GNUC_PRETTY_FUNCTION
327                   ": Finding exit status for thread handle %p id %ld", handle,
328                   thread_handle->thread->id);
329 #endif
330
331         if(exitcode==NULL) {
332 #ifdef DEBUG
333                 g_message(G_GNUC_PRETTY_FUNCTION
334                           ": Nowhere to store exit code");
335 #endif
336                 return(FALSE);
337         }
338         
339         if(thread_handle->state!=THREAD_STATE_EXITED) {
340 #ifdef DEBUG
341                 g_message(G_GNUC_PRETTY_FUNCTION
342                           ": Thread still active (state %d, exited is %d)",
343                           thread_handle->state, THREAD_STATE_EXITED);
344 #endif
345                 *exitcode=STILL_ACTIVE;
346                 return(TRUE);
347         }
348         
349         *exitcode=thread_handle->exitstatus;
350         
351         return(TRUE);
352 }
353
354 /**
355  * ResumeThread:
356  * @handle: the thread handle to resume
357  *
358  * Decrements the suspend count of thread @handle. A thread can only
359  * run if its suspend count is zero.
360  *
361  * Return value: the previous suspend count, or 0xFFFFFFFF on error.
362  */
363 guint32 ResumeThread(WapiHandle *handle G_GNUC_UNUSED)
364 {
365         return(0xFFFFFFFF);
366 }
367
368 /**
369  * SuspendThread:
370  * @handle: the thread handle to suspend
371  *
372  * Increments the suspend count of thread @handle. A thread can only
373  * run if its suspend count is zero.
374  *
375  * Return value: the previous suspend count, or 0xFFFFFFFF on error.
376  */
377 guint32 SuspendThread(WapiHandle *handle G_GNUC_UNUSED)
378 {
379         return(0xFFFFFFFF);
380 }
381
382 /**
383  * TlsAlloc:
384  *
385  * Allocates a Thread Local Storage (TLS) index.  Any thread in the
386  * same process can use this index to store and retrieve values that
387  * are local to that thread.
388  *
389  * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
390  * is available.
391  */
392 guint32 TlsAlloc(void)
393 {
394         return(TLS_OUT_OF_INDEXES);
395 }
396
397 /**
398  * TlsFree:
399  * @idx: The TLS index to free
400  *
401  * Releases a TLS index, making it available for reuse.  This call
402  * will delete any TLS data stored under index @idx in all threads.
403  *
404  * Return value: %TRUE on success, %FALSE otherwise.
405  */
406 gboolean TlsFree(guint32 idx G_GNUC_UNUSED)
407 {
408         return(FALSE);
409 }
410
411 /**
412  * TlsGetValue:
413  * @idx: The TLS index to retrieve
414  *
415  * Retrieves the TLS data stored under index @idx.
416  *
417  * Return value: The value stored in the TLS index @idx in the current
418  * thread, or %NULL on error.  As %NULL can be a valid return value,
419  * in this case GetLastError() returns %ERROR_SUCCESS.
420  */
421 gpointer TlsGetValue(guint32 idx G_GNUC_UNUSED)
422 {
423         return(NULL);
424 }
425
426 /**
427  * TlsSetValue:
428  * @idx: The TLS index to store
429  * @value: The value to store under index @idx
430  *
431  * Stores @value at TLS index @idx.
432  *
433  * Return value: %TRUE on success, %FALSE otherwise.
434  */
435 gboolean TlsSetValue(guint32 idx G_GNUC_UNUSED, gpointer value G_GNUC_UNUSED)
436 {
437         return(FALSE);
438 }
439
440 /**
441  * Sleep:
442  * @ms: The time in milliseconds to suspend for
443  *
444  * Suspends execution of the current thread for @ms milliseconds.  A
445  * value of zero causes the thread to relinquish its time slice.  A
446  * value of %INFINITE causes an infinite delay.
447  */
448 void Sleep(guint32 ms)
449 {
450         struct timespec req, rem;
451         div_t divvy;
452         int ret;
453         
454 #ifdef DEBUG
455         g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
456 #endif
457
458         if(ms==0) {
459                 sched_yield();
460                 return;
461         }
462         
463         /* FIXME: check for INFINITE and sleep forever */
464         divvy=div((int)ms, 1000);
465         
466         req.tv_sec=divvy.quot;
467         req.tv_nsec=divvy.rem*1000;
468         
469 again:
470         ret=nanosleep(&req, &rem);
471         if(ret==-1) {
472                 /* Sleep interrupted with rem time remaining */
473 #ifdef DEBUG
474                 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000;
475                 
476                 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
477                           rems);
478 #endif
479                 req=rem;
480                 goto again;
481         }
482 }