[threads] Remove mono_threads_suspend_needs_abort_syscall (#4450)
[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 /* For pthread_main_np, pthread_get_stackaddr_np and pthread_get_stacksize_np */
13 #if defined (__MACH__)
14 #define _DARWIN_C_SOURCE 1
15 #endif
16
17 #include <mono/utils/mono-threads.h>
18 #include <mono/utils/mono-coop-semaphore.h>
19 #include <mono/metadata/gc-internals.h>
20 #include <mono/utils/mono-threads-debug.h>
21
22 #include <errno.h>
23
24 #if defined(PLATFORM_ANDROID) && !defined(TARGET_ARM64) && !defined(TARGET_AMD64)
25 #define USE_TKILL_ON_ANDROID 1
26 #endif
27
28 #ifdef USE_TKILL_ON_ANDROID
29 extern int tkill (pid_t tid, int signal);
30 #endif
31
32 #if defined(_POSIX_VERSION) || defined(__native_client__)
33
34 #include <pthread.h>
35
36 #include <sys/resource.h>
37
38 #ifdef MONO_THREADS_PLATFORM_HAS_ATTR_SETSCHED
39 void
40 mono_threads_platform_reset_priority (pthread_attr_t *attr)
41 {
42         struct sched_param param;
43         gint res;
44         gint policy;
45
46         memset (&param, 0, sizeof (param));
47
48         res = pthread_attr_getschedpolicy (attr, &policy);
49         if (res != 0)
50                 g_error ("%s: pthread_attr_getschedpolicy failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
51
52 #ifdef _POSIX_PRIORITY_SCHEDULING
53         gint max, min;
54
55         /* Necessary to get valid priority range */
56
57         min = sched_get_priority_min (policy);
58         max = sched_get_priority_max (policy);
59
60         if (max > 0 && min >= 0 && max > min)
61                 param.sched_priority = (max - min) / 2 + min;
62         else
63 #endif
64         {
65                 switch (policy) {
66                 case SCHED_FIFO:
67                 case SCHED_RR:
68                         param.sched_priority = 50;
69                         break;
70 #ifdef SCHED_BATCH
71                 case SCHED_BATCH:
72 #endif
73                 case SCHED_OTHER:
74                         param.sched_priority = 0;
75                         break;
76                 default:
77                         g_warning ("%s: unknown policy %d", __func__, policy);
78                         return;
79                 }
80         }
81
82         res = pthread_attr_setschedparam (attr, &param);
83         if (res != 0)
84                 g_error ("%s: pthread_attr_setschedparam failed, error: \"%s\" (%d)", __func__, g_strerror (res), res);
85 }
86 #endif
87
88 int
89 mono_threads_platform_create_thread (MonoThreadStart thread_fn, gpointer thread_data, gsize* const stack_size, MonoNativeThreadId *out_tid)
90 {
91         pthread_attr_t attr;
92         pthread_t thread;
93         gint res;
94         gsize set_stack_size;
95         gsize min_stack_size;
96
97         res = pthread_attr_init (&attr);
98         g_assert (!res);
99
100         if (stack_size)
101                 set_stack_size = *stack_size;
102         else
103                 set_stack_size = 0;
104
105 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
106         if (set_stack_size == 0) {
107 #if HAVE_VALGRIND_MEMCHECK_H
108                 if (RUNNING_ON_VALGRIND)
109                         set_stack_size = 1 << 20;
110                 else
111                         set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
112 #else
113                 set_stack_size = (SIZEOF_VOID_P / 4) * 1024 * 1024;
114 #endif
115         }
116
117 #ifdef PTHREAD_STACK_MIN
118         if (set_stack_size < PTHREAD_STACK_MIN)
119                 set_stack_size = PTHREAD_STACK_MIN;
120 #endif
121
122         res = pthread_attr_setstacksize (&attr, set_stack_size);
123         g_assert (!res);
124 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
125
126         mono_threads_platform_reset_priority (&attr);
127
128         if (stack_size) {
129                 res = pthread_attr_getstacksize (&attr, &min_stack_size);
130                 if (res != 0)
131                         g_error ("%s: pthread_attr_getstacksize failed, error: \"%s\" (%d)", g_strerror (res), res);
132
133                 *stack_size = min_stack_size;
134         }
135
136         /* Actually start the thread */
137         res = mono_gc_pthread_create (&thread, &attr, (gpointer (*)(gpointer)) thread_fn, thread_data);
138         if (res)
139                 return -1;
140
141         if (out_tid)
142                 *out_tid = thread;
143
144         return 0;
145 }
146
147 void
148 mono_threads_platform_init (void)
149 {
150 }
151
152 gboolean
153 mono_threads_platform_in_critical_region (MonoNativeThreadId tid)
154 {
155         return FALSE;
156 }
157
158 gboolean
159 mono_threads_platform_yield (void)
160 {
161         return sched_yield () == 0;
162 }
163
164 void
165 mono_threads_platform_exit (gsize exit_code)
166 {
167         pthread_exit ((gpointer) exit_code);
168 }
169
170 int
171 mono_threads_get_max_stack_size (void)
172 {
173         struct rlimit lim;
174
175         /* If getrlimit fails, we don't enforce any limits. */
176         if (getrlimit (RLIMIT_STACK, &lim))
177                 return INT_MAX;
178         /* rlim_t is an unsigned long long on 64bits OSX but we want an int response. */
179         if (lim.rlim_max > (rlim_t)INT_MAX)
180                 return INT_MAX;
181         return (int)lim.rlim_max;
182 }
183
184 int
185 mono_threads_pthread_kill (MonoThreadInfo *info, int signum)
186 {
187         THREADS_SUSPEND_DEBUG ("sending signal %d to %p[%p]\n", signum, info, mono_thread_info_get_tid (info));
188 #ifdef USE_TKILL_ON_ANDROID
189         int result, old_errno = errno;
190         result = tkill (info->native_handle, signum);
191         if (result < 0) {
192                 result = errno;
193                 errno = old_errno;
194         }
195         return result;
196 #elif defined(__native_client__)
197         /* Workaround pthread_kill abort() in NaCl glibc. */
198         return 0;
199 #elif !defined(HAVE_PTHREAD_KILL)
200         g_error ("pthread_kill() is not supported by this platform");
201 #else
202         return pthread_kill (mono_thread_info_get_tid (info), signum);
203 #endif
204 }
205
206 MonoNativeThreadId
207 mono_native_thread_id_get (void)
208 {
209         return pthread_self ();
210 }
211
212 gboolean
213 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
214 {
215         return pthread_equal (id1, id2);
216 }
217
218 /*
219  * mono_native_thread_create:
220  *
221  *   Low level thread creation function without any GC wrappers.
222  */
223 gboolean
224 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
225 {
226         return pthread_create (tid, NULL, (void *(*)(void *)) func, arg) == 0;
227 }
228
229 void
230 mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
231 {
232 #ifdef __MACH__
233         /*
234          * We can't set the thread name for other threads, but we can at least make
235          * it work for threads that try to change their own name.
236          */
237         if (tid != mono_native_thread_id_get ())
238                 return;
239
240         if (!name) {
241                 pthread_setname_np ("");
242         } else {
243                 char n [63];
244
245                 memcpy (n, name, sizeof (n) - 1);
246                 n [sizeof (n) - 1] = '\0';
247                 pthread_setname_np (n);
248         }
249 #elif defined (__NetBSD__)
250         if (!name) {
251                 pthread_setname_np (tid, "%s", (void*)"");
252         } else {
253                 char n [PTHREAD_MAX_NAMELEN_NP];
254
255                 memcpy (n, name, sizeof (n) - 1);
256                 n [sizeof (n) - 1] = '\0';
257                 pthread_setname_np (tid, "%s", (void*)n);
258         }
259 #elif defined (HAVE_PTHREAD_SETNAME_NP)
260         if (!name) {
261                 pthread_setname_np (tid, "");
262         } else {
263                 char n [16];
264
265                 memcpy (n, name, sizeof (n) - 1);
266                 n [sizeof (n) - 1] = '\0';
267                 pthread_setname_np (tid, n);
268         }
269 #endif
270 }
271
272 gboolean
273 mono_native_thread_join (MonoNativeThreadId tid)
274 {
275         void *res;
276
277         return !pthread_join (tid, &res);
278 }
279
280 #endif /* defined(_POSIX_VERSION) || defined(__native_client__) */
281
282 #if defined(USE_POSIX_BACKEND)
283
284 gboolean
285 mono_threads_suspend_begin_async_suspend (MonoThreadInfo *info, gboolean interrupt_kernel)
286 {
287         int sig = interrupt_kernel ? mono_threads_suspend_get_abort_signal () :  mono_threads_suspend_get_suspend_signal ();
288
289         if (!mono_threads_pthread_kill (info, sig)) {
290                 mono_threads_add_to_pending_operation_set (info);
291                 return TRUE;
292         }
293         return FALSE;
294 }
295
296 gboolean
297 mono_threads_suspend_check_suspend_result (MonoThreadInfo *info)
298 {
299         return info->suspend_can_continue;
300 }
301
302 /*
303 This begins async resume. This function must do the following:
304
305 - Install an async target if one was requested.
306 - Notify the target to resume.
307 */
308 gboolean
309 mono_threads_suspend_begin_async_resume (MonoThreadInfo *info)
310 {
311         int sig = mono_threads_suspend_get_restart_signal ();
312
313         if (!mono_threads_pthread_kill (info, sig)) {
314                 mono_threads_add_to_pending_operation_set (info);
315                 return TRUE;
316         }
317         return FALSE;
318 }
319
320 void
321 mono_threads_suspend_abort_syscall (MonoThreadInfo *info)
322 {
323         /* We signal a thread to break it from the current syscall.
324          * This signal should not be interpreted as a suspend request. */
325         info->syscall_break_signal = TRUE;
326         if (mono_threads_pthread_kill (info, mono_threads_suspend_get_abort_signal ()) == 0) {
327                 mono_threads_add_to_pending_operation_set (info);
328         }
329 }
330
331 void
332 mono_threads_suspend_register (MonoThreadInfo *info)
333 {
334 #if defined (PLATFORM_ANDROID)
335         info->native_handle = gettid ();
336 #endif
337 }
338
339 void
340 mono_threads_suspend_free (MonoThreadInfo *info)
341 {
342 }
343
344 void
345 mono_threads_suspend_init (void)
346 {
347 }
348
349 #endif /* defined(USE_POSIX_BACKEND) */