Added errorcheck mutexes. These are activated through the preprocessor
[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 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.
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  * Lesser General Public License for more details.
16  *
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.
21  */
22
23 /*
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/. 
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 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
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 )
48
49 #if defined(G_THREADS_IMPL_POSIX)
50 # define posix_check_for_error( what ) G_STMT_START{                    \
51     int error = (what);                                                 \
52     if( error ) { posix_print_error( what, error ); }                   \
53     }G_STMT_END
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 ); }            \
59     }G_STMT_END
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.
68 #endif
69
70 #if defined(POSIX_MIN_PRIORITY) && defined(POSIX_MAX_PRIORITY)
71 #  define HAVE_PRIORITIES 1
72 #endif
73
74 gulong g_thread_min_stack_size = 0;
75
76 #define G_MUTEX_SIZE (sizeof (pthread_mutex_t))
77
78 #define HAVE_G_THREAD_IMPL_INIT
79 static void 
80 g_thread_impl_init()
81 {
82 #ifdef HAVE_PRIORITIES
83   g_thread_min_priority = POSIX_MIN_PRIORITY;
84   g_thread_max_priority = POSIX_MAX_PRIORITY;
85 #endif /* HAVE_PRIORITIES */
86 #ifdef _SC_THREAD_STACK_MIN
87   g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0);
88 #endif /* _SC_THREAD_STACK_MIN */
89 }
90
91 static GMutex *
92 g_mutex_new_posix_impl (void)
93 {
94   GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
95   posix_check_for_error (pthread_mutex_init ((pthread_mutex_t *) result, 
96                                              mutexattr_default));
97   return result;
98 }
99
100 static void
101 g_mutex_free_posix_impl (GMutex * mutex)
102 {
103   posix_check_for_error (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
104   g_free (mutex);
105 }
106
107 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
108    functions from gmem.c and gmessages.c; */
109
110 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
111    signature and semantic are right, but without error check then!!!!,
112    we might want to change this therefore. */
113
114 static gboolean
115 g_mutex_trylock_posix_impl (GMutex * mutex)
116 {
117   int result;
118
119   result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
120
121 #ifdef G_THREADS_IMPL_POSIX
122   if (result == EBUSY)
123     return FALSE;
124 #else /* G_THREADS_IMPL_DCE */
125   if (result == 0)
126     return FALSE;
127 #endif
128
129   posix_check_for_error (result);
130   return TRUE;
131 }
132
133 static GCond *
134 g_cond_new_posix_impl (void)
135 {
136   GCond *result = (GCond *) g_new (pthread_cond_t, 1);
137   posix_check_for_error (pthread_cond_init ((pthread_cond_t *) result, 
138                                             condattr_default));
139   return result;
140 }
141
142 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
143    can be taken directly, as signature and semantic are right, but
144    without error check then!!!!, we might want to change this
145    therfore. */
146
147 #define G_NSEC_PER_SEC 1000000000
148
149 static gboolean
150 g_cond_timed_wait_posix_impl (GCond * cond,
151                               GMutex * entered_mutex,
152                               GTimeVal * abs_time)
153 {
154   int result;
155   struct timespec end_time;
156   gboolean timed_out;
157
158   g_return_val_if_fail (cond != NULL, FALSE);
159   g_return_val_if_fail (entered_mutex != NULL, FALSE);
160
161   if (!abs_time)
162     {
163       g_cond_wait (cond, entered_mutex);
164       return TRUE;
165     }
166
167   end_time.tv_sec = abs_time->tv_sec;
168   end_time.tv_nsec = abs_time->tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
169   g_assert (end_time.tv_nsec < G_NSEC_PER_SEC);
170   result = pthread_cond_timedwait ((pthread_cond_t *) cond,
171                                    (pthread_mutex_t *) entered_mutex,
172                                    &end_time);
173
174 #ifdef G_THREADS_IMPL_POSIX
175   timed_out = (result == ETIMEDOUT);
176 #else /* G_THREADS_IMPL_DCE */
177   timed_out = (result == -1) && (errno = EAGAIN);
178 #endif
179
180   if (!timed_out)
181     posix_check_for_error (result);
182   return !timed_out;
183 }
184
185 static void
186 g_cond_free_posix_impl (GCond * cond)
187 {
188   posix_check_for_error (pthread_cond_destroy ((pthread_cond_t *) cond));
189   g_free (cond);
190 }
191
192 static GPrivate *
193 g_private_new_posix_impl (GDestroyNotify destructor)
194 {
195   GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
196   posix_check_for_error (pthread_key_create ((pthread_key_t *) result,
197                                              destructor));
198   return result;
199 }
200
201 /* NOTE: the functions g_private_get and g_private_set may not use
202    functions from gmem.c and gmessages.c */
203
204 static void
205 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
206 {
207   if (!private_key)
208     return;
209   pthread_setspecific (*(pthread_key_t *) private_key, value);
210 }
211
212 static gpointer
213 g_private_get_posix_impl (GPrivate * private_key)
214 {
215   if (!private_key)
216     return NULL;
217 #ifdef G_THREADS_IMPL_POSIX
218   return pthread_getspecific (*(pthread_key_t *) private_key);
219 #else /* G_THREADS_IMPL_DCE */
220   {
221     void* data;
222     posix_check_for_error (pthread_getspecific (*(pthread_key_t *) 
223                                                 private_key, &data));
224     return data;
225   }
226 #endif
227 }
228
229 static void
230 g_thread_create_posix_impl (GThreadFunc thread_func, 
231                             gpointer arg, 
232                             gulong stack_size,
233                             gboolean joinable,
234                             gboolean bound,
235                             GThreadPriority priority,
236                             gpointer thread,
237                             GError **error)
238 {
239   pthread_attr_t attr;
240   gint ret;
241
242   g_return_if_fail (thread_func);
243
244   posix_check_for_error (pthread_attr_init (&attr));
245   
246 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
247   if (stack_size)
248     {
249       stack_size = MAX (g_thread_min_stack_size, stack_size);
250       posix_check_for_error (pthread_attr_setstacksize (&attr, stack_size));
251     }
252 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
253
254 #ifdef PTHREAD_SCOPE_SYSTEM
255   if (bound)
256     /* No error check here, because some systems can't do it and we
257      * simply don't want threads to fail because of that. */
258     pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
259 #endif /* PTHREAD_SCOPE_SYSTEM */
260
261 #ifdef G_THREADS_IMPL_POSIX
262   posix_check_for_error (pthread_attr_setdetachstate (&attr,
263           joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED));
264 #endif /* G_THREADS_IMPL_POSIX */
265   
266 #ifdef HAVE_PRIORITIES
267 # ifdef G_THREADS_IMPL_POSIX
268   {
269     struct sched_param sched;
270     posix_check_for_error (pthread_attr_getschedparam (&attr, &sched));
271     sched.sched_priority = g_thread_map_priority (priority);
272     posix_check_for_error (pthread_attr_setschedparam (&attr, &sched));
273   }
274 # else /* G_THREADS_IMPL_DCE */
275   posix_check_for_error 
276     (pthread_attr_setprio (&attr, g_thread_map_priority (priority)));
277 # endif /* G_THREADS_IMPL_DCE */
278 #endif /* HAVE_PRIORITIES */
279
280   ret = pthread_create (thread, &attr, (void* (*)(void*))thread_func, arg);
281   
282 #ifdef G_THREADS_IMPL_DCE
283   if (ret == -1) 
284     ret = errno;
285   else
286     ret = 0;
287 #endif /* G_THREADS_IMPL_DCE */
288
289   posix_check_for_error (pthread_attr_destroy (&attr));
290
291   if (ret == EAGAIN)
292     {
293       g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN, 
294                    "Error creating thread: %s", g_strerror (ret));
295       return;
296     }
297
298   posix_check_for_error (ret);
299
300 #ifdef G_THREADS_IMPL_DCE
301   if (!joinable)
302     posix_check_for_error (pthread_detach (thread));
303 #endif /* G_THREADS_IMPL_DCE */
304 }
305
306 static void 
307 g_thread_yield_posix_impl (void)
308 {
309   POSIX_YIELD_FUNC;
310 }
311
312 static void
313 g_thread_join_posix_impl (gpointer thread)
314 {     
315   gpointer ignore;
316   posix_check_for_error (pthread_join (*(pthread_t*)thread, 
317                                        &ignore));
318 }
319
320 static void 
321 g_thread_exit_posix_impl (void) 
322 {
323   pthread_exit (NULL);
324 }
325
326 static void
327 g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
328 {
329 #ifdef HAVE_PRIORITIES
330 # ifdef G_THREADS_IMPL_POSIX
331   struct sched_param sched;
332   int policy;
333   posix_check_for_error (pthread_getschedparam (*(pthread_t*)thread, 
334                                                 &policy, &sched));
335   sched.sched_priority = g_thread_map_priority (priority);
336   posix_check_for_error (pthread_setschedparam (*(pthread_t*)thread, 
337                                                 policy, &sched));
338 # else /* G_THREADS_IMPL_DCE */
339   posix_check_for_error (pthread_setprio (*(pthread_t*)thread, 
340                                           g_thread_map_priority (priority)));
341 # endif
342 #endif /* HAVE_PRIORITIES */
343 }
344
345 static void
346 g_thread_self_posix_impl (gpointer thread)
347 {
348   *(pthread_t*)thread = pthread_self();
349 }
350
351 static GThreadFunctions g_thread_functions_for_glib_use_default =
352 {
353   g_mutex_new_posix_impl,
354   (void (*)(GMutex *)) pthread_mutex_lock,
355   g_mutex_trylock_posix_impl,
356   (void (*)(GMutex *)) pthread_mutex_unlock,
357   g_mutex_free_posix_impl,
358   g_cond_new_posix_impl,
359   (void (*)(GCond *)) pthread_cond_signal,
360   (void (*)(GCond *)) pthread_cond_broadcast,
361   (void (*)(GCond *, GMutex *)) pthread_cond_wait,
362   g_cond_timed_wait_posix_impl,
363   g_cond_free_posix_impl,
364   g_private_new_posix_impl,
365   g_private_get_posix_impl,
366   g_private_set_posix_impl,
367   g_thread_create_posix_impl,
368   g_thread_yield_posix_impl,
369   g_thread_join_posix_impl,
370   g_thread_exit_posix_impl,
371   g_thread_set_priority_posix_impl,
372   g_thread_self_posix_impl
373 };