Changed the prototype of thread_create and thread_self to return the
[platform/upstream/glib.git] / gthread / gthread-posix.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gthread.c: posix thread system implementation
5  * Copyright 1998 Sebastian Wilhelmi; University of Karlsruhe
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /*
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/. 
28  */
29
30 /* 
31  * MT safe
32  */
33
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
40
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 )
45
46 #if defined(G_THREADS_IMPL_POSIX)
47 # define posix_check_for_error( what ) G_STMT_START{             \
48     int error = (what);                                           \
49     if( error ) { posix_print_error( what, error ); }             \
50     }G_STMT_END
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 ); }       \
56     }G_STMT_END
57 # define pthread_key_create(a, b) pthread_keycreate (a, b)
58 # define pthread_attr_init(a) pthread_attr_create (a)
59 # define pthread_attr_destroy(a) pthread_attr_delete (a)
60 # define pthread_create(a, b, c, d) pthread_create(a, *b, c, d) 
61 # define mutexattr_default (pthread_mutexattr_default)
62 # define condattr_default (pthread_condattr_default)
63 #else /* neither G_THREADS_IMPL_POSIX nor G_THREADS_IMPL_DCE are defined */
64 # error This should not happen. Contact the GLib team.
65 #endif
66
67 #define HAVE_G_THREAD_IMPL_INIT
68 static void 
69 g_thread_impl_init()
70 {
71   g_thread_min_priority = POSIX_MIN_PRIORITY;
72   g_thread_max_priority = POSIX_MAX_PRIORITY;
73 }
74
75 static GMutex *
76 g_mutex_new_posix_impl (void)
77 {
78   GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
79   posix_check_for_error (pthread_mutex_init ((pthread_mutex_t *) result, 
80                                              mutexattr_default));
81   return result;
82 }
83
84 static void
85 g_mutex_free_posix_impl (GMutex * mutex)
86 {
87   posix_check_for_error (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
88   g_free (mutex);
89 }
90
91 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
92    functions from gmem.c and gmessages.c; */
93
94 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
95    signature and semantic are right, but without error check then!!!!,
96    we might want to change this therefore. */
97
98 static gboolean
99 g_mutex_trylock_posix_impl (GMutex * mutex)
100 {
101   int result;
102
103   result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
104
105 #ifdef G_THREADS_IMPL_POSIX
106   if (result == EBUSY)
107     return FALSE;
108 #else /* G_THREADS_IMPL_DCE */
109   if (result == 0)
110     return FALSE;
111 #endif
112
113   posix_check_for_error (result);
114   return TRUE;
115 }
116
117 static GCond *
118 g_cond_new_posix_impl (void)
119 {
120   GCond *result = (GCond *) g_new (pthread_cond_t, 1);
121   posix_check_for_error (pthread_cond_init ((pthread_cond_t *) result, 
122                                             condattr_default));
123   return result;
124 }
125
126 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
127    can be taken directly, as signature and semantic are right, but
128    without error check then!!!!, we might want to change this
129    therfore. */
130
131 #define G_MICROSEC 1000000
132 #define G_NANOSEC 1000000000
133
134 static gboolean
135 g_cond_timed_wait_posix_impl (GCond * cond,
136                               GMutex * entered_mutex,
137                               GTimeVal * abs_time)
138 {
139   int result;
140   struct timespec end_time;
141   gboolean timed_out;
142
143   g_return_val_if_fail (cond != NULL, FALSE);
144   g_return_val_if_fail (entered_mutex != NULL, FALSE);
145
146   if (!abs_time)
147     {
148       g_cond_wait (cond, entered_mutex);
149       return TRUE;
150     }
151
152   end_time.tv_sec = abs_time->tv_sec;
153   end_time.tv_nsec = abs_time->tv_usec * (G_NANOSEC / G_MICROSEC);
154   g_assert (end_time.tv_nsec < G_NANOSEC);
155   result = pthread_cond_timedwait ((pthread_cond_t *) cond,
156                                    (pthread_mutex_t *) entered_mutex,
157                                    &end_time);
158
159 #ifdef G_THREADS_IMPL_POSIX
160   timed_out = (result == ETIMEDOUT);
161 #else /* G_THREADS_IMPL_DCE */
162   timed_out = (result == -1) && (errno = EAGAIN);
163 #endif
164
165   if (!timed_out)
166     posix_check_for_error (result);
167   return !timed_out;
168 }
169
170 static void
171 g_cond_free_posix_impl (GCond * cond)
172 {
173   posix_check_for_error (pthread_cond_destroy ((pthread_cond_t *) cond));
174   g_free (cond);
175 }
176
177 static GPrivate *
178 g_private_new_posix_impl (GDestroyNotify destructor)
179 {
180   GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
181   posix_check_for_error (pthread_key_create ((pthread_key_t *) result,
182                                              destructor));
183   return result;
184 }
185
186 /* NOTE: the functions g_private_get and g_private_set may not use
187    functions from gmem.c and gmessages.c */
188
189 static void
190 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
191 {
192   if (!private_key)
193     return;
194   pthread_setspecific (*(pthread_key_t *) private_key, value);
195 }
196
197 static gpointer
198 g_private_get_posix_impl (GPrivate * private_key)
199 {
200   if (!private_key)
201     return NULL;
202 #ifdef G_THREADS_IMPL_POSIX
203   return pthread_getspecific (*(pthread_key_t *) private_key);
204 #else /* G_THREADS_IMPL_DCE */
205   {
206     void* data;
207     posix_check_for_error (pthread_getspecific (*(pthread_key_t *) 
208                                                 private_key, &data));
209     return data;
210   }
211 #endif
212 }
213
214 static void
215 g_thread_create_posix_impl (GThreadFunc thread_func, 
216                             gpointer arg, 
217                             gulong stack_size,
218                             gboolean joinable,
219                             gboolean bound,
220                             GThreadPriority priority,
221                             gpointer thread)
222 {
223   pthread_attr_t attr;
224
225   g_return_if_fail (thread_func);
226
227   posix_check_for_error (pthread_attr_init (&attr));
228   
229 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
230   if (stack_size)
231       posix_check_for_error (pthread_attr_setstacksize (&attr, stack_size));
232 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
233
234 #ifdef PTHREAD_SCOPE_SYSTEM
235   if (bound)
236      posix_check_for_error (pthread_attr_setscope (&attr, 
237                                                    PTHREAD_SCOPE_SYSTEM));
238 #endif /* PTHREAD_SCOPE_SYSTEM */
239
240 #ifdef G_THREADS_IMPL_POSIX
241   posix_check_for_error (pthread_attr_setdetachstate (&attr,
242           joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED));
243 #endif /* G_THREADS_IMPL_POSIX */
244   
245 #ifdef G_THREADS_IMPL_POSIX
246   {
247     struct sched_param sched;
248     posix_check_for_error (pthread_attr_getschedparam (&attr, &sched));
249     sched.sched_priority = g_thread_map_priority (priority);
250     posix_check_for_error (pthread_attr_setschedparam (&attr, &sched));
251   }
252 #else /* G_THREADS_IMPL_DCE */
253   posix_check_for_error 
254     (pthread_attr_setprio (&attr, g_thread_map_priority (priority)));
255 #endif /* G_THREADS_IMPL_DCE */
256
257   posix_check_for_error (pthread_create (thread, &attr, 
258                                          (void* (*)(void*))thread_func,
259                                          arg));
260   
261   posix_check_for_error (pthread_attr_destroy (&attr));
262
263 #ifdef G_THREADS_IMPL_DCE
264   if (!joinable)
265     posix_check_for_error (pthread_detach (thread));
266 #endif /* G_THREADS_IMPL_DCE */
267 }
268
269 static void 
270 g_thread_yield_posix_impl (void)
271 {
272   POSIX_YIELD_FUNC;
273 }
274
275 static void
276 g_thread_join_posix_impl (gpointer thread)
277 {     
278   gpointer ignore;
279   posix_check_for_error (pthread_join (*(pthread_t*)thread, 
280                                        &ignore));
281 }
282
283 static void 
284 g_thread_exit_posix_impl (void) 
285 {
286   pthread_exit (NULL);
287 }
288
289 static void
290 g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
291 {
292 #ifdef G_THREADS_IMPL_POSIX
293   struct sched_param sched;
294   int policy;
295   posix_check_for_error (pthread_getschedparam (*(pthread_t*)thread, 
296                                                 &policy, &sched));
297   sched.sched_priority = g_thread_map_priority (priority);
298   posix_check_for_error (pthread_setschedparam (*(pthread_t*)thread, 
299                                                 policy, &sched));
300 #else /* G_THREADS_IMPL_DCE */
301   posix_check_for_error (pthread_setprio (*(pthread_t*)thread, 
302                                           g_thread_map_priority (priority)));
303 #endif
304 }
305
306 static void
307 g_thread_self_posix_impl (gpointer thread)
308 {
309   *(pthread_t*)thread = pthread_self();
310 }
311
312 static GThreadFunctions g_thread_functions_for_glib_use_default =
313 {
314   g_mutex_new_posix_impl,
315   (void (*)(GMutex *)) pthread_mutex_lock,
316   g_mutex_trylock_posix_impl,
317   (void (*)(GMutex *)) pthread_mutex_unlock,
318   g_mutex_free_posix_impl,
319   g_cond_new_posix_impl,
320   (void (*)(GCond *)) pthread_cond_signal,
321   (void (*)(GCond *)) pthread_cond_broadcast,
322   (void (*)(GCond *, GMutex *)) pthread_cond_wait,
323   g_cond_timed_wait_posix_impl,
324   g_cond_free_posix_impl,
325   g_private_new_posix_impl,
326   g_private_get_posix_impl,
327   g_private_set_posix_impl,
328   g_thread_create_posix_impl,
329   g_thread_yield_posix_impl,
330   g_thread_join_posix_impl,
331   g_thread_exit_posix_impl,
332   g_thread_set_priority_posix_impl,
333   g_thread_self_posix_impl
334 };