Revamped the thread configure stuff. Now dce threads (old posix draft) are
[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 #elif defined(G_THREADS_IMPL_DCE)
52 # define posix_check_for_error( what ) G_STMT_START{             \
53     if( (what) == -1 ) { posix_print_error( what, errno ); }       \
54     }G_STMT_END
55 #else /* neither G_THREADS_IMPL_POSIX nor G_THREADS_IMPL_DCE are defined */
56 # error This should not happen. Contact the GLb team.
57 #endif
58
59 static GMutex *
60 g_mutex_new_posix_impl (void)
61 {
62   GMutex *result = (GMutex *) g_new (pthread_mutex_t, 1);
63   posix_check_for_error (pthread_mutex_init ((pthread_mutex_t *) result, NULL));
64   return result;
65 }
66
67 static void
68 g_mutex_free_posix_impl (GMutex * mutex)
69 {
70   posix_check_for_error (pthread_mutex_destroy ((pthread_mutex_t *) mutex));
71   g_free (mutex);
72 }
73
74 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
75    functions from gmem.c and gmessages.c; */
76
77 /* pthread_mutex_lock, pthread_mutex_unlock can be taken directly, as
78    signature and semantic are right, but without error check then!!!!,
79    we might want to change this therefore. */
80
81 static gboolean
82 g_mutex_trylock_posix_impl (GMutex * mutex)
83 {
84   int result;
85
86   result = pthread_mutex_trylock ((pthread_mutex_t *) mutex);
87
88 #ifdef G_THREADS_IMPL_POSIX
89   if (result == EBUSY)
90     return FALSE;
91 #else /* G_THREADS_IMPL_DCE */
92   if (result == 0)
93     return FALSE;
94 #endif
95
96   posix_check_for_error (result);
97   return TRUE;
98 }
99
100 static GCond *
101 g_cond_new_posix_impl (void)
102 {
103   GCond *result = (GCond *) g_new (pthread_cond_t, 1);
104   posix_check_for_error (pthread_cond_init ((pthread_cond_t *) result, NULL));
105   return result;
106 }
107
108 /* pthread_cond_signal, pthread_cond_broadcast and pthread_cond_wait
109    can be taken directly, as signature and semantic are right, but
110    without error check then!!!!, we might want to change this
111    therfore. */
112
113 #define G_MICROSEC 1000000
114 #define G_NANOSEC 1000000000
115
116 static gboolean
117 g_cond_timed_wait_posix_impl (GCond * cond,
118                               GMutex * entered_mutex,
119                               GTimeVal * abs_time)
120 {
121   int result;
122   struct timespec end_time;
123   gboolean timed_out;
124
125   g_return_val_if_fail (cond != NULL, FALSE);
126   g_return_val_if_fail (entered_mutex != NULL, FALSE);
127
128   if (!abs_time)
129     {
130       g_cond_wait (cond, entered_mutex);
131       return TRUE;
132     }
133
134   end_time.tv_sec = abs_time->tv_sec;
135   end_time.tv_nsec = abs_time->tv_usec * (G_NANOSEC / G_MICROSEC);
136   g_assert (end_time.tv_nsec < G_NANOSEC);
137   result = pthread_cond_timedwait ((pthread_cond_t *) cond,
138                                    (pthread_mutex_t *) entered_mutex,
139                                    &end_time);
140
141 #ifdef G_THREADS_IMPL_POSIX
142   timed_out = (result == ETIMEDOUT);
143 #else /* G_THREADS_IMPL_DCE */
144   timed_out = (result == -1) && (errno = EAGAIN);
145 #endif
146
147   if (!timed_out)
148     posix_check_for_error (result);
149   return !timed_out;
150 }
151
152 static void
153 g_cond_free_posix_impl (GCond * cond)
154 {
155   posix_check_for_error (pthread_cond_destroy ((pthread_cond_t *) cond));
156   g_free (cond);
157 }
158
159 static GPrivate *
160 g_private_new_posix_impl (GDestroyNotify destructor)
161 {
162   GPrivate *result = (GPrivate *) g_new (pthread_key_t, 1);
163   posix_check_for_error (pthread_key_create ((pthread_key_t *) result,
164                                              destructor));
165   return result;
166 }
167
168 /* NOTE: the functions g_private_get and g_private_set may not use
169    functions from gmem.c and gmessages.c */
170
171 static void
172 g_private_set_posix_impl (GPrivate * private_key, gpointer value)
173 {
174   if (!private_key)
175     return;
176
177   pthread_setspecific (*(pthread_key_t *) private_key, value);
178 }
179
180 static gpointer
181 g_private_get_posix_impl (GPrivate * private_key)
182 {
183   if (!private_key)
184     return NULL;
185 #ifdef G_THREADS_IMPL_POSIX
186   return pthread_getspecific (*(pthread_key_t *) private_key);
187 #else /* G_THREADS_IMPL_DCE */
188   {
189     void* data;
190     posix_check_for_error (pthread_getspecific (*(pthread_key_t *) 
191                                                 private_key, &data);
192     return data;
193   }
194 #endif 
195 }
196
197 static GThreadFunctions g_thread_functions_for_glib_use_default =
198 {
199   g_mutex_new_posix_impl,
200   (void (*)(GMutex *)) pthread_mutex_lock,
201   g_mutex_trylock_posix_impl,
202   (void (*)(GMutex *)) pthread_mutex_unlock,
203   g_mutex_free_posix_impl,
204   g_cond_new_posix_impl,
205   (void (*)(GCond *)) pthread_cond_signal,
206   (void (*)(GCond *)) pthread_cond_broadcast,
207   (void (*)(GCond *, GMutex *)) pthread_cond_wait,
208   g_cond_timed_wait_posix_impl,
209   g_cond_free_posix_impl,
210   g_private_new_posix_impl,
211   g_private_get_posix_impl,
212   g_private_set_posix_impl
213 };