1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gthread.c: posix thread system implementation
5 * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 /* The GMutex and GCond implementations in this file are some of the
31 * lowest-level code in GLib. All other parts of GLib (messages,
32 * memory, slices, etc) assume that they can freely use these facilities
33 * without risking recursion.
35 * As such, these functions are NOT permitted to call any other part of
38 * The thread manipulation functions (create, exit, join, etc.) have
39 * more freedom -- they can do as they please.
53 g_thread_abort (gint status,
54 const gchar *function)
56 fprintf (stderr, "GLib (gthread-posix.c): Unexpected error from C library during '%s': %s. Aborting.\n",
57 strerror (status), function);
63 g_mutex_init (GMutex *mutex)
67 if G_UNLIKELY ((status = pthread_mutex_init (&mutex->impl, NULL)) != 0)
68 g_thread_abort (status, "pthread_mutex_init");
72 g_mutex_clear (GMutex *mutex)
76 if G_UNLIKELY ((status = pthread_mutex_destroy (&mutex->impl)) != 0)
77 g_thread_abort (status, "pthread_mutex_destroy");
81 g_mutex_lock (GMutex *mutex)
85 /* temporary until we fix libglib */
89 if G_UNLIKELY ((status = pthread_mutex_lock (&mutex->impl)) != 0)
90 g_thread_abort (status, "pthread_mutex_lock");
94 g_mutex_unlock (GMutex *mutex)
98 /* temporary until we fix libglib */
102 if G_UNLIKELY ((status = pthread_mutex_unlock (&mutex->impl)) != 0)
103 g_thread_abort (status, "pthread_mutex_lock");
107 g_mutex_trylock (GMutex *mutex)
111 /* temporary until we fix libglib */
115 if G_LIKELY ((status = pthread_mutex_trylock (&mutex->impl)) == 0)
118 if G_UNLIKELY (status != EBUSY)
119 g_thread_abort (status, "pthread_mutex_trylock");
127 g_cond_init (GCond *cond)
131 if G_UNLIKELY ((status = pthread_cond_init (&cond->impl, NULL)) != 0)
132 g_thread_abort (status, "pthread_cond_init");
136 g_cond_clear (GCond *cond)
140 if G_UNLIKELY ((status = pthread_cond_destroy (&cond->impl)) != 0)
141 g_thread_abort (status, "pthread_cond_destroy");
145 g_cond_wait (GCond *cond,
150 if G_UNLIKELY ((status = pthread_cond_wait (&cond->impl, &mutex->impl)) != 0)
151 g_thread_abort (status, "pthread_cond_wait");
155 g_cond_signal (GCond *cond)
159 /* temporary until we fix libglib */
163 if G_UNLIKELY ((status = pthread_cond_signal (&cond->impl)) != 0)
164 g_thread_abort (status, "pthread_cond_signal");
168 g_cond_broadcast (GCond *cond)
172 /* temporary until we fix libglib */
176 if G_UNLIKELY ((status = pthread_cond_broadcast (&cond->impl)) != 0)
177 g_thread_abort (status, "pthread_cond_broadcast");
181 g_cond_timed_wait (GCond *cond,
185 struct timespec end_time;
188 if (abs_time == NULL)
190 g_cond_wait (cond, mutex);
194 end_time.tv_sec = abs_time->tv_sec;
195 end_time.tv_nsec = abs_time->tv_usec * 1000;
197 if ((status = pthread_cond_timedwait (&cond->impl, &mutex->impl, &end_time)) == 0)
200 if G_UNLIKELY (status != ETIMEDOUT)
201 g_thread_abort (status, "pthread_cond_timedwait");
207 g_cond_timedwait (GCond *cond,
211 struct timespec end_time;
214 end_time.tv_sec = abs_time / 1000000;
215 end_time.tv_nsec = (abs_time % 1000000) * 1000;
217 if ((status = pthread_cond_timedwait (&cond->impl, &mutex->impl, &end_time)) == 0)
220 if G_UNLIKELY (status != ETIMEDOUT)
221 g_thread_abort (status, "pthread_cond_timedwait");
229 #include "gthreadprivate.h"
234 #ifdef HAVE_SYS_TIME_H
235 # include <sys/time.h>
245 #define posix_check_err(err, name) G_STMT_START{ \
248 g_error ("file %s: line %d (%s): error '%s' during '%s'", \
249 __FILE__, __LINE__, G_STRFUNC, \
250 g_strerror (error), name); \
253 #define posix_check_cmd(cmd) posix_check_err (cmd, #cmd)
255 #ifdef G_ENABLE_DEBUG
256 static gboolean posix_check_cmd_prio_warned = FALSE;
257 # define posix_check_cmd_prio(cmd) G_STMT_START{ \
261 if (!posix_check_cmd_prio_warned) \
263 posix_check_cmd_prio_warned = TRUE; \
264 g_warning ("Priorities can only be changed " \
265 "(resp. increased) by root."); \
269 posix_check_err (err, #cmd); \
271 #else /* G_ENABLE_DEBUG */
272 # define posix_check_cmd_prio(cmd) G_STMT_START{ \
275 posix_check_err (err, #cmd); \
277 #endif /* G_ENABLE_DEBUG */
279 #if defined (POSIX_MIN_PRIORITY) && defined (POSIX_MAX_PRIORITY)
280 # define HAVE_PRIORITIES 1
281 static gint priority_normal_value;
283 /* FreeBSD threads use different priority values from the POSIX_
284 * defines so we just set them here. The corresponding macros
285 * PTHREAD_MIN_PRIORITY and PTHREAD_MAX_PRIORITY are implied to be
286 * exported by the docs, but they aren't.
288 # define PRIORITY_LOW_VALUE 0
289 # define PRIORITY_URGENT_VALUE 31
290 # else /* !__FreeBSD__ */
291 # define PRIORITY_LOW_VALUE POSIX_MIN_PRIORITY
292 # define PRIORITY_URGENT_VALUE POSIX_MAX_PRIORITY
293 # endif /* !__FreeBSD__ */
294 # define PRIORITY_NORMAL_VALUE priority_normal_value
296 # define PRIORITY_HIGH_VALUE \
297 ((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
300 g_thread_priority_map (GThreadPriority priority)
304 case G_THREAD_PRIORITY_LOW:
305 return PRIORITY_LOW_VALUE;
307 case G_THREAD_PRIORITY_NORMAL:
308 return PRIORITY_NORMAL_VALUE;
310 case G_THREAD_PRIORITY_HIGH:
311 return PRIORITY_HIGH_VALUE;
313 case G_THREAD_PRIORITY_URGENT:
314 return PRIORITY_URGENT_VALUE;
317 g_assert_not_reached ();
321 #endif /* POSIX_MIN_PRIORITY && POSIX_MAX_PRIORITY */
323 static gulong g_thread_min_stack_size = 0;
325 #define G_MUTEX_SIZE (sizeof (pthread_mutex_t))
328 _g_thread_impl_init(void)
330 #ifdef _SC_THREAD_STACK_MIN
331 g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0);
332 #endif /* _SC_THREAD_STACK_MIN */
333 #ifdef HAVE_PRIORITIES
335 struct sched_param sched;
337 posix_check_cmd (pthread_getschedparam (pthread_self(), &policy, &sched));
338 priority_normal_value = sched.sched_priority;
340 #endif /* HAVE_PRIORITIES */
344 g_private_new_posix_impl (GDestroyNotify destructor)
346 GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
347 posix_check_cmd (pthread_key_create ((pthread_key_t *) result, destructor));
351 /* NOTE: the functions g_private_get and g_private_set may not use
352 functions from gmem.c and gmessages.c */
355 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
359 pthread_setspecific (*(pthread_key_t *) private_key, value);
363 g_private_get_posix_impl (GPrivate * private_key)
368 return pthread_getspecific (*(pthread_key_t *) private_key);
375 g_thread_create_posix_impl (GThreadFunc thread_func,
380 GThreadPriority priority,
387 g_return_if_fail (thread_func);
388 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
389 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
391 posix_check_cmd (pthread_attr_init (&attr));
393 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
396 stack_size = MAX (g_thread_min_stack_size, stack_size);
397 /* No error check here, because some systems can't do it and
398 * we simply don't want threads to fail because of that. */
399 pthread_attr_setstacksize (&attr, stack_size);
401 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
403 #ifdef PTHREAD_SCOPE_SYSTEM
405 /* No error check here, because some systems can't do it and we
406 * simply don't want threads to fail because of that. */
407 pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
408 #endif /* PTHREAD_SCOPE_SYSTEM */
410 posix_check_cmd (pthread_attr_setdetachstate (&attr,
411 joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED));
413 #ifdef HAVE_PRIORITIES
415 struct sched_param sched;
416 posix_check_cmd (pthread_attr_getschedparam (&attr, &sched));
417 sched.sched_priority = g_thread_priority_map (priority);
418 posix_check_cmd_prio (pthread_attr_setschedparam (&attr, &sched));
420 #endif /* HAVE_PRIORITIES */
421 ret = pthread_create (thread, &attr, (void* (*)(void*))thread_func, arg);
423 posix_check_cmd (pthread_attr_destroy (&attr));
427 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
428 "Error creating thread: %s", g_strerror (ret));
432 posix_check_err (ret, "pthread_create");
436 g_thread_yield_posix_impl (void)
442 g_thread_join_posix_impl (gpointer thread)
445 posix_check_cmd (pthread_join (*(pthread_t*)thread, &ignore));
449 g_thread_exit_posix_impl (void)
455 g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
457 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
458 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
459 #ifdef HAVE_PRIORITIES
461 struct sched_param sched;
463 posix_check_cmd (pthread_getschedparam (*(pthread_t*)thread, &policy,
465 sched.sched_priority = g_thread_priority_map (priority);
466 posix_check_cmd_prio (pthread_setschedparam (*(pthread_t*)thread, policy,
469 #endif /* HAVE_PRIORITIES */
473 g_thread_self_posix_impl (gpointer thread)
475 *(pthread_t*)thread = pthread_self();
479 g_thread_equal_posix_impl (gpointer thread1, gpointer thread2)
481 return (pthread_equal (*(pthread_t*)thread1, *(pthread_t*)thread2) != 0);
485 GThreadFunctions g_thread_functions_for_glib_use =
498 g_private_new_posix_impl,
499 g_private_get_posix_impl,
500 g_private_set_posix_impl,
501 g_thread_create_posix_impl,
502 g_thread_yield_posix_impl,
503 g_thread_join_posix_impl,
504 g_thread_exit_posix_impl,
505 g_thread_set_priority_posix_impl,
506 g_thread_self_posix_impl,
507 g_thread_equal_posix_impl
510 /* vim:set foldmethod=marker: */