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
44 #define posix_print_error( name, num ) \
45 g_error( "file %s: line %d (%s): error %s during %s", \
46 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
47 g_strerror((num)), #name )
49 #if defined(G_THREADS_IMPL_POSIX)
50 # define posix_check_for_error( what ) G_STMT_START{ \
52 if( error ) { posix_print_error( what, error ); } \
54 # define mutexattr_default NULL
55 # define condattr_default NULL
56 #elif defined(G_THREADS_IMPL_DCE)
57 # define posix_check_for_error( what ) G_STMT_START{ \
58 if( (what) == -1 ) { posix_print_error( what, errno ); } \
60 # define pthread_key_create(a, b) pthread_keycreate (a, b)
61 # define pthread_attr_init(a) pthread_attr_create (a)
62 # define pthread_attr_destroy(a) pthread_attr_delete (a)
63 # define pthread_create(a, b, c, d) pthread_create(a, *b, c, d)
64 # define mutexattr_default (pthread_mutexattr_default)
65 # define condattr_default (pthread_condattr_default)
66 #else /* neither G_THREADS_IMPL_POSIX nor G_THREADS_IMPL_DCE are defined */
67 # error This should not happen. Contact the GLib team.
70 #if defined(POSIX_MIN_PRIORITY) && defined(POSIX_MAX_PRIORITY)
71 # define HAVE_PRIORITIES 1
74 gulong g_thread_min_stack_size = 0;
76 #define HAVE_G_THREAD_IMPL_INIT
80 #ifdef HAVE_PRIORITIES
81 g_thread_min_priority = POSIX_MIN_PRIORITY;
82 g_thread_max_priority = POSIX_MAX_PRIORITY;
83 #endif /* HAVE_PRIORITIES */
84 #ifdef _SC_THREAD_STACK_MIN
85 g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0);
86 #endif /* _SC_THREAD_STACK_MIN */
90 g_mutex_new_posix_impl (void)
92 GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
93 posix_check_for_error (pthread_mutex_init ((pthread_mutex_t *) result,
99 g_mutex_free_posix_impl (GMutex * mutex)
101 posix_check_for_error (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
105 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
106 functions from gmem.c and gmessages.c; */
108 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
109 signature and semantic are right, but without error check then!!!!,
110 we might want to change this therefore. */
113 g_mutex_trylock_posix_impl (GMutex * mutex)
117 result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
119 #ifdef G_THREADS_IMPL_POSIX
122 #else /* G_THREADS_IMPL_DCE */
127 posix_check_for_error (result);
132 g_cond_new_posix_impl (void)
134 GCond *result = (GCond *) g_new (pthread_cond_t, 1);
135 posix_check_for_error (pthread_cond_init ((pthread_cond_t *) result,
140 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
141 can be taken directly, as signature and semantic are right, but
142 without error check then!!!!, we might want to change this
145 #define G_NSEC_PER_SEC 1000000000
148 g_cond_timed_wait_posix_impl (GCond * cond,
149 GMutex * entered_mutex,
153 struct timespec end_time;
156 g_return_val_if_fail (cond != NULL, FALSE);
157 g_return_val_if_fail (entered_mutex != NULL, FALSE);
161 g_cond_wait (cond, entered_mutex);
165 end_time.tv_sec = abs_time->tv_sec;
166 end_time.tv_nsec = abs_time->tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
167 g_assert (end_time.tv_nsec < G_NSEC_PER_SEC);
168 result = pthread_cond_timedwait ((pthread_cond_t *) cond,
169 (pthread_mutex_t *) entered_mutex,
172 #ifdef G_THREADS_IMPL_POSIX
173 timed_out = (result == ETIMEDOUT);
174 #else /* G_THREADS_IMPL_DCE */
175 timed_out = (result == -1) && (errno = EAGAIN);
179 posix_check_for_error (result);
184 g_cond_free_posix_impl (GCond * cond)
186 posix_check_for_error (pthread_cond_destroy ((pthread_cond_t *) cond));
191 g_private_new_posix_impl (GDestroyNotify destructor)
193 GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
194 posix_check_for_error (pthread_key_create ((pthread_key_t *) result,
199 /* NOTE: the functions g_private_get and g_private_set may not use
200 functions from gmem.c and gmessages.c */
203 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
207 pthread_setspecific (*(pthread_key_t *) private_key, value);
211 g_private_get_posix_impl (GPrivate * private_key)
215 #ifdef G_THREADS_IMPL_POSIX
216 return pthread_getspecific (*(pthread_key_t *) private_key);
217 #else /* G_THREADS_IMPL_DCE */
220 posix_check_for_error (pthread_getspecific (*(pthread_key_t *)
221 private_key, &data));
228 g_thread_create_posix_impl (GThreadFunc thread_func,
233 GThreadPriority priority,
240 g_return_if_fail (thread_func);
242 posix_check_for_error (pthread_attr_init (&attr));
244 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
247 stack_size = MAX (g_thread_min_stack_size, stack_size);
248 posix_check_for_error (pthread_attr_setstacksize (&attr, stack_size));
250 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
252 #ifdef PTHREAD_SCOPE_SYSTEM
254 /* No error check here, because some systems can't do it and we
255 * simply don't want threads to fail because of that. */
256 pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
257 #endif /* PTHREAD_SCOPE_SYSTEM */
259 #ifdef G_THREADS_IMPL_POSIX
260 posix_check_for_error (pthread_attr_setdetachstate (&attr,
261 joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED));
262 #endif /* G_THREADS_IMPL_POSIX */
264 #ifdef HAVE_PRIORITIES
265 # ifdef G_THREADS_IMPL_POSIX
267 struct sched_param sched;
268 posix_check_for_error (pthread_attr_getschedparam (&attr, &sched));
269 sched.sched_priority = g_thread_map_priority (priority);
270 posix_check_for_error (pthread_attr_setschedparam (&attr, &sched));
272 # else /* G_THREADS_IMPL_DCE */
273 posix_check_for_error
274 (pthread_attr_setprio (&attr, g_thread_map_priority (priority)));
275 # endif /* G_THREADS_IMPL_DCE */
276 #endif /* HAVE_PRIORITIES */
278 ret = pthread_create (thread, &attr, (void* (*)(void*))thread_func, arg);
280 #ifdef G_THREADS_IMPL_DCE
285 #endif /* G_THREADS_IMPL_DCE */
287 posix_check_for_error (pthread_attr_destroy (&attr));
291 g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN,
292 "Error creating thread: %s", g_strerror (ret));
296 posix_check_for_error (ret);
298 #ifdef G_THREADS_IMPL_DCE
300 posix_check_for_error (pthread_detach (thread));
301 #endif /* G_THREADS_IMPL_DCE */
305 g_thread_yield_posix_impl (void)
311 g_thread_join_posix_impl (gpointer thread)
314 posix_check_for_error (pthread_join (*(pthread_t*)thread,
319 g_thread_exit_posix_impl (void)
325 g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
327 #ifdef HAVE_PRIORITIES
328 # ifdef G_THREADS_IMPL_POSIX
329 struct sched_param sched;
331 posix_check_for_error (pthread_getschedparam (*(pthread_t*)thread,
333 sched.sched_priority = g_thread_map_priority (priority);
334 posix_check_for_error (pthread_setschedparam (*(pthread_t*)thread,
336 # else /* G_THREADS_IMPL_DCE */
337 posix_check_for_error (pthread_setprio (*(pthread_t*)thread,
338 g_thread_map_priority (priority)));
340 #endif /* HAVE_PRIORITIES */
344 g_thread_self_posix_impl (gpointer thread)
346 *(pthread_t*)thread = pthread_self();
349 static GThreadFunctions g_thread_functions_for_glib_use_default =
351 g_mutex_new_posix_impl,
352 (void (*)(GMutex *)) pthread_mutex_lock,
353 g_mutex_trylock_posix_impl,
354 (void (*)(GMutex *)) pthread_mutex_unlock,
355 g_mutex_free_posix_impl,
356 g_cond_new_posix_impl,
357 (void (*)(GCond *)) pthread_cond_signal,
358 (void (*)(GCond *)) pthread_cond_broadcast,
359 (void (*)(GCond *, GMutex *)) pthread_cond_wait,
360 g_cond_timed_wait_posix_impl,
361 g_cond_free_posix_impl,
362 g_private_new_posix_impl,
363 g_private_get_posix_impl,
364 g_private_set_posix_impl,
365 g_thread_create_posix_impl,
366 g_thread_yield_posix_impl,
367 g_thread_join_posix_impl,
368 g_thread_exit_posix_impl,
369 g_thread_set_priority_posix_impl,
370 g_thread_self_posix_impl