Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / glib / gthread / gthread-posix.c
bloba62ab91dc3dda74dbaa9a78b860ec65c7f6d3766
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/.
30 /*
31 * MT safe
34 #include <pthread.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #ifdef HAVE_SYS_TIME_H
38 #include <sys/time.h>
39 #endif
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 #define posix_check_for_error( what ) G_STMT_START{ \
47 int error = (what); \
48 if( error ) { posix_print_error( what, error ); } \
49 }G_STMT_END
51 static GMutex *
52 g_mutex_new_posix_impl (void)
54 GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
55 posix_check_for_error (pthread_mutex_init ((pthread_mutex_t *) result, NULL));
56 return result;
59 static void
60 g_mutex_free_posix_impl (GMutex * mutex)
62 posix_check_for_error (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
63 g_free (mutex);
66 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
67 functions from gmem.c and gmessages.c; */
69 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
70 signature and semantic are right, but without error check then!!!!,
71 we might want to change this therefore. */
73 static gboolean
74 g_mutex_trylock_posix_impl (GMutex * mutex)
76 int result;
78 result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
79 #ifdef HAVE_PTHREAD_MUTEX_TRYLOCK_POSIX
80 if (result == EBUSY)
81 return FALSE;
82 posix_check_for_error (result);
83 #else
84 if (result == 0)
85 return FALSE;
86 #endif
87 return TRUE;
90 static GCond *
91 g_cond_new_posix_impl (void)
93 GCond *result = (GCond *) g_new (pthread_cond_t, 1);
94 posix_check_for_error (pthread_cond_init ((pthread_cond_t *) result, NULL));
95 return result;
98 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
99 can be taken directly, as signature and semantic are right, but
100 without error check then!!!!, we might want to change this
101 therfore. */
103 #define G_MICROSEC 1000000
104 #define G_NANOSEC 1000000000
106 static gboolean
107 g_cond_timed_wait_posix_impl (GCond * cond,
108 GMutex * entered_mutex,
109 GTimeVal * abs_time)
111 int result;
112 struct timespec end_time;
113 gboolean timed_out;
115 g_return_val_if_fail (cond != NULL, FALSE);
116 g_return_val_if_fail (entered_mutex != NULL, FALSE);
118 if (!abs_time)
120 g_cond_wait (cond, entered_mutex);
121 return TRUE;
124 end_time.tv_sec = abs_time->tv_sec;
125 end_time.tv_nsec = abs_time->tv_usec * (G_NANOSEC / G_MICROSEC);
126 g_assert (end_time.tv_nsec < G_NANOSEC);
127 result = pthread_cond_timedwait ((pthread_cond_t *) cond,
128 (pthread_mutex_t *) entered_mutex,
129 &end_time);
130 #ifdef HAVE_PTHREAD_COND_TIMEDWAIT_POSIX
131 timed_out = (result == ETIMEDOUT);
132 #else
133 timed_out = (result == -1 && errno == EAGAIN);
134 #endif
135 if (!timed_out)
136 posix_check_for_error (result);
137 return !timed_out;
140 static void
141 g_cond_free_posix_impl (GCond * cond)
143 posix_check_for_error (pthread_cond_destroy ((pthread_cond_t *) cond));
144 g_free (cond);
147 static GPrivate *
148 g_private_new_posix_impl (GDestroyNotify destructor)
150 GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
151 posix_check_for_error (pthread_key_create ((pthread_key_t *) result,
152 destructor));
153 return result;
156 /* NOTE: the functions g_private_get and g_private_set may not use
157 functions from gmem.c and gmessages.c */
159 static void
160 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
162 if (!private_key)
163 return;
165 pthread_setspecific (*(pthread_key_t *) private_key, value);
168 static gpointer
169 g_private_get_posix_impl (GPrivate * private_key)
171 if (!private_key)
172 return NULL;
173 #ifdef HAVE_PTHREAD_GETSPECIFIC_POSIX
174 return pthread_getspecific (*(pthread_key_t *) private_key);
175 #else /* HAVE_PTHREAD_GETSPECIFIC_POSIX */
177 void* data;
178 pthread_getspecific (*(pthread_key_t *) private_key, &data);
179 return data;
181 #endif /* HAVE_PTHREAD_GETSPECIFIC_POSIX */
184 static GThreadFunctions g_thread_functions_for_glib_use_default =
186 g_mutex_new_posix_impl,
187 (void (*)(GMutex *)) pthread_mutex_lock,
188 g_mutex_trylock_posix_impl,
189 (void (*)(GMutex *)) pthread_mutex_unlock,
190 g_mutex_free_posix_impl,
191 g_cond_new_posix_impl,
192 (void (*)(GCond *)) pthread_cond_signal,
193 (void (*)(GCond *)) pthread_cond_broadcast,
194 (void (*)(GCond *, GMutex *)) pthread_cond_wait,
195 g_cond_timed_wait_posix_impl,
196 g_cond_free_posix_impl,
197 g_private_new_posix_impl,
198 g_private_get_posix_impl,
199 g_private_set_posix_impl