Remove "temporary until GLib is fixed" code
[platform/upstream/glib.git] / glib / 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 /* The GMutex and GCond implementations in this file are some of the
31  * lowest-level code in GLib.  All other parts of GLib (messages,
32  * memory, slices, etc) assume that they can freely use these facilities
33  * without risking recursion.
34  *
35  * As such, these functions are NOT permitted to call any other part of
36  * GLib.
37  *
38  * The thread manipulation functions (create, exit, join, etc.) have
39  * more freedom -- they can do as they please.
40  */
41
42 #include "config.h"
43
44 #include "gthread.h"
45
46 #include <pthread.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <stdio.h>
51
52 static void
53 g_thread_abort (gint         status,
54                 const gchar *function)
55 {
56   fprintf (stderr, "GLib (gthread-posix.c): Unexpected error from C library during '%s': %s.  Aborting.\n",
57            strerror (status), function);
58   abort ();
59 }
60
61 /* {{{1 GMutex */
62 void
63 g_mutex_init (GMutex *mutex)
64 {
65   gint status;
66
67   if G_UNLIKELY ((status = pthread_mutex_init (&mutex->impl, NULL)) != 0)
68     g_thread_abort (status, "pthread_mutex_init");
69 }
70
71 void
72 g_mutex_clear (GMutex *mutex)
73 {
74   gint status;
75
76   if G_UNLIKELY ((status = pthread_mutex_destroy (&mutex->impl)) != 0)
77     g_thread_abort (status, "pthread_mutex_destroy");
78 }
79
80 void
81 g_mutex_lock (GMutex *mutex)
82 {
83   gint status;
84
85   if G_UNLIKELY ((status = pthread_mutex_lock (&mutex->impl)) != 0)
86     g_thread_abort (status, "pthread_mutex_lock");
87 }
88
89 void
90 g_mutex_unlock (GMutex *mutex)
91 {
92   gint status;
93
94   if G_UNLIKELY ((status = pthread_mutex_unlock (&mutex->impl)) != 0)
95     g_thread_abort (status, "pthread_mutex_lock");
96 }
97
98 gboolean
99 g_mutex_trylock (GMutex *mutex)
100 {
101   gint status;
102
103   if G_LIKELY ((status = pthread_mutex_trylock (&mutex->impl)) == 0)
104     return TRUE;
105
106   if G_UNLIKELY (status != EBUSY)
107     g_thread_abort (status, "pthread_mutex_trylock");
108
109   return FALSE;
110 }
111
112 /* {{{1 GCond */
113
114 void
115 g_cond_init (GCond *cond)
116 {
117   gint status;
118
119   if G_UNLIKELY ((status = pthread_cond_init (&cond->impl, NULL)) != 0)
120     g_thread_abort (status, "pthread_cond_init");
121 }
122
123 void
124 g_cond_clear (GCond *cond)
125 {
126   gint status;
127
128   if G_UNLIKELY ((status = pthread_cond_destroy (&cond->impl)) != 0)
129     g_thread_abort (status, "pthread_cond_destroy");
130 }
131
132 void
133 g_cond_wait (GCond  *cond,
134              GMutex *mutex)
135 {
136   gint status;
137
138   if G_UNLIKELY ((status = pthread_cond_wait (&cond->impl, &mutex->impl)) != 0)
139     g_thread_abort (status, "pthread_cond_wait");
140 }
141
142 void
143 g_cond_signal (GCond *cond)
144 {
145   gint status;
146
147   if G_UNLIKELY ((status = pthread_cond_signal (&cond->impl)) != 0)
148     g_thread_abort (status, "pthread_cond_signal");
149 }
150
151 void
152 g_cond_broadcast (GCond *cond)
153 {
154   gint status;
155
156   if G_UNLIKELY ((status = pthread_cond_broadcast (&cond->impl)) != 0)
157     g_thread_abort (status, "pthread_cond_broadcast");
158 }
159
160 gboolean
161 g_cond_timed_wait (GCond    *cond,
162                    GMutex   *mutex,
163                    GTimeVal *abs_time)
164 {
165   struct timespec end_time;
166   gint status;
167
168   if (abs_time == NULL)
169     {
170       g_cond_wait (cond, mutex);
171       return TRUE;
172     }
173
174   end_time.tv_sec = abs_time->tv_sec;
175   end_time.tv_nsec = abs_time->tv_usec * 1000;
176
177   if ((status = pthread_cond_timedwait (&cond->impl, &mutex->impl, &end_time)) == 0)
178     return TRUE;
179
180   if G_UNLIKELY (status != ETIMEDOUT)
181     g_thread_abort (status, "pthread_cond_timedwait");
182
183   return FALSE;
184 }
185
186 gboolean
187 g_cond_timedwait (GCond  *cond,
188                   GMutex *mutex,
189                   gint64  abs_time)
190 {
191   struct timespec end_time;
192   gint status;
193
194   end_time.tv_sec = abs_time / 1000000;
195   end_time.tv_nsec = (abs_time % 1000000) * 1000;
196
197   if ((status = pthread_cond_timedwait (&cond->impl, &mutex->impl, &end_time)) == 0)
198     return TRUE;
199
200   if G_UNLIKELY (status != ETIMEDOUT)
201     g_thread_abort (status, "pthread_cond_timedwait");
202
203   return FALSE;
204 }
205
206 /* {{{1 GPrivate */
207
208 #include "glib.h"
209 #include "gthreadprivate.h"
210
211 #include <pthread.h>
212 #include <errno.h>
213 #include <stdlib.h>
214 #ifdef HAVE_SYS_TIME_H
215 # include <sys/time.h>
216 #endif
217 #ifdef HAVE_UNISTD_H
218 # include <unistd.h>
219 #endif
220
221 #ifdef HAVE_SCHED_H
222 #include <sched.h>
223 #endif
224
225 #define posix_check_err(err, name) G_STMT_START{                        \
226   int error = (err);                                                    \
227   if (error)                                                            \
228     g_error ("file %s: line %d (%s): error '%s' during '%s'",           \
229            __FILE__, __LINE__, G_STRFUNC,                               \
230            g_strerror (error), name);                                   \
231   }G_STMT_END
232
233 #define posix_check_cmd(cmd) posix_check_err (cmd, #cmd)
234
235 #ifdef G_ENABLE_DEBUG
236 static gboolean posix_check_cmd_prio_warned = FALSE;
237 # define posix_check_cmd_prio(cmd) G_STMT_START{                        \
238     int err = (cmd);                                                    \
239     if (err == EPERM)                                                   \
240       {                                                                 \
241         if (!posix_check_cmd_prio_warned)                               \
242           {                                                             \
243             posix_check_cmd_prio_warned = TRUE;                         \
244             g_warning ("Priorities can only be changed "                \
245                         "(resp. increased) by root.");                  \
246           }                                                             \
247       }                                                                 \
248     else                                                                \
249       posix_check_err (err, #cmd);                                      \
250      }G_STMT_END
251 #else /* G_ENABLE_DEBUG */
252 # define posix_check_cmd_prio(cmd) G_STMT_START{                        \
253     int err = (cmd);                                                    \
254     if (err != EPERM)                                                   \
255       posix_check_err (err, #cmd);                                      \
256      }G_STMT_END
257 #endif /* G_ENABLE_DEBUG */
258
259 #if defined (POSIX_MIN_PRIORITY) && defined (POSIX_MAX_PRIORITY)
260 # define HAVE_PRIORITIES 1
261 static gint priority_normal_value;
262 # ifdef __FreeBSD__
263    /* FreeBSD threads use different priority values from the POSIX_
264     * defines so we just set them here. The corresponding macros
265     * PTHREAD_MIN_PRIORITY and PTHREAD_MAX_PRIORITY are implied to be
266     * exported by the docs, but they aren't.
267     */
268 #  define PRIORITY_LOW_VALUE      0
269 #  define PRIORITY_URGENT_VALUE   31
270 # else /* !__FreeBSD__ */
271 #  define PRIORITY_LOW_VALUE      POSIX_MIN_PRIORITY
272 #  define PRIORITY_URGENT_VALUE   POSIX_MAX_PRIORITY
273 # endif /* !__FreeBSD__ */
274 # define PRIORITY_NORMAL_VALUE    priority_normal_value
275
276 # define PRIORITY_HIGH_VALUE \
277     ((PRIORITY_NORMAL_VALUE + PRIORITY_URGENT_VALUE * 2) / 3)
278
279 static gint
280 g_thread_priority_map (GThreadPriority priority)
281 {
282   switch (priority)
283     {
284     case G_THREAD_PRIORITY_LOW:
285       return PRIORITY_LOW_VALUE;
286
287     case G_THREAD_PRIORITY_NORMAL:
288       return PRIORITY_NORMAL_VALUE;
289
290     case G_THREAD_PRIORITY_HIGH:
291       return PRIORITY_HIGH_VALUE;
292
293     case G_THREAD_PRIORITY_URGENT:
294       return PRIORITY_URGENT_VALUE;
295
296     default:
297       g_assert_not_reached ();
298     }
299 }
300
301 #endif /* POSIX_MIN_PRIORITY && POSIX_MAX_PRIORITY */
302
303 static gulong g_thread_min_stack_size = 0;
304
305 #define G_MUTEX_SIZE (sizeof (pthread_mutex_t))
306
307 void
308 _g_thread_impl_init(void)
309 {
310 #ifdef _SC_THREAD_STACK_MIN
311   g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0);
312 #endif /* _SC_THREAD_STACK_MIN */
313 #ifdef HAVE_PRIORITIES
314   {
315     struct sched_param sched;
316     int policy;
317     posix_check_cmd (pthread_getschedparam (pthread_self(), &policy, &sched));
318     priority_normal_value = sched.sched_priority;
319   }
320 #endif /* HAVE_PRIORITIES */
321 }
322
323 static GPrivate *
324 g_private_new_posix_impl (GDestroyNotify destructor)
325 {
326   GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
327   posix_check_cmd (pthread_key_create ((pthread_key_t *) result, destructor));
328   return result;
329 }
330
331 /* NOTE: the functions g_private_get and g_private_set may not use
332    functions from gmem.c and gmessages.c */
333
334 static void
335 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
336 {
337   if (!private_key)
338     return;
339   pthread_setspecific (*(pthread_key_t *) private_key, value);
340 }
341
342 static gpointer
343 g_private_get_posix_impl (GPrivate * private_key)
344 {
345   if (!private_key)
346     return NULL;
347
348   return pthread_getspecific (*(pthread_key_t *) private_key);
349 }
350
351 /* {{{1 GThread */
352
353
354 static void
355 g_thread_create_posix_impl (GThreadFunc thread_func,
356                             gpointer arg,
357                             gulong stack_size,
358                             gboolean joinable,
359                             gboolean bound,
360                             GThreadPriority priority,
361                             gpointer thread,
362                             GError **error)
363 {
364   pthread_attr_t attr;
365   gint ret;
366
367   g_return_if_fail (thread_func);
368   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
369   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
370
371   posix_check_cmd (pthread_attr_init (&attr));
372
373 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
374   if (stack_size)
375     {
376       stack_size = MAX (g_thread_min_stack_size, stack_size);
377       /* No error check here, because some systems can't do it and
378        * we simply don't want threads to fail because of that. */
379       pthread_attr_setstacksize (&attr, stack_size);
380     }
381 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
382
383 #ifdef PTHREAD_SCOPE_SYSTEM
384   if (bound)
385     /* No error check here, because some systems can't do it and we
386      * simply don't want threads to fail because of that. */
387     pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);
388 #endif /* PTHREAD_SCOPE_SYSTEM */
389
390   posix_check_cmd (pthread_attr_setdetachstate (&attr,
391           joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED));
392
393 #ifdef HAVE_PRIORITIES
394   {
395     struct sched_param sched;
396     posix_check_cmd (pthread_attr_getschedparam (&attr, &sched));
397     sched.sched_priority = g_thread_priority_map (priority);
398     posix_check_cmd_prio (pthread_attr_setschedparam (&attr, &sched));
399   }
400 #endif /* HAVE_PRIORITIES */
401   ret = pthread_create (thread, &attr, (void* (*)(void*))thread_func, arg);
402
403   posix_check_cmd (pthread_attr_destroy (&attr));
404
405   if (ret == EAGAIN)
406     {
407       g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN, 
408                    "Error creating thread: %s", g_strerror (ret));
409       return;
410     }
411
412   posix_check_err (ret, "pthread_create");
413 }
414
415 static void
416 g_thread_yield_posix_impl (void)
417 {
418   POSIX_YIELD_FUNC;
419 }
420
421 static void
422 g_thread_join_posix_impl (gpointer thread)
423 {
424   gpointer ignore;
425   posix_check_cmd (pthread_join (*(pthread_t*)thread, &ignore));
426 }
427
428 static void
429 g_thread_exit_posix_impl (void)
430 {
431   pthread_exit (NULL);
432 }
433
434 static void
435 g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
436 {
437   g_return_if_fail (priority >= G_THREAD_PRIORITY_LOW);
438   g_return_if_fail (priority <= G_THREAD_PRIORITY_URGENT);
439 #ifdef HAVE_PRIORITIES
440   {
441     struct sched_param sched;
442     int policy;
443     posix_check_cmd (pthread_getschedparam (*(pthread_t*)thread, &policy,
444                                             &sched));
445     sched.sched_priority = g_thread_priority_map (priority);
446     posix_check_cmd_prio (pthread_setschedparam (*(pthread_t*)thread, policy,
447                                                  &sched));
448   }
449 #endif /* HAVE_PRIORITIES */
450 }
451
452 static void
453 g_thread_self_posix_impl (gpointer thread)
454 {
455   *(pthread_t*)thread = pthread_self();
456 }
457
458 static gboolean
459 g_thread_equal_posix_impl (gpointer thread1, gpointer thread2)
460 {
461   return (pthread_equal (*(pthread_t*)thread1, *(pthread_t*)thread2) != 0);
462 }
463
464 /* {{{1 Epilogue */
465 GThreadFunctions g_thread_functions_for_glib_use =
466 {
467   g_mutex_new,
468   g_mutex_lock,
469   g_mutex_trylock,
470   g_mutex_unlock,
471   g_mutex_free,
472   g_cond_new,
473   g_cond_signal,
474   g_cond_broadcast,
475   g_cond_wait,
476   g_cond_timed_wait,
477   g_cond_free,
478   g_private_new_posix_impl,
479   g_private_get_posix_impl,
480   g_private_set_posix_impl,
481   g_thread_create_posix_impl,
482   g_thread_yield_posix_impl,
483   g_thread_join_posix_impl,
484   g_thread_exit_posix_impl,
485   g_thread_set_priority_posix_impl,
486   g_thread_self_posix_impl,
487   g_thread_equal_posix_impl
488 };
489
490 /* vim:set foldmethod=marker: */