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"
16 #include "pthread-compat.h"
25 struct _WapiHandle_thread
28 WapiThreadState state;
33 static pthread_mutex_t thread_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
34 static pthread_cond_t thread_signal_cond = PTHREAD_COND_INITIALIZER;
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
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;
43 static void thread_close(WapiHandle *handle);
44 static gboolean thread_wait(WapiHandle *handle, guint32 ms);
45 static guint32 thread_wait_multiple(gpointer data);
47 static struct _WapiHandleOps thread_ops = {
48 thread_close, /* close */
49 NULL, /* getfiletype */
53 NULL, /* setendoffile */
54 NULL, /* getfilesize */
55 thread_wait, /* wait */
56 thread_wait_multiple, /* wait_multiple */
59 static void thread_close(WapiHandle *handle)
61 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
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);
70 g_free(thread_handle->thread);
73 static gboolean thread_wait(WapiHandle *handle, guint32 ms)
75 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
78 if(handle->signalled) {
79 /* Already signalled, so return straight away */
81 g_message(G_GNUC_PRETTY_FUNCTION ": thread handle %p already signalled, returning now", handle);
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);
94 ret=_wapi_timed_thread_join(thread_handle->thread, NULL, NULL);
96 struct timespec timeout;
98 _wapi_calc_timeout(&timeout, ms);
100 ret=_wapi_timed_thread_join(thread_handle->thread, &timeout,
108 /* ret might be ETIMEDOUT for timeout, or other for error */
113 static guint32 thread_wait_multiple(gpointer data)
115 WaitQueueItem *item=(WaitQueueItem *)data;
117 guint32 numhandles, count;
118 struct timespec timeout;
120 numhandles=item->handles[WAPI_HANDLE_THREAD]->len;
123 g_message(G_GNUC_PRETTY_FUNCTION
124 ": waiting on %d thread handles for %d ms", numhandles,
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.
132 count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
135 g_message(G_GNUC_PRETTY_FUNCTION
136 ": Preliminary check found %d handles signalled", count);
139 if((item->waitall==TRUE && count==numhandles) ||
140 (item->waitall==FALSE && count>0)) {
144 /* OK, we need to wait for some */
145 if(item->timeout!=INFINITE) {
146 _wapi_calc_timeout(&timeout, item->timeout);
149 /* We can restart from here without resetting the timeout,
150 * because it is calculated from absolute time, not an offset
153 pthread_mutex_lock(&thread_signal_mutex);
154 if(item->timeout==INFINITE) {
155 ret=pthread_cond_wait(&thread_signal_cond,
156 &thread_signal_mutex);
158 ret=pthread_cond_timedwait(&thread_signal_cond,
159 &thread_signal_mutex,
162 pthread_mutex_unlock(&thread_signal_mutex);
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.
171 g_message(G_GNUC_PRETTY_FUNCTION ": Wait timed out");
174 count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
179 g_message(G_GNUC_PRETTY_FUNCTION ": Thread exited, checking status");
182 /* Another thread exited, so see if it was one we are
185 count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
188 g_message(G_GNUC_PRETTY_FUNCTION
189 ": Check after thread exit found %d handles signalled",
193 if((item->waitall==TRUE && count==numhandles) ||
194 (item->waitall==FALSE && count>0)) {
198 /* Either we have waitall set with more handles to wait for,
199 * or the thread that exited wasn't interesting to us
202 g_message(G_GNUC_PRETTY_FUNCTION ": Waiting a bit longer");
208 item->waited[WAPI_HANDLE_THREAD]=TRUE;
209 item->waitcount[WAPI_HANDLE_THREAD]=count;
214 static void thread_exit(guint32 exitstatus, gpointer userdata)
216 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)userdata;
218 thread_handle->exitstatus=exitstatus;
219 thread_handle->state=THREAD_STATE_EXITED;
220 thread_handle->handle.signalled=TRUE;
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);
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);
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);
239 static void thread_hash_init(void)
241 thread_hash=g_hash_table_new(g_int_hash, g_int_equal);
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.
256 * Creates a new threading handle.
258 * Return value: a new handle, or NULL
260 WapiHandle *CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize G_GNUC_UNUSED,
261 WapiThreadStart start, gpointer param, guint32 create G_GNUC_UNUSED,
264 struct _WapiHandle_thread *thread_handle;
268 pthread_once(&thread_hash_once, thread_hash_init);
274 thread_handle=(struct _WapiHandle_thread *)g_new0(struct _WapiHandle_thread, 1);
275 thread_handle->state=THREAD_STATE_START;
277 /* Lock around the thread create, so that the new thread cant
278 * race us to look up the thread handle in GetCurrentThread()
280 pthread_mutex_lock(&thread_hash_mutex);
282 ret=_wapi_timed_thread_create(&thread_handle->thread, NULL, start,
283 thread_exit, param, thread_handle);
286 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
289 pthread_mutex_unlock(&thread_hash_mutex);
290 g_free(thread_handle);
294 g_hash_table_insert(thread_hash, &thread_handle->thread->id,
296 pthread_mutex_unlock(&thread_hash_mutex);
298 handle=(WapiHandle *)thread_handle;
299 _WAPI_HANDLE_INIT(handle, WAPI_HANDLE_THREAD, thread_ops);
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);
308 *tid=thread_handle->thread->id;
316 * @exitcode: Sets the thread's exit code, which can be read from
317 * another thread with GetExitCodeThread().
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.
323 void ExitThread(guint32 exitcode)
325 _wapi_timed_thread_exit(exitcode);
330 * @handle: The thread handle to query
331 * @exitcode: The thread @handle exit code is stored here
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.
336 * Return value: %TRUE, or %FALSE on error.
338 gboolean GetExitCodeThread(WapiHandle *handle, guint32 *exitcode)
340 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
343 g_message(G_GNUC_PRETTY_FUNCTION
344 ": Finding exit status for thread handle %p id %ld", handle,
345 thread_handle->thread->id);
350 g_message(G_GNUC_PRETTY_FUNCTION
351 ": Nowhere to store exit code");
356 if(thread_handle->state!=THREAD_STATE_EXITED) {
358 g_message(G_GNUC_PRETTY_FUNCTION
359 ": Thread still active (state %d, exited is %d)",
360 thread_handle->state, THREAD_STATE_EXITED);
362 *exitcode=STILL_ACTIVE;
366 *exitcode=thread_handle->exitstatus;
372 * GetCurrentThreadId:
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.
377 * Return value: the thread ID.
379 guint32 GetCurrentThreadId(void)
381 pthread_t tid=pthread_self();
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.
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).
397 WapiHandle *GetCurrentThread(void)
399 WapiHandle *ret=NULL;
402 tid=GetCurrentThreadId();
404 pthread_mutex_lock(&thread_hash_mutex);
406 ret=g_hash_table_lookup(thread_hash, &tid);
408 pthread_mutex_unlock(&thread_hash_mutex);
415 * @handle: the thread handle to resume
417 * Decrements the suspend count of thread @handle. A thread can only
418 * run if its suspend count is zero.
420 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
422 guint32 ResumeThread(WapiHandle *handle G_GNUC_UNUSED)
429 * @handle: the thread handle to suspend
431 * Increments the suspend count of thread @handle. A thread can only
432 * run if its suspend count is zero.
434 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
436 guint32 SuspendThread(WapiHandle *handle G_GNUC_UNUSED)
442 * We assume here that TLS_MINIMUM_AVAILABLE is less than
443 * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
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
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;
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.
462 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
465 guint32 TlsAlloc(void)
469 pthread_mutex_lock(&TLS_mutex);
471 for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
472 if(TLS_used[i]==FALSE) {
474 pthread_key_create(&TLS_keys[i], NULL);
476 pthread_mutex_unlock(&TLS_mutex);
482 pthread_mutex_unlock(&TLS_mutex);
484 return(TLS_OUT_OF_INDEXES);
489 * @idx: The TLS index to free
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.
494 * Return value: %TRUE on success, %FALSE otherwise.
496 gboolean TlsFree(guint32 idx)
498 pthread_mutex_lock(&TLS_mutex);
500 if(TLS_used[idx]==FALSE) {
501 pthread_mutex_unlock(&TLS_mutex);
506 pthread_key_delete(TLS_keys[idx]);
508 pthread_mutex_unlock(&TLS_mutex);
515 * @idx: The TLS index to retrieve
517 * Retrieves the TLS data stored under index @idx.
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.
523 gpointer TlsGetValue(guint32 idx)
527 pthread_mutex_lock(&TLS_mutex);
529 if(TLS_used[idx]==FALSE) {
530 pthread_mutex_unlock(&TLS_mutex);
534 ret=pthread_getspecific(TLS_keys[idx]);
536 pthread_mutex_unlock(&TLS_mutex);
543 * @idx: The TLS index to store
544 * @value: The value to store under index @idx
546 * Stores @value at TLS index @idx.
548 * Return value: %TRUE on success, %FALSE otherwise.
550 gboolean TlsSetValue(guint32 idx, gpointer value)
554 pthread_mutex_lock(&TLS_mutex);
556 if(TLS_used[idx]==FALSE) {
557 pthread_mutex_unlock(&TLS_mutex);
561 ret=pthread_setspecific(TLS_keys[idx], value);
563 pthread_mutex_unlock(&TLS_mutex);
567 pthread_mutex_unlock(&TLS_mutex);
574 * @ms: The time in milliseconds to suspend for
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.
580 void Sleep(guint32 ms)
582 struct timespec req, rem;
587 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
595 /* FIXME: check for INFINITE and sleep forever */
596 divvy=div((int)ms, 1000);
598 req.tv_sec=divvy.quot;
599 req.tv_nsec=divvy.rem*1000;
602 ret=nanosleep(&req, &rem);
604 /* Sleep interrupted with rem time remaining */
606 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000;
608 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",