|
1 /* GLIB - Library of useful routines for C programming |
|
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
|
3 * |
|
4 * gthread.c: posix thread system implementation |
|
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe |
|
6 * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved. |
|
7 * |
|
8 * This library is free software; you can redistribute it and/or |
|
9 * modify it under the terms of the GNU Lesser General Public |
|
10 * License as published by the Free Software Foundation; either |
|
11 * version 2 of the License, or (at your option) any later version. |
|
12 * |
|
13 * This library is distributed in the hope that it will be useful, |
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * Lesser General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU Lesser General Public |
|
19 * License along with this library; if not, write to the |
|
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
21 * Boston, MA 02111-1307, USA. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
|
26 * file for a list of people on the GLib Team. See the ChangeLog |
|
27 * files for a list of changes. These files are distributed with |
|
28 * GLib at ftp://ftp.gtk.org/pub/gtk/. |
|
29 */ |
|
30 |
|
31 /* |
|
32 * MT safe |
|
33 */ |
|
34 |
|
35 #include "config.h" |
|
36 |
|
37 #include <pthread.h> |
|
38 #include <errno.h> |
|
39 #include <stdlib.h> |
|
40 #ifdef HAVE_SYS_TIME_H |
|
41 # include <sys/time.h> |
|
42 #endif |
|
43 #ifdef HAVE_UNISTD_H |
|
44 # include <unistd.h> |
|
45 #endif |
|
46 |
|
47 #ifdef HAVE_SCHED_H |
|
48 #include <sched.h> |
|
49 #endif |
|
50 |
|
51 #define posix_check_err(err, name) G_STMT_START{ \ |
|
52 int error = (err); \ |
|
53 if (error) \ |
|
54 g_error ("file %s: line %d (%s): error '%s' during '%s'", \ |
|
55 __FILE__, __LINE__, G_STRFUNC, \ |
|
56 g_strerror (error), name); \ |
|
57 }G_STMT_END |
|
58 |
|
59 #define posix_check_cmd(cmd) posix_check_err (posix_error (cmd), #cmd) |
|
60 |
|
61 #ifdef G_ENABLE_DEBUG |
|
62 #if EMULATOR |
|
63 PLS(posix_check_cmd_prio_warned,gthread_posix,gboolean) |
|
64 #define posix_check_cmd_prio_warned (*FUNCTION_NAME(posix_check_cmd_prio_warned,gthread_posix)()) |
|
65 #else |
|
66 static gboolean posix_check_cmd_prio_warned = FALSE; |
|
67 #endif /* EMULATOR */ |
|
68 # define posix_check_cmd_prio(cmd) G_STMT_START{ \ |
|
69 int err = posix_error (cmd); \ |
|
70 if (err == EPERM) \ |
|
71 { \ |
|
72 if (!posix_check_cmd_prio_warned) \ |
|
73 { \ |
|
74 posix_check_cmd_prio_warned = TRUE; \ |
|
75 g_warning ("Priorities can only be changed " \ |
|
76 "(resp. increased) by root."); \ |
|
77 } \ |
|
78 } \ |
|
79 else \ |
|
80 posix_check_err (err, #cmd); \ |
|
81 }G_STMT_END |
|
82 #else /* G_ENABLE_DEBUG */ |
|
83 # define posix_check_cmd_prio(cmd) G_STMT_START{ \ |
|
84 int err = posix_error (cmd); \ |
|
85 if (err != EPERM) \ |
|
86 posix_check_err (err, #cmd); \ |
|
87 }G_STMT_END |
|
88 #endif /* G_ENABLE_DEBUG */ |
|
89 |
|
90 #if defined(G_THREADS_IMPL_POSIX) |
|
91 # define posix_error(what) (what) |
|
92 # define mutexattr_default NULL |
|
93 # define condattr_default NULL |
|
94 #elif defined(G_THREADS_IMPL_DCE) |
|
95 # define posix_error(what) ((what) == -1 ? errno : 0) |
|
96 # define pthread_key_create(a, b) pthread_keycreate (a, b) |
|
97 # define pthread_attr_init(a) pthread_attr_create (a) |
|
98 # define pthread_attr_destroy(a) pthread_attr_delete (a) |
|
99 # define pthread_create(a, b, c, d) pthread_create (a, *b, c, d) |
|
100 # define mutexattr_default (pthread_mutexattr_default) |
|
101 # define condattr_default (pthread_condattr_default) |
|
102 #else /* neither G_THREADS_IMPL_POSIX nor G_THREADS_IMPL_DCE are defined */ |
|
103 # error This should not happen. Contact the GLib team. |
|
104 #endif |
|
105 |
|
106 #if defined (POSIX_MIN_PRIORITY) && defined (POSIX_MAX_PRIORITY) |
|
107 # define HAVE_PRIORITIES 1 |
|
108 #if EMULATOR |
|
109 |
|
110 PLS(priority_normal_value,gthread_posix,gint) |
|
111 #define priority_normal_value (*FUNCTION_NAME(priority_normal_value,gthread_posix)()) |
|
112 |
|
113 #else |
|
114 |
|
115 static gint priority_normal_value; |
|
116 |
|
117 #endif /* EMULATOR */ |
|
118 # ifdef __FreeBSD__ |
|
119 /* FreeBSD threads use different priority values from the POSIX_ |
|
120 * defines so we just set them here. The corresponding macros |
|
121 * PTHREAD_MIN_PRIORITY and PTHREAD_MAX_PRIORITY are implied to be |
|
122 * exported by the docs, but they aren't. |
|
123 */ |
|
124 # define PRIORITY_LOW_VALUE 0 |
|
125 # define PRIORITY_URGENT_VALUE 31 |
|
126 # else /* !__FreeBSD__ */ |
|
127 # define PRIORITY_LOW_VALUE POSIX_MIN_PRIORITY |
|
128 # define PRIORITY_URGENT_VALUE POSIX_MAX_PRIORITY |
|
129 # endif /* !__FreeBSD__ */ |
|
130 # define PRIORITY_NORMAL_VALUE priority_normal_value |
|
131 #endif /* POSIX_MIN_PRIORITY && POSIX_MAX_PRIORITY */ |
|
132 |
|
133 #if EMULATOR |
|
134 |
|
135 PLS(g_thread_min_stack_size ,gthread_posix,gulong) |
|
136 #define g_thread_min_stack_size (*FUNCTION_NAME(g_thread_min_stack_size ,gthread_posix)()) |
|
137 |
|
138 #else |
|
139 |
|
140 static gulong g_thread_min_stack_size = 0; |
|
141 |
|
142 #endif /* EMULATOR */ |
|
143 #define G_MUTEX_SIZE (sizeof (pthread_mutex_t)) |
|
144 |
|
145 #if defined(HAVE_CLOCK_GETTIME) && defined(HAVE_MONOTONIC_CLOCK) |
|
146 #define USE_CLOCK_GETTIME 1 |
|
147 static gint posix_clock = 0; |
|
148 #endif |
|
149 |
|
150 #if defined(_SC_THREAD_STACK_MIN) || defined (HAVE_PRIORITIES) || defined (USE_CLOCK_GETTIME) |
|
151 #define HAVE_G_THREAD_IMPL_INIT |
|
152 static void |
|
153 g_thread_impl_init(void) |
|
154 { |
|
155 #ifdef _SC_THREAD_STACK_MIN |
|
156 g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0); |
|
157 #endif /* _SC_THREAD_STACK_MIN */ |
|
158 #ifdef HAVE_PRIORITIES |
|
159 # ifdef G_THREADS_IMPL_POSIX |
|
160 { |
|
161 struct sched_param sched; |
|
162 int policy; |
|
163 posix_check_cmd (pthread_getschedparam (pthread_self(), &policy, &sched)); |
|
164 priority_normal_value = sched.sched_priority; |
|
165 } |
|
166 # else /* G_THREADS_IMPL_DCE */ |
|
167 posix_check_cmd (priority_normal_value = |
|
168 pthread_getprio (*(pthread_t*)thread, |
|
169 g_thread_priority_map [priority])); |
|
170 # endif |
|
171 #endif /* HAVE_PRIORITIES */ |
|
172 |
|
173 #ifdef USE_CLOCK_GETTIME |
|
174 if (sysconf (_SC_MONOTONIC_CLOCK) >= 0) |
|
175 posix_clock = CLOCK_MONOTONIC; |
|
176 else |
|
177 posix_clock = CLOCK_REALTIME; |
|
178 #endif |
|
179 } |
|
180 #endif /* _SC_THREAD_STACK_MIN || HAVE_PRIORITIES */ |
|
181 |
|
182 static GMutex * |
|
183 g_mutex_new_posix_impl (void) |
|
184 { |
|
185 GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1); |
|
186 posix_check_cmd (pthread_mutex_init ((pthread_mutex_t *) result, |
|
187 mutexattr_default)); |
|
188 return result; |
|
189 } |
|
190 |
|
191 static void |
|
192 g_mutex_free_posix_impl (GMutex * mutex) |
|
193 { |
|
194 posix_check_cmd (pthread_mutex_destroy ((pthread_mutex_t *) mutex)); |
|
195 g_free (mutex); |
|
196 } |
|
197 |
|
198 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use |
|
199 functions from gmem.c and gmessages.c; */ |
|
200 |
|
201 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as |
|
202 signature and semantic are right, but without error check then!!!!, |
|
203 we might want to change this therefore. */ |
|
204 |
|
205 static gboolean |
|
206 g_mutex_trylock_posix_impl (GMutex * mutex) |
|
207 { |
|
208 int result; |
|
209 |
|
210 result = pthread_mutex_trylock ((pthread_mutex_t *) mutex); |
|
211 |
|
212 #ifdef G_THREADS_IMPL_POSIX |
|
213 if (result == EBUSY) |
|
214 return FALSE; |
|
215 #else /* G_THREADS_IMPL_DCE */ |
|
216 if (result == 0) |
|
217 return FALSE; |
|
218 #endif |
|
219 |
|
220 posix_check_err (posix_error (result), "pthread_mutex_trylock"); |
|
221 return TRUE; |
|
222 } |
|
223 |
|
224 static GCond * |
|
225 g_cond_new_posix_impl (void) |
|
226 { |
|
227 GCond *result = (GCond *) g_new (pthread_cond_t, 1); |
|
228 posix_check_cmd (pthread_cond_init ((pthread_cond_t *) result, |
|
229 condattr_default)); |
|
230 return result; |
|
231 } |
|
232 |
|
233 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait |
|
234 can be taken directly, as signature and semantic are right, but |
|
235 without error check then!!!!, we might want to change this |
|
236 therefore. */ |
|
237 |
|
238 #define G_NSEC_PER_SEC 1000000000 |
|
239 |
|
240 static gboolean |
|
241 g_cond_timed_wait_posix_impl (GCond * cond, |
|
242 GMutex * entered_mutex, |
|
243 GTimeVal * abs_time) |
|
244 { |
|
245 int result; |
|
246 struct timespec end_time; |
|
247 gboolean timed_out; |
|
248 |
|
249 g_return_val_if_fail (cond != NULL, FALSE); |
|
250 g_return_val_if_fail (entered_mutex != NULL, FALSE); |
|
251 |
|
252 if (!abs_time) |
|
253 { |
|
254 result = pthread_cond_wait ((pthread_cond_t *)cond, |
|
255 (pthread_mutex_t *) entered_mutex); |
|
256 timed_out = FALSE; |
|
257 } |
|
258 else |
|
259 { |
|
260 end_time.tv_sec = abs_time->tv_sec; |
|
261 end_time.tv_nsec = abs_time->tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC); |
|
262 |
|
263 g_return_val_if_fail (end_time.tv_nsec < G_NSEC_PER_SEC, TRUE); |
|
264 |
|
265 result = pthread_cond_timedwait ((pthread_cond_t *) cond, |
|
266 (pthread_mutex_t *) entered_mutex, |
|
267 &end_time); |
|
268 #ifdef G_THREADS_IMPL_POSIX |
|
269 timed_out = (result == ETIMEDOUT); |
|
270 #else /* G_THREADS_IMPL_DCE */ |
|
271 timed_out = (result == -1) && (errno == EAGAIN); |
|
272 #endif |
|
273 } |
|
274 |
|
275 if (!timed_out) |
|
276 posix_check_err (posix_error (result), "pthread_cond_timedwait"); |
|
277 |
|
278 return !timed_out; |
|
279 } |
|
280 |
|
281 static void |
|
282 g_cond_free_posix_impl (GCond * cond) |
|
283 { |
|
284 posix_check_cmd (pthread_cond_destroy ((pthread_cond_t *) cond)); |
|
285 g_free (cond); |
|
286 } |
|
287 |
|
288 static GPrivate * |
|
289 g_private_new_posix_impl (GDestroyNotify destructor) |
|
290 { |
|
291 GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1); |
|
292 posix_check_cmd (pthread_key_create ((pthread_key_t *) result, destructor)); |
|
293 return result; |
|
294 } |
|
295 |
|
296 /* NOTE: the functions g_private_get and g_private_set may not use |
|
297 functions from gmem.c and gmessages.c */ |
|
298 |
|
299 static void |
|
300 g_private_set_posix_impl (GPrivate * private_key, gpointer value) |
|
301 { |
|
302 if (!private_key) |
|
303 return; |
|
304 pthread_setspecific (*(pthread_key_t *) private_key, value); |
|
305 } |
|
306 |
|
307 static gpointer |
|
308 g_private_get_posix_impl (GPrivate * private_key) |
|
309 { |
|
310 if (!private_key) |
|
311 return NULL; |
|
312 #ifdef G_THREADS_IMPL_POSIX |
|
313 return pthread_getspecific (*(pthread_key_t *) private_key); |
|
314 #else /* G_THREADS_IMPL_DCE */ |
|
315 { |
|
316 void* data; |
|
317 posix_check_cmd (pthread_getspecific (*(pthread_key_t *) private_key, |
|
318 &data)); |
|
319 return data; |
|
320 } |
|
321 #endif |
|
322 } |
|
323 |
|
324 static void |
|
325 g_thread_create_posix_impl (GThreadFunc thread_func, |
|
326 gpointer arg, |
|
327 gulong stack_size, |
|
328 gboolean joinable, |
|
329 gboolean bound, |
|
330 GThreadPriority priority, |
|
331 gpointer thread, |
|
332 GError **error) |
|
333 { |
|
334 pthread_attr_t attr; |
|
335 gint ret; |
|
336 |
|
337 g_return_if_fail (thread_func); |
|
338 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW); |
|
339 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT); |
|
340 |
|
341 posix_check_cmd (pthread_attr_init (&attr)); |
|
342 |
|
343 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE |
|
344 if (stack_size) |
|
345 { |
|
346 stack_size = MAX (g_thread_min_stack_size, stack_size); |
|
347 /* No error check here, because some systems can't do it and |
|
348 * we simply don't want threads to fail because of that. */ |
|
349 pthread_attr_setstacksize (&attr, stack_size); |
|
350 } |
|
351 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */ |
|
352 |
|
353 #ifdef PTHREAD_SCOPE_SYSTEM |
|
354 if (bound) |
|
355 /* No error check here, because some systems can't do it and we |
|
356 * simply don't want threads to fail because of that. */ |
|
357 pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM); |
|
358 #endif /* PTHREAD_SCOPE_SYSTEM */ |
|
359 |
|
360 #ifdef G_THREADS_IMPL_POSIX |
|
361 posix_check_cmd (pthread_attr_setdetachstate (&attr, |
|
362 joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED)); |
|
363 #endif /* G_THREADS_IMPL_POSIX */ |
|
364 |
|
365 #ifdef HAVE_PRIORITIES |
|
366 # ifdef G_THREADS_IMPL_POSIX |
|
367 { |
|
368 struct sched_param sched; |
|
369 posix_check_cmd (pthread_attr_getschedparam (&attr, &sched)); |
|
370 sched.sched_priority = g_thread_priority_map [priority]; |
|
371 posix_check_cmd_prio (pthread_attr_setschedparam (&attr, &sched)); |
|
372 } |
|
373 # else /* G_THREADS_IMPL_DCE */ |
|
374 posix_check_cmd_prio |
|
375 (pthread_attr_setprio (&attr, g_thread_priority_map [priority])); |
|
376 # endif /* G_THREADS_IMPL_DCE */ |
|
377 #endif /* HAVE_PRIORITIES */ |
|
378 ret = posix_error (pthread_create (thread, &attr, |
|
379 (void* (*)(void*))thread_func, arg)); |
|
380 |
|
381 posix_check_cmd (pthread_attr_destroy (&attr)); |
|
382 |
|
383 if (ret == EAGAIN) |
|
384 { |
|
385 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN, |
|
386 "Error creating thread: %s", g_strerror (ret)); |
|
387 return; |
|
388 } |
|
389 |
|
390 posix_check_err (ret, "pthread_create"); |
|
391 |
|
392 #ifdef G_THREADS_IMPL_DCE |
|
393 if (!joinable) |
|
394 posix_check_cmd (pthread_detach (thread)); |
|
395 #endif /* G_THREADS_IMPL_DCE */ |
|
396 } |
|
397 |
|
398 static void |
|
399 g_thread_yield_posix_impl (void) |
|
400 { |
|
401 POSIX_YIELD_FUNC; |
|
402 } |
|
403 |
|
404 static void |
|
405 g_thread_join_posix_impl (gpointer thread) |
|
406 { |
|
407 gpointer ignore; |
|
408 posix_check_cmd (pthread_join (*(pthread_t*)thread, &ignore)); |
|
409 } |
|
410 |
|
411 static void |
|
412 g_thread_exit_posix_impl (void) |
|
413 { |
|
414 pthread_exit (NULL); |
|
415 } |
|
416 |
|
417 static void |
|
418 g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority) |
|
419 { |
|
420 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW); |
|
421 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT); |
|
422 #ifdef HAVE_PRIORITIES |
|
423 # ifdef G_THREADS_IMPL_POSIX |
|
424 { |
|
425 struct sched_param sched; |
|
426 int policy; |
|
427 posix_check_cmd (pthread_getschedparam (*(pthread_t*)thread, &policy, |
|
428 &sched)); |
|
429 sched.sched_priority = g_thread_priority_map [priority]; |
|
430 posix_check_cmd_prio (pthread_setschedparam (*(pthread_t*)thread, policy, |
|
431 &sched)); |
|
432 } |
|
433 # else /* G_THREADS_IMPL_DCE */ |
|
434 posix_check_cmd_prio (pthread_setprio (*(pthread_t*)thread, |
|
435 g_thread_priority_map [priority])); |
|
436 # endif |
|
437 #endif /* HAVE_PRIORITIES */ |
|
438 } |
|
439 |
|
440 static void |
|
441 g_thread_self_posix_impl (gpointer thread) |
|
442 { |
|
443 *(pthread_t*)thread = pthread_self(); |
|
444 } |
|
445 |
|
446 static gboolean |
|
447 g_thread_equal_posix_impl (gpointer thread1, gpointer thread2) |
|
448 { |
|
449 return (pthread_equal (*(pthread_t*)thread1, *(pthread_t*)thread2) != 0); |
|
450 } |
|
451 |
|
452 #ifdef USE_CLOCK_GETTIME |
|
453 static guint64 |
|
454 gettime (void) |
|
455 { |
|
456 struct timespec tv; |
|
457 |
|
458 clock_gettime (posix_clock, &tv); |
|
459 |
|
460 return (guint64) tv.tv_sec * G_NSEC_PER_SEC + tv.tv_nsec; |
|
461 } |
|
462 static guint64 (*g_thread_gettime_impl)(void) = gettime; |
|
463 #else |
|
464 static guint64 (*g_thread_gettime_impl)(void) = 0; |
|
465 #endif |
|
466 |
|
467 #if EMULATOR |
|
468 PLS(g_thread_functions_for_glib_use_default ,gthread_posix,GThreadFunctions) |
|
469 #define g_thread_functions_for_glib_use_default (*FUNCTION_NAME(g_thread_functions_for_glib_use_default ,gthread_posix)()) |
|
470 #else |
|
471 static GThreadFunctions g_thread_functions_for_glib_use_default = |
|
472 { |
|
473 NULL,//g_mutex_new_posix_impl, |
|
474 NULL,//(void (*)(GMutex *)) pthread_mutex_lock, |
|
475 NULL,//g_mutex_trylock_posix_impl, |
|
476 NULL,//(void (*)(GMutex *)) pthread_mutex_unlock, |
|
477 NULL,//g_mutex_free_posix_impl, |
|
478 NULL,//g_cond_new_posix_impl, |
|
479 NULL,//(void (*)(GCond *)) pthread_cond_signal, |
|
480 NULL,//(void (*)(GCond *)) pthread_cond_broadcast, |
|
481 NULL,//(void (*)(GCond *, GMutex *)) pthread_cond_wait, |
|
482 NULL,//g_cond_timed_wait_posix_impl, |
|
483 NULL,//g_cond_free_posix_impl, |
|
484 NULL,//g_private_new_posix_impl, |
|
485 NULL,//g_private_get_posix_impl, |
|
486 NULL,//g_private_set_posix_impl, |
|
487 NULL,//g_thread_create_posix_impl, |
|
488 NULL,//g_thread_yield_posix_impl, |
|
489 NULL,//g_thread_join_posix_impl, |
|
490 NULL,//g_thread_exit_posix_impl, |
|
491 NULL,//g_thread_set_priority_posix_impl, |
|
492 NULL,//g_thread_self_posix_impl, |
|
493 NULL,//g_thread_equal_posix_impl |
|
494 }; |
|
495 #endif//EMULATOR |
|
496 |
|
497 #ifdef __SYMBIAN32__ |
|
498 const GThreadFunctions g_thread_functions_for_glib_use_default_temp = |
|
499 { |
|
500 g_mutex_new_posix_impl, |
|
501 (void (*)(GMutex *)) pthread_mutex_lock, |
|
502 g_mutex_trylock_posix_impl, |
|
503 (void (*)(GMutex *)) pthread_mutex_unlock, |
|
504 g_mutex_free_posix_impl, |
|
505 g_cond_new_posix_impl, |
|
506 (void (*)(GCond *)) pthread_cond_signal, |
|
507 (void (*)(GCond *)) pthread_cond_broadcast, |
|
508 (void (*)(GCond *, GMutex *)) pthread_cond_wait, |
|
509 g_cond_timed_wait_posix_impl, |
|
510 g_cond_free_posix_impl, |
|
511 g_private_new_posix_impl, |
|
512 g_private_get_posix_impl, |
|
513 g_private_set_posix_impl, |
|
514 g_thread_create_posix_impl, |
|
515 g_thread_yield_posix_impl, |
|
516 g_thread_join_posix_impl, |
|
517 g_thread_exit_posix_impl, |
|
518 g_thread_set_priority_posix_impl, |
|
519 g_thread_self_posix_impl, |
|
520 g_thread_equal_posix_impl |
|
521 }; |
|
522 #endif//__SYMBIAN32__ |