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