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"
15 #include "pthread-compat.h"
24 struct _WapiHandle_thread
27 WapiThreadState state;
32 static pthread_mutex_t thread_signal_mutex = PTHREAD_MUTEX_INITIALIZER;
33 static pthread_cond_t thread_signal_cond = PTHREAD_COND_INITIALIZER;
35 static void thread_close(WapiHandle *handle);
36 static gboolean thread_wait(WapiHandle *handle, guint32 ms);
37 static guint32 thread_wait_multiple(gpointer data);
39 static struct _WapiHandleOps thread_ops = {
40 thread_close, /* close */
41 NULL, /* getfiletype */
45 NULL, /* setendoffile */
46 NULL, /* getfilesize */
47 thread_wait, /* wait */
48 thread_wait_multiple, /* wait_multiple */
51 static void thread_close(WapiHandle *handle)
53 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
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);
62 g_free(thread_handle->thread);
65 static gboolean thread_wait(WapiHandle *handle, guint32 ms)
67 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
70 if(handle->signalled) {
71 /* Already signalled, so return straight away */
73 g_message(G_GNUC_PRETTY_FUNCTION ": thread handle %p already signalled, returning now", handle);
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);
86 ret=_wapi_timed_thread_join(thread_handle->thread, NULL, NULL);
88 struct timespec timeout;
92 divvy=div((int)ms, 1000);
93 gettimeofday(&now, NULL);
95 timeout.tv_sec=now.tv_sec+divvy.quot;
96 timeout.tv_nsec=(now.tv_usec+divvy.rem)*1000;
98 ret=_wapi_timed_thread_join(thread_handle->thread, &timeout,
106 /* ret might be ETIMEDOUT for timeout, or other for error */
111 static guint32 thread_wait_multiple(gpointer data)
113 WaitQueueItem *item=(WaitQueueItem *)data;
115 guint32 numhandles, count;
116 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->handles[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 divvy=div((int)item->timeout, 1000);
147 gettimeofday(&now, NULL);
149 timeout.tv_sec=now.tv_sec+divvy.quot;
150 timeout.tv_nsec=(now.tv_usec+divvy.rem)*1000;
153 /* We can restart from here without resetting the timeout,
154 * because it is calculated from absolute time, not an offset
157 pthread_mutex_lock(&thread_signal_mutex);
158 if(item->timeout==INFINITE) {
159 ret=pthread_cond_wait(&thread_signal_cond,
160 &thread_signal_mutex);
162 ret=pthread_cond_timedwait(&thread_signal_cond,
163 &thread_signal_mutex,
166 pthread_mutex_unlock(&thread_signal_mutex);
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.
175 g_message(G_GNUC_PRETTY_FUNCTION ": Wait timed out");
178 count=_wapi_handle_count_signalled(
179 item->handles[WAPI_HANDLE_THREAD]);
184 g_message(G_GNUC_PRETTY_FUNCTION ": Thread exited, checking status");
187 /* Another thread exited, so see if it was one we are
190 count=_wapi_handle_count_signalled(item->handles[WAPI_HANDLE_THREAD]);
193 g_message(G_GNUC_PRETTY_FUNCTION
194 ": Check after thread exit found %d handles signalled",
198 if((item->waitall==TRUE && count==numhandles) ||
199 (item->waitall==FALSE && count>0)) {
203 /* Either we have waitall set with more handles to wait for,
204 * or the thread that exited wasn't interesting to us
207 g_message(G_GNUC_PRETTY_FUNCTION ": Waiting a bit longer");
213 item->waited[WAPI_HANDLE_THREAD]=TRUE;
214 item->waitcount[WAPI_HANDLE_THREAD]=count;
219 static void thread_exit(guint32 exitstatus, gpointer userdata)
221 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)userdata;
223 thread_handle->exitstatus=exitstatus;
224 thread_handle->state=THREAD_STATE_EXITED;
225 thread_handle->handle.signalled=TRUE;
228 g_message(G_GNUC_PRETTY_FUNCTION
229 ": Recording thread handle %p status as %d", thread_handle,
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);
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.
251 * Creates a new threading handle.
253 * Return value: a new handle, or NULL
255 WapiHandle *CreateThread(WapiSecurityAttributes *security G_GNUC_UNUSED, guint32 stacksize G_GNUC_UNUSED,
256 WapiThreadStart start, gpointer param, guint32 create G_GNUC_UNUSED,
259 struct _WapiHandle_thread *thread_handle;
267 thread_handle=(struct _WapiHandle_thread *)g_new0(struct _WapiHandle_thread, 1);
268 thread_handle->state=THREAD_STATE_START;
270 ret=_wapi_timed_thread_create(&thread_handle->thread, NULL, start,
271 thread_exit, param, thread_handle);
274 g_message(G_GNUC_PRETTY_FUNCTION ": Thread create error: %s",
277 g_free(thread_handle);
281 handle=(WapiHandle *)thread_handle;
282 _WAPI_HANDLE_INIT(handle, WAPI_HANDLE_THREAD, thread_ops);
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);
291 *tid=thread_handle->thread->id;
299 * @exitcode: Sets the thread's exit code, which can be read from
300 * another thread with GetExitCodeThread().
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.
306 void ExitThread(guint32 exitcode)
308 _wapi_timed_thread_exit(exitcode);
313 * @handle: The thread handle to query
314 * @exitcode: The thread @handle exit code is stored here
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.
319 * Return value: %TRUE, or %FALSE on error.
321 gboolean GetExitCodeThread(WapiHandle *handle, guint32 *exitcode)
323 struct _WapiHandle_thread *thread_handle=(struct _WapiHandle_thread *)handle;
326 g_message(G_GNUC_PRETTY_FUNCTION
327 ": Finding exit status for thread handle %p id %ld", handle,
328 thread_handle->thread->id);
333 g_message(G_GNUC_PRETTY_FUNCTION
334 ": Nowhere to store exit code");
339 if(thread_handle->state!=THREAD_STATE_EXITED) {
341 g_message(G_GNUC_PRETTY_FUNCTION
342 ": Thread still active (state %d, exited is %d)",
343 thread_handle->state, THREAD_STATE_EXITED);
345 *exitcode=STILL_ACTIVE;
349 *exitcode=thread_handle->exitstatus;
356 * @handle: the thread handle to resume
358 * Decrements the suspend count of thread @handle. A thread can only
359 * run if its suspend count is zero.
361 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
363 guint32 ResumeThread(WapiHandle *handle G_GNUC_UNUSED)
370 * @handle: the thread handle to suspend
372 * Increments the suspend count of thread @handle. A thread can only
373 * run if its suspend count is zero.
375 * Return value: the previous suspend count, or 0xFFFFFFFF on error.
377 guint32 SuspendThread(WapiHandle *handle G_GNUC_UNUSED)
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.
389 * Return value: The index value, or %TLS_OUT_OF_INDEXES if no index
392 guint32 TlsAlloc(void)
394 return(TLS_OUT_OF_INDEXES);
399 * @idx: The TLS index to free
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.
404 * Return value: %TRUE on success, %FALSE otherwise.
406 gboolean TlsFree(guint32 idx G_GNUC_UNUSED)
413 * @idx: The TLS index to retrieve
415 * Retrieves the TLS data stored under index @idx.
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.
421 gpointer TlsGetValue(guint32 idx G_GNUC_UNUSED)
428 * @idx: The TLS index to store
429 * @value: The value to store under index @idx
431 * Stores @value at TLS index @idx.
433 * Return value: %TRUE on success, %FALSE otherwise.
435 gboolean TlsSetValue(guint32 idx G_GNUC_UNUSED, gpointer value G_GNUC_UNUSED)
442 * @ms: The time in milliseconds to suspend for
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.
448 void Sleep(guint32 ms)
450 struct timespec req, rem;
455 g_message(G_GNUC_PRETTY_FUNCTION ": Sleeping for %d ms", ms);
463 /* FIXME: check for INFINITE and sleep forever */
464 divvy=div((int)ms, 1000);
466 req.tv_sec=divvy.quot;
467 req.tv_nsec=divvy.rem*1000;
470 ret=nanosleep(&req, &rem);
472 /* Sleep interrupted with rem time remaining */
474 guint32 rems=rem.tv_sec*1000 + rem.tv_nsec/1000;
476 g_message(G_GNUC_PRETTY_FUNCTION ": Still got %d ms to go",