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