This commit merges the glib-threads branch into the main
[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  * MT safe
25  */
26
27 #include <thread.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 #define solaris_print_error( name, num )                        \
33   g_error( "file %s: line %d (%s): error %s during %s",         \
34            __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION,          \
35            g_strerror((num)), #name )
36
37 #define solaris_check_for_error( what ) G_STMT_START{           \
38   int error = (what);                                           \
39   if( error ) { solaris_print_error( what, error ); }           \
40   }G_STMT_END
41
42 static GMutex *
43 g_mutex_new_solaris_impl (void)
44 {
45   GMutex *result = (GMutex *) g_new (mutex_t, 1);
46   solaris_check_for_error (mutex_init ((mutex_t *) result, USYNC_PROCESS, 0));
47   return result;
48 }
49
50 static void
51 g_mutex_free_solaris_impl (GMutex * mutex)
52 {
53   solaris_check_for_error (mutex_destroy ((mutex_t *) mutex));
54   free (mutex);
55 }
56
57 /* NOTE: the functions g_mutex_lock and g_mutex_unlock may not use
58    functions from gmem.c and gmessages.c; */
59
60 /* mutex_lock, mutex_unlock can be taken directly, as
61    signature and semantic are right, but without error check then!!!!,
62    we might want to change this therefore. */
63
64 static gboolean
65 g_mutex_trylock_solaris_impl (GMutex * mutex)
66 {
67   int result;
68   result = mutex_trylock ((mutex_t *) mutex);
69   if (result == EBUSY)
70     return FALSE;
71   solaris_check_for_error (result);
72   return TRUE;
73 }
74
75 static GCond *
76 g_cond_new_solaris_impl ()
77 {
78   GCond *result = (GCond *) g_new (cond_t, 1);
79   solaris_check_for_error (cond_init ((cond_t *) result, USYNC_THREAD, 0));
80   return result;
81 }
82
83 /* cond_signal, cond_broadcast and cond_wait
84    can be taken directly, as signature and semantic are right, but
85    without error check then!!!!, we might want to change this
86    therfore. */
87
88 #define G_MICROSEC 1000000
89 #define G_NANOSEC 1000000000
90
91 static gboolean
92 g_cond_timed_wait_solaris_impl (GCond * cond, 
93                                 GMutex * entered_mutex,
94                                 GTimeVal * abs_time)
95 {
96   int result;
97   timestruc_t end_time;
98   gboolean timed_out;
99
100   g_return_val_if_fail (cond != NULL, FALSE);
101   g_return_val_if_fail (entered_mutex != NULL, FALSE);
102
103   if (!abs_time)
104     {
105       g_cond_wait (cond, entered_mutex);
106       return TRUE;
107     }
108
109   end_time.tv_sec = abs_time->tv_sec;
110   end_time.tv_nsec = abs_time->tv_usec * (G_NANOSEC / G_MICROSEC);
111   g_assert (end_time.tv_nsec < G_NANOSEC);
112   result = cond_timedwait ((cond_t *) cond, (mutex_t *) entered_mutex,
113                            &end_time);
114   timed_out = (result == ETIME);
115   if (!timed_out)
116     solaris_check_for_error (result);
117   return !timed_out;
118 }
119
120 static void
121 g_cond_free_solaris_impl (GCond * cond)
122 {
123   solaris_check_for_error (cond_destroy ((cond_t *) cond));
124   g_free (cond);
125 }
126
127 static GPrivate *
128 g_private_new_solaris_impl (GDestroyNotify destructor)
129 {
130   GPrivate *result = (GPrivate *) g_new (thread_key_t,1);
131   solaris_check_for_error (thr_keycreate ((thread_key_t *) result,
132                                           destructor));
133   return result;
134 }
135
136 /* NOTE: the functions g_private_get and g_private_set may not use
137    functions from gmem.c and gmessages.c */
138
139 static void
140 g_private_set_solaris_impl (GPrivate * private, gpointer value)
141 {
142   if (!private)
143     return;
144
145   thr_setspecific (*(thread_key_t *) private, value);
146 }
147
148 static gpointer
149 g_private_get_solaris_impl (GPrivate * private)
150 {
151   gpointer result;
152
153   if (!private)
154     return NULL;
155   
156   thr_getspecific (*(thread_key_t *) private, &result);
157
158   return result;
159 }
160
161 static GThreadFunctions g_thread_functions_for_glib_use_default =
162 {
163   g_mutex_new_solaris_impl,
164   (void (*)(GMutex *)) mutex_lock,
165   g_mutex_trylock_solaris_impl,
166   (void (*)(GMutex *)) mutex_unlock,
167   g_mutex_free_solaris_impl,
168   g_cond_new_solaris_impl,
169   (void (*)(GCond *)) cond_signal,
170   (void (*)(GCond *)) cond_broadcast,
171   (void (*)(GCond *, GMutex *)) cond_wait,
172   g_cond_timed_wait_solaris_impl,
173   g_cond_free_solaris_impl,
174   g_private_new_solaris_impl,
175   g_private_get_solaris_impl,
176   g_private_set_solaris_impl
177 };