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 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
31 #ifndef __G_THREAD_H__
32 #define __G_THREAD_H__
34 #include <glib/gatomic.h>
35 #include <glib/gerror.h>
39 #define G_THREAD_ERROR g_thread_error_quark ()
40 GQuark g_thread_error_quark (void);
44 G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
47 typedef gpointer (*GThreadFunc) (gpointer data);
49 typedef struct _GThread GThread;
51 typedef union _GMutex GMutex;
52 typedef struct _GRecMutex GRecMutex;
53 typedef struct _GRWLock GRWLock;
54 typedef struct _GCond GCond;
55 typedef struct _GPrivate GPrivate;
56 typedef struct _GOnce GOnce;
86 #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
90 GDestroyNotify notify;
96 G_ONCE_STATUS_NOTCALLED,
97 G_ONCE_STATUS_PROGRESS,
101 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
104 volatile GOnceStatus status;
105 volatile gpointer retval;
108 #define G_LOCK_NAME(name) g__ ## name ## _lock
109 #define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
110 #define G_LOCK_DEFINE(name) GMutex G_LOCK_NAME (name)
111 #define G_LOCK_EXTERN(name) extern GMutex G_LOCK_NAME (name)
114 # define G_LOCK(name) G_STMT_START{ \
115 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
116 "file %s: line %d (%s): locking: %s ", \
117 __FILE__, __LINE__, G_STRFUNC, \
119 g_mutex_lock (&G_LOCK_NAME (name)); \
121 # define G_UNLOCK(name) G_STMT_START{ \
122 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
123 "file %s: line %d (%s): unlocking: %s ", \
124 __FILE__, __LINE__, G_STRFUNC, \
126 g_mutex_unlock (&G_LOCK_NAME (name)); \
128 # define G_TRYLOCK(name) \
129 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
130 "file %s: line %d (%s): try locking: %s ", \
131 __FILE__, __LINE__, G_STRFUNC, \
132 #name), g_mutex_trylock (&G_LOCK_NAME (name)))
133 #else /* !G_DEBUG_LOCKS */
134 # define G_LOCK(name) g_mutex_lock (&G_LOCK_NAME (name))
135 # define G_UNLOCK(name) g_mutex_unlock (&G_LOCK_NAME (name))
136 # define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
137 #endif /* !G_DEBUG_LOCKS */
139 GThread * g_thread_new (const gchar *name,
144 GThread * g_thread_new_full (const gchar *name,
150 GThread * g_thread_self (void);
151 void g_thread_exit (gpointer retval);
152 gpointer g_thread_join (GThread *thread);
153 void g_thread_yield (void);
156 void g_mutex_init (GMutex *mutex);
157 void g_mutex_clear (GMutex *mutex);
158 void g_mutex_lock (GMutex *mutex);
159 gboolean g_mutex_trylock (GMutex *mutex);
160 void g_mutex_unlock (GMutex *mutex);
162 void g_rw_lock_init (GRWLock *rw_lock);
163 void g_rw_lock_clear (GRWLock *rw_lock);
164 void g_rw_lock_writer_lock (GRWLock *rw_lock);
165 gboolean g_rw_lock_writer_trylock (GRWLock *rw_lock);
166 void g_rw_lock_writer_unlock (GRWLock *rw_lock);
167 void g_rw_lock_reader_lock (GRWLock *rw_lock);
168 gboolean g_rw_lock_reader_trylock (GRWLock *rw_lock);
169 void g_rw_lock_reader_unlock (GRWLock *rw_lock);
171 void g_rec_mutex_init (GRecMutex *rec_mutex);
172 void g_rec_mutex_clear (GRecMutex *rec_mutex);
173 void g_rec_mutex_lock (GRecMutex *rec_mutex);
174 gboolean g_rec_mutex_trylock (GRecMutex *rec_mutex);
175 void g_rec_mutex_unlock (GRecMutex *rec_mutex);
177 void g_cond_init (GCond *cond);
178 void g_cond_clear (GCond *cond);
179 void g_cond_wait (GCond *cond,
181 void g_cond_signal (GCond *cond);
182 void g_cond_broadcast (GCond *cond);
183 gboolean g_cond_timed_wait (GCond *cond,
186 gboolean g_cond_timedwait (GCond *cond,
190 gpointer g_private_get (GPrivate *key);
191 void g_private_set (GPrivate *key,
193 void g_private_replace (GPrivate *key,
196 gpointer g_once_impl (GOnce *once,
199 gboolean g_once_init_enter (volatile void *location);
200 void g_once_init_leave (volatile void *location,
203 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
204 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
205 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
206 # define g_once(once, func, arg) \
207 (((once)->status == G_ONCE_STATUS_READY) ? \
209 g_once_impl ((once), (func), (arg)))
210 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
213 # define g_once_init_enter(location) \
214 (G_GNUC_EXTENSION ({ \
215 G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
216 (void) (0 ? (gpointer) *(location) : 0); \
217 (!g_atomic_pointer_get (location) && \
218 g_once_init_enter (location)); \
220 # define g_once_init_leave(location, result) \
221 (G_GNUC_EXTENSION ({ \
222 G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
223 (void) (0 ? *(location) = (result) : 0); \
224 g_once_init_leave ((location), (gsize) (result)); \
227 # define g_once_init_enter(location) \
228 (g_once_init_enter((location)))
229 # define g_once_init_leave(location, result) \
230 (g_once_init_leave((location), (gsize) (result)))
235 #endif /* __G_THREAD_H__ */