1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #ifndef __G_THREAD_H__
28 #define __G_THREAD_H__
30 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
34 #include <glib/gatomic.h>
35 #include <glib/gerror.h>
39 #define G_THREAD_ERROR g_thread_error_quark ()
41 GQuark g_thread_error_quark (void);
45 G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
48 typedef gpointer (*GThreadFunc) (gpointer data);
50 typedef struct _GThread GThread;
52 typedef union _GMutex GMutex;
53 typedef struct _GRecMutex GRecMutex;
54 typedef struct _GRWLock GRWLock;
55 typedef struct _GCond GCond;
56 typedef struct _GPrivate GPrivate;
57 typedef struct _GOnce GOnce;
87 #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
92 GDestroyNotify notify;
98 G_ONCE_STATUS_NOTCALLED,
99 G_ONCE_STATUS_PROGRESS,
103 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
106 volatile GOnceStatus status;
107 volatile gpointer retval;
110 #define G_LOCK_NAME(name) g__ ## name ## _lock
111 #define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
112 #define G_LOCK_DEFINE(name) GMutex G_LOCK_NAME (name)
113 #define G_LOCK_EXTERN(name) extern GMutex G_LOCK_NAME (name)
116 # define G_LOCK(name) G_STMT_START{ \
117 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
118 "file %s: line %d (%s): locking: %s ", \
119 __FILE__, __LINE__, G_STRFUNC, \
121 g_mutex_lock (&G_LOCK_NAME (name)); \
123 # define G_UNLOCK(name) G_STMT_START{ \
124 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
125 "file %s: line %d (%s): unlocking: %s ", \
126 __FILE__, __LINE__, G_STRFUNC, \
128 g_mutex_unlock (&G_LOCK_NAME (name)); \
130 # define G_TRYLOCK(name) \
131 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
132 "file %s: line %d (%s): try locking: %s ", \
133 __FILE__, __LINE__, G_STRFUNC, \
134 #name), g_mutex_trylock (&G_LOCK_NAME (name)))
135 #else /* !G_DEBUG_LOCKS */
136 # define G_LOCK(name) g_mutex_lock (&G_LOCK_NAME (name))
137 # define G_UNLOCK(name) g_mutex_unlock (&G_LOCK_NAME (name))
138 # define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
139 #endif /* !G_DEBUG_LOCKS */
141 GLIB_AVAILABLE_IN_2_32
142 GThread * g_thread_ref (GThread *thread);
143 GLIB_AVAILABLE_IN_2_32
144 void g_thread_unref (GThread *thread);
145 GLIB_AVAILABLE_IN_2_32
146 GThread * g_thread_new (const gchar *name,
149 GLIB_AVAILABLE_IN_2_32
150 GThread * g_thread_try_new (const gchar *name,
154 GLIB_AVAILABLE_IN_ALL
155 GThread * g_thread_self (void);
156 GLIB_AVAILABLE_IN_ALL
157 void g_thread_exit (gpointer retval);
158 GLIB_AVAILABLE_IN_ALL
159 gpointer g_thread_join (GThread *thread);
160 GLIB_AVAILABLE_IN_ALL
161 void g_thread_yield (void);
164 GLIB_AVAILABLE_IN_2_32
165 void g_mutex_init (GMutex *mutex);
166 GLIB_AVAILABLE_IN_2_32
167 void g_mutex_clear (GMutex *mutex);
168 GLIB_AVAILABLE_IN_ALL
169 void g_mutex_lock (GMutex *mutex);
170 GLIB_AVAILABLE_IN_ALL
171 gboolean g_mutex_trylock (GMutex *mutex);
172 GLIB_AVAILABLE_IN_ALL
173 void g_mutex_unlock (GMutex *mutex);
175 GLIB_AVAILABLE_IN_2_32
176 void g_rw_lock_init (GRWLock *rw_lock);
177 GLIB_AVAILABLE_IN_2_32
178 void g_rw_lock_clear (GRWLock *rw_lock);
179 GLIB_AVAILABLE_IN_2_32
180 void g_rw_lock_writer_lock (GRWLock *rw_lock);
181 GLIB_AVAILABLE_IN_2_32
182 gboolean g_rw_lock_writer_trylock (GRWLock *rw_lock);
183 GLIB_AVAILABLE_IN_2_32
184 void g_rw_lock_writer_unlock (GRWLock *rw_lock);
185 GLIB_AVAILABLE_IN_2_32
186 void g_rw_lock_reader_lock (GRWLock *rw_lock);
187 GLIB_AVAILABLE_IN_2_32
188 gboolean g_rw_lock_reader_trylock (GRWLock *rw_lock);
189 GLIB_AVAILABLE_IN_2_32
190 void g_rw_lock_reader_unlock (GRWLock *rw_lock);
192 GLIB_AVAILABLE_IN_2_32
193 void g_rec_mutex_init (GRecMutex *rec_mutex);
194 GLIB_AVAILABLE_IN_2_32
195 void g_rec_mutex_clear (GRecMutex *rec_mutex);
196 GLIB_AVAILABLE_IN_2_32
197 void g_rec_mutex_lock (GRecMutex *rec_mutex);
198 GLIB_AVAILABLE_IN_2_32
199 gboolean g_rec_mutex_trylock (GRecMutex *rec_mutex);
200 GLIB_AVAILABLE_IN_2_32
201 void g_rec_mutex_unlock (GRecMutex *rec_mutex);
203 GLIB_AVAILABLE_IN_2_32
204 void g_cond_init (GCond *cond);
205 GLIB_AVAILABLE_IN_2_32
206 void g_cond_clear (GCond *cond);
207 GLIB_AVAILABLE_IN_ALL
208 void g_cond_wait (GCond *cond,
210 GLIB_AVAILABLE_IN_ALL
211 void g_cond_signal (GCond *cond);
212 GLIB_AVAILABLE_IN_ALL
213 void g_cond_broadcast (GCond *cond);
214 GLIB_AVAILABLE_IN_2_32
215 gboolean g_cond_wait_until (GCond *cond,
219 GLIB_AVAILABLE_IN_ALL
220 gpointer g_private_get (GPrivate *key);
221 GLIB_AVAILABLE_IN_ALL
222 void g_private_set (GPrivate *key,
224 GLIB_AVAILABLE_IN_2_32
225 void g_private_replace (GPrivate *key,
228 GLIB_AVAILABLE_IN_ALL
229 gpointer g_once_impl (GOnce *once,
232 GLIB_AVAILABLE_IN_ALL
233 gboolean g_once_init_enter (volatile void *location);
234 GLIB_AVAILABLE_IN_ALL
235 void g_once_init_leave (volatile void *location,
238 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
239 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
240 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
241 # define g_once(once, func, arg) \
242 (((once)->status == G_ONCE_STATUS_READY) ? \
244 g_once_impl ((once), (func), (arg)))
245 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
248 # define g_once_init_enter(location) \
249 (G_GNUC_EXTENSION ({ \
250 G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
251 (void) (0 ? (gpointer) *(location) : 0); \
252 (!g_atomic_pointer_get (location) && \
253 g_once_init_enter (location)); \
255 # define g_once_init_leave(location, result) \
256 (G_GNUC_EXTENSION ({ \
257 G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
258 (void) (0 ? *(location) = (result) : 0); \
259 g_once_init_leave ((location), (gsize) (result)); \
262 # define g_once_init_enter(location) \
263 (g_once_init_enter((location)))
264 # define g_once_init_leave(location, result) \
265 (g_once_init_leave((location), (gsize) (result)))
268 GLIB_AVAILABLE_IN_2_36
269 guint g_get_num_processors (void);
273 #endif /* __G_THREAD_H__ */