99e5ee97ab16ae0f1e7ca972476ceef6dce16bad
[platform/upstream/glib.git] / gthread / gthread-solaris.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: solaris 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 <thread.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 #define solaris_print_error( name, num )                                \
43   g_error( "file %s: line %d (%s): error %s during %s",                 \
44            __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION,                  \
45            g_strerror((num)), #name )
46
47 #define solaris_check_for_error( what ) G_STMT_START{                   \
48   int error = (what);                                                   \
49   if( error ) { solaris_print_error( what, error ); }                   \
50   }G_STMT_END
51
52 gulong g_thread_min_stack_size = 0;
53
54 #define HAVE_G_THREAD_IMPL_INIT
55 static void 
56 g_thread_impl_init()
57 {
58   g_thread_min_priority = 0;
59   g_thread_max_priority = 127;
60 #ifdef _SC_THREAD_STACK_MIN
61   g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0);
62 #endif /* _SC_THREAD_STACK_MIN */
63 }
64
65 static GMutex *
66 g_mutex_new_solaris_impl (void)
67 {
68   GMutex *result = (GMutex *) g_new (mutex_t, 1);
69   solaris_check_for_error (mutex_init ((mutex_t *) result, USYNC_THREAD, 0));
70   return result;
71 }
72
73 static void
74 g_mutex_free_solaris_impl (GMutex * mutex)
75 {
76   solaris_check_for_error (mutex_destroy ((mutex_t *) mutex));
77   free (mutex);
78 }
79
80 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
81    functions from gmem.c and gmessages.c; */
82
83 /* mutex_lock, mutex_unlock can be taken directly, as
84    signature and semantic are right, but without error check then!!!!,
85    we might want to change this therefore. */
86
87 static gboolean
88 g_mutex_trylock_solaris_impl (GMutex * mutex)
89 {
90   int result;
91   result = mutex_trylock ((mutex_t *) mutex);
92   if (result == EBUSY)
93     return FALSE;
94   solaris_check_for_error (result);
95   return TRUE;
96 }
97
98 static GCond *
99 g_cond_new_solaris_impl ()
100 {
101   GCond *result = (GCond *) g_new (cond_t, 1);
102   solaris_check_for_error (cond_init ((cond_t *) result, USYNC_THREAD, 0));
103   return result;
104 }
105
106 /* cond_signal, cond_broadcast and cond_wait
107    can be taken directly, as signature and semantic are right, but
108    without error check then!!!!, we might want to change this
109    therfore. */
110
111 #define G_NSEC_PER_SEC 1000000000
112
113 static gboolean
114 g_cond_timed_wait_solaris_impl (GCond * cond, 
115                                 GMutex * entered_mutex,
116                                 GTimeVal * abs_time)
117 {
118   int result;
119   timestruc_t end_time;
120   gboolean timed_out;
121
122   g_return_val_if_fail (cond != NULL, FALSE);
123   g_return_val_if_fail (entered_mutex != NULL, FALSE);
124
125   if (!abs_time)
126     {
127       g_cond_wait (cond, entered_mutex);
128       return TRUE;
129     }
130
131   end_time.tv_sec = abs_time->tv_sec;
132   end_time.tv_nsec = abs_time->tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
133   g_assert (end_time.tv_nsec < G_NSEC_PER_SEC);
134   result = cond_timedwait ((cond_t *) cond, (mutex_t *) entered_mutex,
135                            &end_time);
136   timed_out = (result == ETIME);
137   if (!timed_out)
138     solaris_check_for_error (result);
139   return !timed_out;
140 }
141
142 static void
143 g_cond_free_solaris_impl (GCond * cond)
144 {
145   solaris_check_for_error (cond_destroy ((cond_t *) cond));
146   g_free (cond);
147 }
148
149 static GPrivate *
150 g_private_new_solaris_impl (GDestroyNotify destructor)
151 {
152   GPrivate *result = (GPrivate *) g_new (thread_key_t,1);
153   solaris_check_for_error (thr_keycreate ((thread_key_t *) result,
154                                           destructor));
155   return result;
156 }
157
158 /* NOTE: the functions g_private_get and g_private_set may not use
159    functions from gmem.c and gmessages.c */
160
161 static void
162 g_private_set_solaris_impl (GPrivate * private_key, gpointer value)
163 {
164   if (!private_key)
165     return;
166
167   thr_setspecific (*(thread_key_t *) private_key, value);
168 }
169
170 static gpointer
171 g_private_get_solaris_impl (GPrivate * private_key)
172 {
173   gpointer result;
174
175   if (!private_key)
176     return NULL;
177   
178   thr_getspecific (*(thread_key_t *) private_key, &result);
179
180   return result;
181 }
182
183 static void
184 g_thread_set_priority_solaris_impl (gpointer thread, GThreadPriority priority)
185 {
186   solaris_check_for_error (thr_setprio (*(thread_t*)thread,  
187                                         g_thread_map_priority (priority)));
188 }
189
190 static void
191 g_thread_create_solaris_impl (GThreadFunc thread_func, 
192                               gpointer arg, 
193                               gulong stack_size,
194                               gboolean joinable,
195                               gboolean bound,
196                               GThreadPriority priority,
197                               gpointer thread,
198                               GError **error)
199 {     
200   long flags = (bound ? THR_BOUND : 0) | (joinable ? 0: THR_DETACHED);
201   gint ret;
202   
203   g_return_if_fail (thread_func);
204   
205   stack_size = MAX (g_thread_min_stack_size, stack_size);
206   
207   ret = thr_create (NULL, stack_size, (void* (*)(void*))thread_func,
208                     arg, flags, thread);
209
210   if (ret == EAGAIN)
211     {
212       g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN, 
213                    "Error creating thread: %s", g_strerror (ret));
214       return;
215     }
216   
217   solaris_check_for_error (ret);
218
219   g_thread_set_priority_solaris_impl (thread, priority);
220 }
221
222 static void 
223 g_thread_yield_solaris_impl (void)
224 {
225   thr_yield ();
226 }
227
228 static void
229 g_thread_join_solaris_impl (gpointer thread)
230 {     
231   gpointer ignore;
232   solaris_check_for_error (thr_join (*(thread_t*)thread, NULL, &ignore));
233 }
234
235 static void 
236 g_thread_exit_solaris_impl (void) 
237 {
238   thr_exit (NULL);
239 }
240
241 static void
242 g_thread_self_solaris_impl (gpointer thread)
243 {
244   *(thread_t*)thread = thr_self();
245 }
246
247 static GThreadFunctions g_thread_functions_for_glib_use_default =
248 {
249   g_mutex_new_solaris_impl,
250   (void (*)(GMutex *)) mutex_lock,
251   g_mutex_trylock_solaris_impl,
252   (void (*)(GMutex *)) mutex_unlock,
253   g_mutex_free_solaris_impl,
254   g_cond_new_solaris_impl,
255   (void (*)(GCond *)) cond_signal,
256   (void (*)(GCond *)) cond_broadcast,
257   (void (*)(GCond *, GMutex *)) cond_wait,
258   g_cond_timed_wait_solaris_impl,
259   g_cond_free_solaris_impl,
260   g_private_new_solaris_impl,
261   g_private_get_solaris_impl,
262   g_private_set_solaris_impl,
263   g_thread_create_solaris_impl,
264   g_thread_yield_solaris_impl,
265   g_thread_join_solaris_impl,
266   g_thread_exit_solaris_impl,
267   g_thread_set_priority_solaris_impl,
268   g_thread_self_solaris_impl
269 };