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/.
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
48 #ifdef G_THREAD_USE_PID_SURROGATE
49 # include <sys/resource.h>
50 # define PID_IN_THREAD(thread) (*(pid_t*)(((gchar*)thread)+sizeof(pthread_t)))
51 # define SET_PRIO(pid, prio) \
52 posix_check_cmd_prio ((setpriority (PRIO_PROCESS, (pid), \
53 g_thread_priority_map [prio]) == -1) ? \
54 (errno == EACCES ? EPERM : errno ): 0)
55 #endif /* G_THREAD_USE_PID_SURROGATE */
57 #define posix_check_err(err, name) G_STMT_START{ \
60 g_error ("file %s: line %d (%s): error '%s' during '%s'", \
61 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
62 g_strerror (error), name); \
65 #define posix_check_cmd(cmd) posix_check_err (posix_error (cmd), #cmd)
68 static gboolean posix_check_cmd_prio_warned = FALSE;
69 # define posix_check_cmd_prio(cmd) G_STMT_START{ \
70 int err = posix_error (cmd); \
73 if (!posix_check_cmd_prio_warned) \
75 posix_check_cmd_prio_warned = TRUE; \
76 g_warning ("Priorities can only be changed " \
77 "(resp. increased) by root."); \
81 posix_check_err (err, #cmd); \
83 #else /* G_ENABLE_DEBUG */
84 # define posix_check_cmd_prio(cmd) G_STMT_START{ \
85 int err = posix_error (cmd); \
87 posix_check_err (err, #cmd); \
89 #endif /* G_ENABLE_DEBUG */
91 #if defined(G_THREADS_IMPL_POSIX)
92 # define posix_error(what) (what)
93 # define mutexattr_default NULL
94 # define condattr_default NULL
95 #elif defined(G_THREADS_IMPL_DCE)
96 # define posix_error(what) ((what) == -1 ? errno : 0)
97 # define pthread_key_create(a, b) pthread_keycreate (a, b)
98 # define pthread_attr_init(a) pthread_attr_create (a)
99 # define pthread_attr_destroy(a) pthread_attr_delete (a)
100 # define pthread_create(a, b, c, d) pthread_create (a, *b, c, d)
101 # define mutexattr_default (pthread_mutexattr_default)
102 # define condattr_default (pthread_condattr_default)
103 #else /* neither G_THREADS_IMPL_POSIX nor G_THREADS_IMPL_DCE are defined */
104 # error This should not happen. Contact the GLib team.
107 #ifdef G_THREAD_USE_PID_SURROGATE
108 # define PRIORITY_LOW_VALUE 15
109 # define PRIORITY_NORMAL_VALUE 0
110 # define PRIORITY_HIGH_VALUE -15
111 # define PRIORITY_URGENT_VALUE -20
112 #elif defined (POSIX_MIN_PRIORITY) && defined (POSIX_MAX_PRIORITY)
113 # define HAVE_PRIORITIES 1
114 # define PRIORITY_LOW_VALUE POSIX_MIN_PRIORITY
115 # define PRIORITY_URGENT_VALUE POSIX_MAX_PRIORITY
116 #endif /* POSIX_MIN_PRIORITY && POSIX_MAX_PRIORITY */
118 gulong g_thread_min_stack_size = 0;
120 #define G_MUTEX_SIZE (sizeof (pthread_mutex_t))
122 #ifdef _SC_THREAD_STACK_MIN
123 #define HAVE_G_THREAD_IMPL_INIT
127 g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0);
129 #endif /* _SC_THREAD_STACK_MIN */
132 g_mutex_new_posix_impl (void)
134 GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
135 posix_check_cmd (pthread_mutex_init ((pthread_mutex_t *) result,
141 g_mutex_free_posix_impl (GMutex * mutex)
143 posix_check_cmd (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
147 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
148 functions from gmem.c and gmessages.c; */
150 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
151 signature and semantic are right, but without error check then!!!!,
152 we might want to change this therefore. */
155 g_mutex_trylock_posix_impl (GMutex * mutex)
159 result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
161 #ifdef G_THREADS_IMPL_POSIX
164 #else /* G_THREADS_IMPL_DCE */
169 posix_check_err (posix_error (result), "pthread_mutex_trylock");
174 g_cond_new_posix_impl (void)
176 GCond *result = (GCond *) g_new (pthread_cond_t, 1);
177 posix_check_cmd (pthread_cond_init ((pthread_cond_t *) result,
182 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
183 can be taken directly, as signature and semantic are right, but
184 without error check then!!!!, we might want to change this
187 #define G_NSEC_PER_SEC 1000000000
190 g_cond_timed_wait_posix_impl (GCond * cond,
191 GMutex * entered_mutex,
195 struct timespec end_time;
198 g_return_val_if_fail (cond != NULL, FALSE);
199 g_return_val_if_fail (entered_mutex != NULL, FALSE);
203 g_cond_wait (cond, entered_mutex);
207 end_time.tv_sec = abs_time->tv_sec;
208 end_time.tv_nsec = abs_time->tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
209 g_assert (end_time.tv_nsec < G_NSEC_PER_SEC);
210 result = pthread_cond_timedwait ((pthread_cond_t *) cond,
211 (pthread_mutex_t *) entered_mutex,
214 #ifdef G_THREADS_IMPL_POSIX
215 timed_out = (result == ETIMEDOUT);
216 #else /* G_THREADS_IMPL_DCE */
217 timed_out = (result == -1) && (errno = EAGAIN);
221 posix_check_err (posix_error (result), "pthread_cond_timedwait");
226 g_cond_free_posix_impl (GCond * cond)
228 posix_check_cmd (pthread_cond_destroy ((pthread_cond_t *) cond));
233 g_private_new_posix_impl (GDestroyNotify destructor)
235 GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
236 posix_check_cmd (pthread_key_create ((pthread_key_t *) result, destructor));
240 /* NOTE: the functions g_private_get and g_private_set may not use
241 functions from gmem.c and gmessages.c */
244 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
248 pthread_setspecific (*(pthread_key_t *) private_key, value);
252 g_private_get_posix_impl (GPrivate * private_key)
256 #ifdef G_THREADS_IMPL_POSIX
257 return pthread_getspecific (*(pthread_key_t *) private_key);
258 #else /* G_THREADS_IMPL_DCE */
261 posix_check_cmd (pthread_getspecific (*(pthread_key_t *) private_key,
268 #ifdef G_THREAD_USE_PID_SURROGATE
271 GThreadFunc thread_func;
274 GThreadPriority priority;
278 g_thread_create_posix_impl_proxy (struct proxy_data *data)
280 GThreadFunc thread_func = data->thread_func;
281 GThreadFunc arg = data->arg;
282 PID_IN_THREAD (data->thread) = getpid();
283 SET_PRIO (PID_IN_THREAD (data->thread), data->priority);
287 #endif /* G_THREAD_USE_PID_SURROGATE */
290 g_thread_create_posix_impl (GThreadFunc thread_func,
295 GThreadPriority priority,
302 g_return_if_fail (thread_func);
303 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
304 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
306 posix_check_cmd (pthread_attr_init (&attr));
308 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
311 stack_size = MAX (g_thread_min_stack_size, stack_size);
312 posix_check_cmd (pthread_attr_setstacksize (&attr, stack_size));
314 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
316 #ifdef PTHREAD_SCOPE_SYSTEM
318 /* No error check here, because some systems can't do it and we
319 * simply don't want threads to fail because of that. */
320 pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
321 #endif /* PTHREAD_SCOPE_SYSTEM */
323 #ifdef G_THREADS_IMPL_POSIX
324 posix_check_cmd (pthread_attr_setdetachstate (&attr,
325 joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED));
326 #endif /* G_THREADS_IMPL_POSIX */
328 #ifdef G_THREAD_USE_PID_SURROGATE
330 struct proxy_data *data = g_new (struct proxy_data, 1);
331 data->thread_func = thread_func;
333 data->thread = thread;
334 data->priority = priority;
335 PID_IN_THREAD (thread) = 0;
336 ret = posix_error (pthread_create (thread, &attr, (void* (*)(void*))
337 g_thread_create_posix_impl_proxy,
340 #else /* G_THREAD_USE_PID_SURROGATE */
341 # ifdef HAVE_PRIORITIES
342 # ifdef G_THREADS_IMPL_POSIX
344 struct sched_param sched;
345 posix_check_cmd (pthread_attr_getschedparam (&attr, &sched));
346 sched.sched_priority = g_thread_priority_map [priority];
347 posix_check_cmd_prio (pthread_attr_setschedparam (&attr, &sched));
349 # else /* G_THREADS_IMPL_DCE */
351 (pthread_attr_setprio (&attr, g_thread_priority_map [priority]));
352 # endif /* G_THREADS_IMPL_DCE */
353 # endif /* HAVE_PRIORITIES */
354 ret = posix_error (pthread_create (thread, &attr,
355 (void* (*)(void*))thread_func, arg));
356 #endif /* !G_THREAD_USE_PID_SURROGATE */
358 posix_check_cmd (pthread_attr_destroy (&attr));
362 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
363 "Error creating thread: %s", g_strerror (ret));
367 posix_check_err (ret, "pthread_create");
369 #ifdef G_THREADS_IMPL_DCE
371 posix_check_cmd (pthread_detach (thread));
372 #endif /* G_THREADS_IMPL_DCE */
376 g_thread_yield_posix_impl (void)
382 g_thread_join_posix_impl (gpointer thread)
385 posix_check_cmd (pthread_join (*(pthread_t*)thread, &ignore));
389 g_thread_exit_posix_impl (void)
395 g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
397 g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
398 g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
399 #ifdef HAVE_PRIORITIES
400 # ifdef G_THREADS_IMPL_POSIX
402 struct sched_param sched;
404 posix_check_cmd (pthread_getschedparam (*(pthread_t*)thread, &policy,
406 sched.sched_priority = g_thread_priority_map [priority];
407 posix_check_cmd_prio (pthread_setschedparam (*(pthread_t*)thread, policy,
410 # else /* G_THREADS_IMPL_DCE */
411 posix_check_cmd_prio (pthread_setprio (*(pthread_t*)thread,
412 g_thread_priority_map [priority]));
414 #elif defined (G_THREAD_USE_PID_SURROGATE)
415 /* If the addressed thread hasn't yet been able to provide it's pid,
416 * we ignore the request. Should be more than rare */
417 if (PID_IN_THREAD (thread) != 0)
418 SET_PRIO (PID_IN_THREAD (thread), priority);
419 #endif /* G_THREAD_USE_PID_SURROGATE */
423 g_thread_self_posix_impl (gpointer thread)
425 *(pthread_t*)thread = pthread_self();
426 #ifdef G_THREAD_USE_PID_SURROGATE
427 PID_IN_THREAD (thread) = getpid();
428 #endif /* G_THREAD_USE_PID_SURROGATE */
431 static GThreadFunctions g_thread_functions_for_glib_use_default =
433 g_mutex_new_posix_impl,
434 (void (*)(GMutex *)) pthread_mutex_lock,
435 g_mutex_trylock_posix_impl,
436 (void (*)(GMutex *)) pthread_mutex_unlock,
437 g_mutex_free_posix_impl,
438 g_cond_new_posix_impl,
439 (void (*)(GCond *)) pthread_cond_signal,
440 (void (*)(GCond *)) pthread_cond_broadcast,
441 (void (*)(GCond *, GMutex *)) pthread_cond_wait,
442 g_cond_timed_wait_posix_impl,
443 g_cond_free_posix_impl,
444 g_private_new_posix_impl,
445 g_private_get_posix_impl,
446 g_private_set_posix_impl,
447 g_thread_create_posix_impl,
448 g_thread_yield_posix_impl,
449 g_thread_join_posix_impl,
450 g_thread_exit_posix_impl,
451 g_thread_set_priority_posix_impl,
452 g_thread_self_posix_impl