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 Library 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 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library 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-1999. 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
41 #define posix_print_error( name, num ) \
42 g_error( "file %s: line %d (%s): error %s during %s", \
43 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
44 g_strerror((num)), #name )
46 #if defined(G_THREADS_IMPL_POSIX)
47 # define posix_check_for_error( what ) G_STMT_START{ \
49 if( error ) { posix_print_error( what, error ); } \
51 # define mutexattr_default NULL
52 # define condattr_default NULL
53 #elif defined(G_THREADS_IMPL_DCE)
54 # define posix_check_for_error( what ) G_STMT_START{ \
55 if( (what) == -1 ) { posix_print_error( what, errno ); } \
57 # define mutexattr_default (&pthread_mutexattr_default)
58 # define condattr_default (&pthread_condattr_default)
59 #else /* neither G_THREADS_IMPL_POSIX nor G_THREADS_IMPL_DCE are defined */
60 # error This should not happen. Contact the GLb team.
64 g_mutex_new_posix_impl (void)
66 GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
67 posix_check_for_error (pthread_mutex_init ((pthread_mutex_t *) result,
73 g_mutex_free_posix_impl (GMutex * mutex)
75 posix_check_for_error (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
79 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
80 functions from gmem.c and gmessages.c; */
82 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
83 signature and semantic are right, but without error check then!!!!,
84 we might want to change this therefore. */
87 g_mutex_trylock_posix_impl (GMutex * mutex)
91 result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
93 #ifdef G_THREADS_IMPL_POSIX
96 #else /* G_THREADS_IMPL_DCE */
101 posix_check_for_error (result);
106 g_cond_new_posix_impl (void)
108 GCond *result = (GCond *) g_new (pthread_cond_t, 1);
109 posix_check_for_error (pthread_cond_init ((pthread_cond_t *) result,
114 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
115 can be taken directly, as signature and semantic are right, but
116 without error check then!!!!, we might want to change this
119 #define G_MICROSEC 1000000
120 #define G_NANOSEC 1000000000
123 g_cond_timed_wait_posix_impl (GCond * cond,
124 GMutex * entered_mutex,
128 struct timespec end_time;
131 g_return_val_if_fail (cond != NULL, FALSE);
132 g_return_val_if_fail (entered_mutex != NULL, FALSE);
136 g_cond_wait (cond, entered_mutex);
140 end_time.tv_sec = abs_time->tv_sec;
141 end_time.tv_nsec = abs_time->tv_usec * (G_NANOSEC / G_MICROSEC);
142 g_assert (end_time.tv_nsec < G_NANOSEC);
143 result = pthread_cond_timedwait ((pthread_cond_t *) cond,
144 (pthread_mutex_t *) entered_mutex,
147 #ifdef G_THREADS_IMPL_POSIX
148 timed_out = (result == ETIMEDOUT);
149 #else /* G_THREADS_IMPL_DCE */
150 timed_out = (result == -1) && (errno = EAGAIN);
154 posix_check_for_error (result);
159 g_cond_free_posix_impl (GCond * cond)
161 posix_check_for_error (pthread_cond_destroy ((pthread_cond_t *) cond));
166 g_private_new_posix_impl (GDestroyNotify destructor)
168 GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
169 posix_check_for_error (pthread_key_create ((pthread_key_t *) result,
174 /* NOTE: the functions g_private_get and g_private_set may not use
175 functions from gmem.c and gmessages.c */
178 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
183 pthread_setspecific (*(pthread_key_t *) private_key, value);
187 g_private_get_posix_impl (GPrivate * private_key)
191 #ifdef G_THREADS_IMPL_POSIX
192 return pthread_getspecific (*(pthread_key_t *) private_key);
193 #else /* G_THREADS_IMPL_DCE */
196 posix_check_for_error (pthread_getspecific (*(pthread_key_t *)
197 private_key, &data));
203 static GThreadFunctions g_thread_functions_for_glib_use_default =
205 g_mutex_new_posix_impl,
206 (void (*)(GMutex *)) pthread_mutex_lock,
207 g_mutex_trylock_posix_impl,
208 (void (*)(GMutex *)) pthread_mutex_unlock,
209 g_mutex_free_posix_impl,
210 g_cond_new_posix_impl,
211 (void (*)(GCond *)) pthread_cond_signal,
212 (void (*)(GCond *)) pthread_cond_broadcast,
213 (void (*)(GCond *, GMutex *)) pthread_cond_wait,
214 g_cond_timed_wait_posix_impl,
215 g_cond_free_posix_impl,
216 g_private_new_posix_impl,
217 g_private_get_posix_impl,
218 g_private_set_posix_impl