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