Remove excessive shortcut key matching in ToolStrip
[mono.git] / mono / utils / mono-threads-posix.c
1 /*
2  * mono-threads-posix.c: Low-level threading, posix version
3  *
4  * Author:
5  *      Rodrigo Kumpera (kumpera@gmail.com)
6  *
7  * (C) 2011 Novell, Inc
8  */
9
10 #include <config.h>
11
12 #include <mono/utils/mono-compiler.h>
13 #include <mono/utils/mono-semaphore.h>
14 #include <mono/utils/mono-threads.h>
15 #include <mono/utils/mono-tls.h>
16 #include <mono/utils/mono-mmap.h>
17 #include <mono/metadata/threads-types.h>
18 #include <limits.h>
19
20 #include <errno.h>
21
22 #if defined(PLATFORM_ANDROID)
23 extern int tkill (pid_t tid, int signal);
24 #endif
25
26 #if defined(_POSIX_VERSION) || defined(__native_client__)
27 #include <sys/resource.h>
28 #include <signal.h>
29
30 #if defined(__native_client__)
31 void nacl_shutdown_gc_thread(void);
32 #endif
33
34 typedef struct {
35         void *(*start_routine)(void*);
36         void *arg;
37         int flags;
38         MonoSemType registered;
39         HANDLE handle;
40 } StartInfo;
41
42 static void*
43 inner_start_thread (void *arg)
44 {
45         StartInfo *start_info = arg;
46         void *t_arg = start_info->arg;
47         int res;
48         void *(*start_func)(void*) = start_info->start_routine;
49         guint32 flags = start_info->flags;
50         void *result;
51         HANDLE handle;
52         MonoThreadInfo *info;
53
54         /* Register the thread with the io-layer */
55         handle = wapi_create_thread_handle ();
56         if (!handle) {
57                 res = MONO_SEM_POST (&(start_info->registered));
58                 g_assert (!res);
59                 return NULL;
60         }
61         start_info->handle = handle;
62
63         info = mono_thread_info_attach (&result);
64         info->runtime_thread = TRUE;
65         info->handle = handle;
66
67         if (flags & CREATE_SUSPENDED) {
68                 info->create_suspended = TRUE;
69                 MONO_SEM_INIT (&info->create_suspended_sem, 0);
70         }
71
72         /* start_info is not valid after this */
73         res = MONO_SEM_POST (&(start_info->registered));
74         g_assert (!res);
75         start_info = NULL;
76
77         if (flags & CREATE_SUSPENDED) {
78                 while (MONO_SEM_WAIT (&info->create_suspended_sem) != 0 &&
79                            errno == EINTR);
80                 MONO_SEM_DESTROY (&info->create_suspended_sem);
81         }
82
83         /* Run the actual main function of the thread */
84         result = start_func (t_arg);
85
86         /*
87         mono_thread_info_detach ();
88         */
89
90 #if defined(__native_client__)
91         nacl_shutdown_gc_thread();
92 #endif
93
94         wapi_thread_handle_set_exited (handle, GPOINTER_TO_UINT (result));
95         /* This is needed by mono_threads_core_unregister () which is called later */
96         info->handle = NULL;
97
98         g_assert (mono_threads_get_callbacks ()->thread_exit);
99         mono_threads_get_callbacks ()->thread_exit (NULL);
100         g_assert_not_reached ();
101         return result;
102 }
103
104 HANDLE
105 mono_threads_core_create_thread (LPTHREAD_START_ROUTINE start_routine, gpointer arg, guint32 stack_size, guint32 creation_flags, MonoNativeThreadId *out_tid)
106 {
107         pthread_attr_t attr;
108         int res;
109         pthread_t thread;
110         StartInfo start_info;
111
112         res = pthread_attr_init (&attr);
113         g_assert (!res);
114
115         if (stack_size == 0) {
116 #if HAVE_VALGRIND_MEMCHECK_H
117                 if (RUNNING_ON_VALGRIND)
118                         stack_size = 1 << 20;
119                 else
120                         stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
121 #else
122                 stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
123 #endif
124         }
125
126 #ifdef PTHREAD_STACK_MIN
127         if (stack_size < PTHREAD_STACK_MIN)
128                 stack_size = PTHREAD_STACK_MIN;
129 #endif
130
131 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
132         res = pthread_attr_setstacksize (&attr, stack_size);
133         g_assert (!res);
134 #endif
135
136         memset (&start_info, 0, sizeof (StartInfo));
137         start_info.start_routine = (gpointer)start_routine;
138         start_info.arg = arg;
139         start_info.flags = creation_flags;
140         MONO_SEM_INIT (&(start_info.registered), 0);
141
142         /* Actually start the thread */
143         res = mono_threads_get_callbacks ()->mono_gc_pthread_create (&thread, &attr, inner_start_thread, &start_info);
144         if (res) {
145                 MONO_SEM_DESTROY (&(start_info.registered));
146                 return NULL;
147         }
148
149         /* Wait until the thread register itself in various places */
150         while (MONO_SEM_WAIT (&(start_info.registered)) != 0) {
151                 /*if (EINTR != errno) ABORT("sem_wait failed"); */
152         }
153         MONO_SEM_DESTROY (&(start_info.registered));
154
155         if (out_tid)
156                 *out_tid = thread;
157
158         return start_info.handle;
159 }
160
161 /*
162  * mono_threads_core_resume_created:
163  *
164  *   Resume a newly created thread created using CREATE_SUSPENDED.
165  */
166 void
167 mono_threads_core_resume_created (MonoThreadInfo *info, MonoNativeThreadId tid)
168 {
169         MONO_SEM_POST (&info->create_suspended_sem);
170 }
171
172 gboolean
173 mono_threads_core_yield (void)
174 {
175         return sched_yield () == 0;
176 }
177
178 void
179 mono_threads_core_exit (int exit_code)
180 {
181         MonoThreadInfo *current = mono_thread_info_current ();
182
183 #if defined(__native_client__)
184         nacl_shutdown_gc_thread();
185 #endif
186
187         wapi_thread_handle_set_exited (current->handle, exit_code);
188
189         g_assert (mono_threads_get_callbacks ()->thread_exit);
190         mono_threads_get_callbacks ()->thread_exit (NULL);
191 }
192
193 void
194 mono_threads_core_unregister (MonoThreadInfo *info)
195 {
196         if (info->handle) {
197                 wapi_thread_handle_set_exited (info->handle, 0);
198                 info->handle = NULL;
199         }
200 }
201
202 HANDLE
203 mono_threads_core_open_handle (void)
204 {
205         MonoThreadInfo *info;
206
207         info = mono_thread_info_current ();
208         g_assert (info);
209
210         if (!info->handle)
211                 info->handle = wapi_create_thread_handle ();
212         else
213                 wapi_ref_thread_handle (info->handle);
214         return info->handle;
215 }
216
217 int
218 mono_threads_get_max_stack_size (void)
219 {
220         struct rlimit lim;
221
222         /* If getrlimit fails, we don't enforce any limits. */
223         if (getrlimit (RLIMIT_STACK, &lim))
224                 return INT_MAX;
225         /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
226         if (lim.rlim_max > (rlim_t)INT_MAX)
227                 return INT_MAX;
228         return (int)lim.rlim_max;
229 }
230
231 HANDLE
232 mono_threads_core_open_thread_handle (HANDLE handle, MonoNativeThreadId tid)
233 {
234         wapi_ref_thread_handle (handle);
235
236         return handle;
237 }
238
239 gpointer
240 mono_threads_core_prepare_interrupt (HANDLE thread_handle)
241 {
242         return wapi_prepare_interrupt_thread (thread_handle);
243 }
244
245 void
246 mono_threads_core_finish_interrupt (gpointer wait_handle)
247 {
248         wapi_finish_interrupt_thread (wait_handle);
249 }
250
251 void
252 mono_threads_core_self_interrupt (void)
253 {
254         wapi_self_interrupt ();
255 }
256
257 void
258 mono_threads_core_clear_interruption (void)
259 {
260         wapi_clear_interruption ();
261 }
262
263 #if !defined (__MACH__)
264
265 #if !defined(__native_client__)
266 static void
267 suspend_signal_handler (int _dummy, siginfo_t *info, void *context)
268 {
269         MonoThreadInfo *current = mono_thread_info_current ();
270         gboolean ret;
271         
272         if (current->syscall_break_signal) {
273                 current->syscall_break_signal = FALSE;
274                 return;
275         }
276
277         ret = mono_threads_get_runtime_callbacks ()->thread_state_init_from_sigctx (&current->suspend_state, context);
278
279         /* thread_state_init_from_sigctx return FALSE if the current thread is detaching and suspend can't continue. */
280         current->suspend_can_continue = ret;
281
282         MONO_SEM_POST (&current->begin_suspend_semaphore);
283
284         /* This thread is doomed, all we can do is give up and let the suspender recover. */
285         if (!ret)
286                 return;
287
288         while (MONO_SEM_WAIT (&current->resume_semaphore) != 0) {
289                 /*if (EINTR != errno) ABORT("sem_wait failed"); */
290         }
291
292         if (current->async_target) {
293 #if MONO_ARCH_HAS_MONO_CONTEXT
294                 MonoContext tmp = current->suspend_state.ctx;
295                 mono_threads_get_runtime_callbacks ()->setup_async_callback (&tmp, current->async_target, current->user_data);
296                 current->async_target = current->user_data = NULL;
297                 mono_monoctx_to_sigctx (&tmp, context);
298 #else
299                 g_error ("The new interruption machinery requires a working mono-context");
300 #endif
301         }
302
303         MONO_SEM_POST (&current->finish_resume_semaphore);
304 }
305 #endif
306
307 static void
308 mono_posix_add_signal_handler (int signo, gpointer handler)
309 {
310 #if !defined(__native_client__)
311         /*FIXME, move the code from mini to utils and do the right thing!*/
312         struct sigaction sa;
313         struct sigaction previous_sa;
314         int ret;
315
316         sa.sa_sigaction = handler;
317         sigemptyset (&sa.sa_mask);
318         sa.sa_flags = SA_SIGINFO;
319         ret = sigaction (signo, &sa, &previous_sa);
320
321         g_assert (ret != -1);
322 #endif
323 }
324
325 void
326 mono_threads_init_platform (void)
327 {
328 #if !defined(__native_client__)
329         /*
330         FIXME we should use all macros from mini to make this more portable
331         FIXME it would be very sweet if sgen could end up using this too.
332         */
333         if (mono_thread_info_new_interrupt_enabled ())
334                 mono_posix_add_signal_handler (mono_thread_get_abort_signal (), suspend_signal_handler);
335 #endif
336 }
337
338 /*nothing to be done here since suspend always abort syscalls due using signals*/
339 void
340 mono_threads_core_interrupt (MonoThreadInfo *info)
341 {
342 }
343
344 int
345 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
346 {
347 #if defined (PLATFORM_ANDROID)
348         int result, old_errno = errno;
349         result = tkill (info->native_handle, signum);
350         if (result < 0) {
351                 result = errno;
352                 errno = old_errno;
353         }
354         return result;
355 #elif defined(__native_client__)
356         /* Workaround pthread_kill abort() in NaCl glibc. */
357         return 0;
358 #else
359         return pthread_kill (mono_thread_info_get_tid (info), signum);
360 #endif
361
362 }
363
364 void
365 mono_threads_core_abort_syscall (MonoThreadInfo *info)
366 {
367         /*
368         We signal a thread to break it from the urrent syscall.
369         This signal should not be interpreted as a suspend request.
370         */
371         info->syscall_break_signal = TRUE;
372         mono_threads_pthread_kill (info, mono_thread_get_abort_signal ());
373 }
374
375 gboolean
376 mono_threads_core_needs_abort_syscall (void)
377 {
378         return TRUE;
379 }
380
381 gboolean
382 mono_threads_core_suspend (MonoThreadInfo *info)
383 {
384         /*FIXME, check return value*/
385         mono_threads_pthread_kill (info, mono_thread_get_abort_signal ());
386         while (MONO_SEM_WAIT (&info->begin_suspend_semaphore) != 0) {
387                 /* g_assert (errno == EINTR); */
388         }
389         return info->suspend_can_continue;
390 }
391
392 gboolean
393 mono_threads_core_resume (MonoThreadInfo *info)
394 {
395         MONO_SEM_POST (&info->resume_semaphore);
396         while (MONO_SEM_WAIT (&info->finish_resume_semaphore) != 0) {
397                 /* g_assert (errno == EINTR); */
398         }
399
400         return TRUE;
401 }
402
403 void
404 mono_threads_platform_register (MonoThreadInfo *info)
405 {
406         MONO_SEM_INIT (&info->begin_suspend_semaphore, 0);
407
408 #if defined (PLATFORM_ANDROID)
409         info->native_handle = (gpointer) gettid ();
410 #endif
411 }
412
413 void
414 mono_threads_platform_free (MonoThreadInfo *info)
415 {
416         MONO_SEM_DESTROY (&info->begin_suspend_semaphore);
417 }
418
419 MonoNativeThreadId
420 mono_native_thread_id_get (void)
421 {
422         return pthread_self ();
423 }
424
425 gboolean
426 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
427 {
428         return pthread_equal (id1, id2);
429 }
430
431 /*
432  * mono_native_thread_create:
433  *
434  *   Low level thread creation function without any GC wrappers.
435  */
436 gboolean
437 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
438 {
439         return pthread_create (tid, NULL, func, arg) == 0;
440 }
441
442 void
443 mono_threads_core_set_name (MonoNativeThreadId tid, const char *name)
444 {
445 #ifdef HAVE_PTHREAD_SETNAME_NP
446         if (!name) {
447                 pthread_setname_np (tid, "");
448         } else {
449                 char n [16];
450
451                 strncpy (n, name, 16);
452                 n [15] = '\0';
453                 pthread_setname_np (tid, n);
454         }
455 #endif
456 }
457
458 #endif /*!defined (__MACH__)*/
459
460 #endif