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.
30 #ifdef HAVE_SYS_TIME_H
34 #define posix_print_error( name, num ) \
35 g_error( "file %s: line %d (%s): error %s during %s", \
36 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
37 g_strerror((num)), #name )
39 #define posix_check_for_error( what ) G_STMT_START{ \
41 if( error ) { posix_print_error( what, error ); } \
45 g_mutex_new_posix_impl (void)
47 GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
48 posix_check_for_error (pthread_mutex_init ((pthread_mutex_t *) result, NULL));
53 g_mutex_free_posix_impl (GMutex * mutex)
55 posix_check_for_error (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
59 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
60 functions from gmem.c and gmessages.c; */
62 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
63 signature and semantic are right, but without error check then!!!!,
64 we might want to change this therefore. */
67 g_mutex_trylock_posix_impl (GMutex * mutex)
71 result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
75 posix_check_for_error (result);
80 g_cond_new_posix_impl (void)
82 GCond *result = (GCond *) g_new (pthread_cond_t, 1);
83 posix_check_for_error (pthread_cond_init ((pthread_cond_t *) result, NULL));
87 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
88 can be taken directly, as signature and semantic are right, but
89 without error check then!!!!, we might want to change this
92 #define G_MICROSEC 1000000
93 #define G_NANOSEC 1000000000
96 g_cond_timed_wait_posix_impl (GCond * cond,
97 GMutex * entered_mutex,
101 struct timespec end_time;
104 g_return_val_if_fail (cond != NULL, FALSE);
105 g_return_val_if_fail (entered_mutex != NULL, FALSE);
109 g_cond_wait (cond, entered_mutex);
113 end_time.tv_sec = abs_time->tv_sec;
114 end_time.tv_nsec = abs_time->tv_usec * (G_NANOSEC / G_MICROSEC);
115 g_assert (end_time.tv_nsec < G_NANOSEC);
116 result = pthread_cond_timedwait ((pthread_cond_t *) cond,
117 (pthread_mutex_t *) entered_mutex,
119 timed_out = (result == ETIMEDOUT);
121 posix_check_for_error (result);
126 g_cond_free_posix_impl (GCond * cond)
128 posix_check_for_error (pthread_cond_destroy ((pthread_cond_t *) cond));
133 g_private_new_posix_impl (GDestroyNotify destructor)
135 GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
136 posix_check_for_error (pthread_key_create ((pthread_key_t *) result,
141 /* NOTE: the functions g_private_get and g_private_set may not use
142 functions from gmem.c and gmessages.c */
145 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
150 pthread_setspecific (*(pthread_key_t *) private_key, value);
154 g_private_get_posix_impl (GPrivate * private_key)
159 return pthread_getspecific (*(pthread_key_t *) private_key);
162 static GThreadFunctions g_thread_functions_for_glib_use_default =
164 g_mutex_new_posix_impl,
165 (void (*)(GMutex *)) pthread_mutex_lock,
166 g_mutex_trylock_posix_impl,
167 (void (*)(GMutex *)) pthread_mutex_unlock,
168 g_mutex_free_posix_impl,
169 g_cond_new_posix_impl,
170 (void (*)(GCond *)) pthread_cond_signal,
171 (void (*)(GCond *)) pthread_cond_broadcast,
172 (void (*)(GCond *, GMutex *)) pthread_cond_wait,
173 g_cond_timed_wait_posix_impl,
174 g_cond_free_posix_impl,
175 g_private_new_posix_impl,
176 g_private_get_posix_impl,
177 g_private_set_posix_impl