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