4 #include "mono/utils/mono-hash.h"
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"
20 #include "mono-mutex.h"
29 struct _WapiHandle_thread
32 WapiThreadState state;
37 static mono_mutex_t thread_signal_mutex = MONO_MUTEX_INITIALIZER;
38 static pthread_cond_t thread_signal_cond = PTHREAD_COND_INITIALIZER;
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
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;
48 static MonoGHashTable *tls_gc_hash = NULL;
51 static void thread_close(WapiHandle *handle);
52 static gboolean thread_wait(WapiHandle *handle, WapiHandle *signal,
54 static guint32 thread_wait_multiple(gpointer data);
56 static struct _WapiHandleOps thread_ops = {
57 thread_close, /* close */
58 NULL, /* getfiletype */
63 NULL, /* setendoffile */
64 NULL, /* getfilesize */
65 NULL, /* getfiletime */
66 NULL, /* setfiletime */
67 thread_wait, /* wait */
68 thread_wait_multiple, /* wait_multiple */
72 static void thread_close(WapiHandle *handle)
74 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
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);
83 g_free(thread_handle->thread);
86 static gboolean thread_wait(WapiHandle *handle, WapiHandle *signal, guint32 ms)
88 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
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
96 signal->ops->signal(signal);
99 if(handle->signalled==TRUE) {
100 /* Already signalled, so return straight away */
102 g_message(G_GNUC_PRETTY_FUNCTION ": thread handle %p already signalled, returning now", handle);
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);
115 ret=_wapi_timed_thread_join(thread_handle->thread, NULL, NULL);
117 struct timespec timeout;
119 _wapi_calc_timeout(&timeout, ms);
121 ret=_wapi_timed_thread_join(thread_handle->thread, &timeout,
129 /* ret might be ETIMEDOUT for timeout, or other for error */
134 static guint32 thread_wait_multiple(gpointer data)
136 WaitQueueItem *item=(WaitQueueItem *)data;
138 guint32 numhandles, count;
139 struct timespec timeout;
141 numhandles=item->handles[WAPI_HANDLE_THREAD]->len;
144 g_message(G_GNUC_PRETTY_FUNCTION
145 ": waiting on %d thread handles for %d ms", numhandles,
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.
153 count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
156 g_message(G_GNUC_PRETTY_FUNCTION
157 ": Preliminary check found %d handles signalled", count);
160 if((item->waitall==TRUE && count==numhandles) ||
161 (item->waitall==FALSE && count>0)) {
165 /* OK, we need to wait for some */
166 if(item->timeout!=INFINITE) {
167 _wapi_calc_timeout(&timeout, item->timeout);
170 /* We can restart from here without resetting the timeout,
171 * because it is calculated from absolute time, not an offset
174 mono_mutex_lock(&thread_signal_mutex);
175 if(item->timeout==INFINITE) {
176 ret=mono_cond_wait(&thread_signal_cond,
177 &thread_signal_mutex);
179 ret=mono_cond_timedwait(&thread_signal_cond,
180 &thread_signal_mutex,
183 mono_mutex_unlock(&thread_signal_mutex);
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.
192 g_message(G_GNUC_PRETTY_FUNCTION ": Wait timed out");
195 count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
200 g_message(G_GNUC_PRETTY_FUNCTION ": Thread exited, checking status");
203 /* Another thread exited, so see if it was one we are
206 count=_wapi_handle_count_signalled(item, WAPI_HANDLE_THREAD);
209 g_message(G_GNUC_PRETTY_FUNCTION
210 ": Check after thread exit found %d handles signalled",
214 if((item->waitall==TRUE && count==numhandles) ||
215 (item->waitall==FALSE && count>0)) {
219 /* Either we have waitall set with more handles to wait for,
220 * or the thread that exited wasn't interesting to us
223 g_message(G_GNUC_PRETTY_FUNCTION ": Waiting a bit longer");
229 item->waited[WAPI_HANDLE_THREAD]=TRUE;
230 item->waitcount[WAPI_HANDLE_THREAD]=count;
235 static void thread_exit(guint32 exitstatus, gpointer userdata)
237 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)userdata;
239 thread_handle->exitstatus=exitstatus;
240 thread_handle->state=THREAD_STATE_EXITED;
241 thread_handle->handle.signalled=TRUE;
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);
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);
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);
260 static void thread_hash_init(void)
262 thread_hash=g_hash_table_new(g_int_hash, g_int_equal);
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.
277 * Creates a new threading handle.
279 * Return value: a new handle, or NULL
281 WapiHandle *CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize G_GNUC_UNUSED,
282 WapiThreadStart start, gpointer param, guint32 create G_GNUC_UNUSED,
285 struct _WapiHandle_thread *thread_handle;
289 pthread_once(&thread_hash_once, thread_hash_init);
295 thread_handle=(struct _WapiHandle_thread *)g_new0(struct _WapiHandle_thread, 1);
297 handle=(WapiHandle *)thread_handle;
298 _WAPI_HANDLE_INIT(handle, WAPI_HANDLE_THREAD, thread_ops);
300 thread_handle->state=THREAD_STATE_START;
302 /* Lock around the thread create, so that the new thread cant
303 * race us to look up the thread handle in GetCurrentThread()
305 mono_mutex_lock(&thread_hash_mutex);
307 ret=_wapi_timed_thread_create(&thread_handle->thread, NULL, start,
308 thread_exit, param, thread_handle);
311 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
314 mono_mutex_unlock(&thread_hash_mutex);
315 g_free(thread_handle);
319 g_hash_table_insert(thread_hash, &thread_handle->thread->id,
321 mono_mutex_unlock(&thread_hash_mutex);
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);
330 *tid=thread_handle->thread->id;
338 * @exitcode: Sets the thread's exit code, which can be read from
339 * another thread with GetExitCodeThread().
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.
345 void ExitThread(guint32 exitcode)
347 _wapi_timed_thread_exit(exitcode);
352 * @handle: The thread handle to query
353 * @exitcode: The thread @handle exit code is stored here
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.
358 * Return value: %TRUE, or %FALSE on error.
360 gboolean GetExitCodeThread(WapiHandle *handle, guint32 *exitcode)
362 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
365 g_message(G_GNUC_PRETTY_FUNCTION
366 ": Finding exit status for thread handle %p id %ld", handle,
367 thread_handle->thread->id);
372 g_message(G_GNUC_PRETTY_FUNCTION
373 ": Nowhere to store exit code");
378 if(thread_handle->state!=THREAD_STATE_EXITED) {
380 g_message(G_GNUC_PRETTY_FUNCTION
381 ": Thread still active (state %d, exited is %d)",
382 thread_handle->state, THREAD_STATE_EXITED);
384 *exitcode=STILL_ACTIVE;
388 *exitcode=thread_handle->exitstatus;
394 * GetCurrentThreadId:
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.
399 * Return value: the thread ID.
401 guint32 GetCurrentThreadId(void)
403 pthread_t tid=pthread_self();
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.
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).
419 WapiHandle *GetCurrentThread(void)
421 WapiHandle *ret=NULL;
424 tid=GetCurrentThreadId();
426 mono_mutex_lock(&thread_hash_mutex);
428 ret=g_hash_table_lookup(thread_hash, &tid);
430 mono_mutex_unlock(&thread_hash_mutex);
437 * @handle: the thread handle to resume
439 * Decrements the suspend count of thread @handle. A thread can only
440 * run if its suspend count is zero.
442 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
444 guint32 ResumeThread(WapiHandle *handle G_GNUC_UNUSED)
451 * @handle: the thread handle to suspend
453 * Increments the suspend count of thread @handle. A thread can only
454 * run if its suspend count is zero.
456 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
458 guint32 SuspendThread(WapiHandle *handle G_GNUC_UNUSED)
464 * We assume here that TLS_MINIMUM_AVAILABLE is less than
465 * PTHREAD_KEYS_MAX, allowing enough overhead for a few TLS keys for
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
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;
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.
484 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
487 guint32 TlsAlloc(void)
491 mono_mutex_lock(&TLS_mutex);
493 for(i=0; i<TLS_MINIMUM_AVAILABLE; i++) {
494 if(TLS_used[i]==FALSE) {
496 pthread_key_create(&TLS_keys[i], NULL);
498 mono_mutex_unlock(&TLS_mutex);
504 mono_mutex_unlock(&TLS_mutex);
506 return(TLS_OUT_OF_INDEXES);
511 * @idx: The TLS index to free
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.
516 * Return value: %TRUE on success, %FALSE otherwise.
518 gboolean TlsFree(guint32 idx)
520 mono_mutex_lock(&TLS_mutex);
522 if(TLS_used[idx]==FALSE) {
523 mono_mutex_unlock(&TLS_mutex);
528 pthread_key_delete(TLS_keys[idx]);
531 mono_g_hash_table_remove (tls_gc_hash, GUINT_TO_POINTER (idx));
533 mono_mutex_unlock(&TLS_mutex);
540 * @idx: The TLS index to retrieve
542 * Retrieves the TLS data stored under index @idx.
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.
548 gpointer TlsGetValue(guint32 idx)
552 mono_mutex_lock(&TLS_mutex);
554 if(TLS_used[idx]==FALSE) {
555 mono_mutex_unlock(&TLS_mutex);
559 ret=pthread_getspecific(TLS_keys[idx]);
561 mono_mutex_unlock(&TLS_mutex);
568 * @idx: The TLS index to store
569 * @value: The value to store under index @idx
571 * Stores @value at TLS index @idx.
573 * Return value: %TRUE on success, %FALSE otherwise.
575 gboolean TlsSetValue(guint32 idx, gpointer value)
579 mono_mutex_lock(&TLS_mutex);
581 if(TLS_used[idx]==FALSE) {
582 mono_mutex_unlock(&TLS_mutex);
586 ret=pthread_setspecific(TLS_keys[idx], value);
588 mono_mutex_unlock(&TLS_mutex);
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);
598 mono_mutex_unlock(&TLS_mutex);
605 * @ms: The time in milliseconds to suspend for
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.
611 void Sleep(guint32 ms)
613 struct timespec req, rem;
618 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
626 /* FIXME: check for INFINITE and sleep forever */
627 divvy=div((int)ms, 1000);
629 req.tv_sec=divvy.quot;
630 req.tv_nsec=divvy.rem*1000000;
633 ret=nanosleep(&req, &rem);
635 /* Sleep interrupted with rem time remaining */
637 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000000;
639 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",
647 /* FIXME: implement alertable */
648 void SleepEx(guint32 ms, gboolean alertable)
650 if(alertable==TRUE) {
651 g_warning(G_GNUC_PRETTY_FUNCTION ": alertable not implemented");