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