Merge in current Win32 version. Almost no Unix code touched.
[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  * MT safe
25  */
26
27 #include <pthread.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #ifdef HAVE_SYS_TIME_H
31 #include <sys/time.h>
32 #endif
33
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 )
38
39 #define posix_check_for_error( what ) G_STMT_START{             \
40   int error = (what);                                           \
41   if( error ) { posix_print_error( what, error ); }             \
42   }G_STMT_END
43
44 static GMutex *
45 g_mutex_new_posix_impl (void)
46 {
47   GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
48   posix_check_for_error (pthread_mutex_init ((pthread_mutex_t *) result, NULL));
49   return result;
50 }
51
52 static void
53 g_mutex_free_posix_impl (GMutex * mutex)
54 {
55   posix_check_for_error (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
56   g_free (mutex);
57 }
58
59 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
60    functions from gmem.c and gmessages.c; */
61
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. */
65
66 static gboolean
67 g_mutex_trylock_posix_impl (GMutex * mutex)
68 {
69   int result;
70
71   result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
72   if (result != EBUSY)
73     return FALSE;
74
75   posix_check_for_error (result);
76   return TRUE;
77 }
78
79 static GCond *
80 g_cond_new_posix_impl (void)
81 {
82   GCond *result = (GCond *) g_new (pthread_cond_t, 1);
83   posix_check_for_error (pthread_cond_init ((pthread_cond_t *) result, NULL));
84   return result;
85 }
86
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
90    therfore. */
91
92 #define G_MICROSEC 1000000
93 #define G_NANOSEC 1000000000
94
95 static gboolean
96 g_cond_timed_wait_posix_impl (GCond * cond,
97                               GMutex * entered_mutex,
98                               GTimeVal * abs_time)
99 {
100   int result;
101   struct timespec end_time;
102   gboolean timed_out;
103
104   g_return_val_if_fail (cond != NULL, FALSE);
105   g_return_val_if_fail (entered_mutex != NULL, FALSE);
106
107   if (!abs_time)
108     {
109       g_cond_wait (cond, entered_mutex);
110       return TRUE;
111     }
112
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,
118                                    &end_time);
119   timed_out = (result == ETIMEDOUT);
120   if (!timed_out)
121     posix_check_for_error (result);
122   return !timed_out;
123 }
124
125 static void
126 g_cond_free_posix_impl (GCond * cond)
127 {
128   posix_check_for_error (pthread_cond_destroy ((pthread_cond_t *) cond));
129   g_free (cond);
130 }
131
132 static GPrivate *
133 g_private_new_posix_impl (GDestroyNotify destructor)
134 {
135   GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
136   posix_check_for_error (pthread_key_create ((pthread_key_t *) result,
137                                              destructor));
138   return result;
139 }
140
141 /* NOTE: the functions g_private_get and g_private_set may not use
142    functions from gmem.c and gmessages.c */
143
144 static void
145 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
146 {
147   if (!private_key)
148     return;
149
150   pthread_setspecific (*(pthread_key_t *) private_key, value);
151 }
152
153 static gpointer
154 g_private_get_posix_impl (GPrivate * private_key)
155 {
156   if (!private_key)
157     return NULL;
158
159   return pthread_getspecific (*(pthread_key_t *) private_key);
160 }
161
162 static GThreadFunctions g_thread_functions_for_glib_use_default =
163 {
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
178 };