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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 #ifndef __G_THREAD_H__
26 #define __G_THREAD_H__
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
32 #include <glib/gatomic.h>
33 #include <glib/gerror.h>
37 #define G_THREAD_ERROR g_thread_error_quark ()
39 GQuark
g_thread_error_quark (void);
43 G_THREAD_ERROR_AGAIN
/* Resource temporarily unavailable */
46 typedef gpointer (*GThreadFunc
) (gpointer data
);
48 typedef struct _GThread GThread
;
50 typedef union _GMutex GMutex
;
51 typedef struct _GRecMutex GRecMutex
;
52 typedef struct _GRWLock GRWLock
;
53 typedef struct _GCond GCond
;
54 typedef struct _GPrivate GPrivate
;
55 typedef struct _GOnce GOnce
;
85 #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 GLIB_AVAILABLE_IN_2_32
140 GThread
* g_thread_ref (GThread
*thread
);
141 GLIB_AVAILABLE_IN_2_32
142 void g_thread_unref (GThread
*thread
);
143 GLIB_AVAILABLE_IN_2_32
144 GThread
* g_thread_new (const gchar
*name
,
147 GLIB_AVAILABLE_IN_2_32
148 GThread
* g_thread_try_new (const gchar
*name
,
152 GLIB_AVAILABLE_IN_ALL
153 GThread
* g_thread_self (void);
154 GLIB_AVAILABLE_IN_ALL
155 void g_thread_exit (gpointer retval
);
156 GLIB_AVAILABLE_IN_ALL
157 gpointer
g_thread_join (GThread
*thread
);
158 GLIB_AVAILABLE_IN_ALL
159 void g_thread_yield (void);
162 GLIB_AVAILABLE_IN_2_32
163 void g_mutex_init (GMutex
*mutex
);
164 GLIB_AVAILABLE_IN_2_32
165 void g_mutex_clear (GMutex
*mutex
);
166 GLIB_AVAILABLE_IN_ALL
167 void g_mutex_lock (GMutex
*mutex
);
168 GLIB_AVAILABLE_IN_ALL
169 gboolean
g_mutex_trylock (GMutex
*mutex
);
170 GLIB_AVAILABLE_IN_ALL
171 void g_mutex_unlock (GMutex
*mutex
);
173 GLIB_AVAILABLE_IN_2_32
174 void g_rw_lock_init (GRWLock
*rw_lock
);
175 GLIB_AVAILABLE_IN_2_32
176 void g_rw_lock_clear (GRWLock
*rw_lock
);
177 GLIB_AVAILABLE_IN_2_32
178 void g_rw_lock_writer_lock (GRWLock
*rw_lock
);
179 GLIB_AVAILABLE_IN_2_32
180 gboolean
g_rw_lock_writer_trylock (GRWLock
*rw_lock
);
181 GLIB_AVAILABLE_IN_2_32
182 void g_rw_lock_writer_unlock (GRWLock
*rw_lock
);
183 GLIB_AVAILABLE_IN_2_32
184 void g_rw_lock_reader_lock (GRWLock
*rw_lock
);
185 GLIB_AVAILABLE_IN_2_32
186 gboolean
g_rw_lock_reader_trylock (GRWLock
*rw_lock
);
187 GLIB_AVAILABLE_IN_2_32
188 void g_rw_lock_reader_unlock (GRWLock
*rw_lock
);
190 GLIB_AVAILABLE_IN_2_32
191 void g_rec_mutex_init (GRecMutex
*rec_mutex
);
192 GLIB_AVAILABLE_IN_2_32
193 void g_rec_mutex_clear (GRecMutex
*rec_mutex
);
194 GLIB_AVAILABLE_IN_2_32
195 void g_rec_mutex_lock (GRecMutex
*rec_mutex
);
196 GLIB_AVAILABLE_IN_2_32
197 gboolean
g_rec_mutex_trylock (GRecMutex
*rec_mutex
);
198 GLIB_AVAILABLE_IN_2_32
199 void g_rec_mutex_unlock (GRecMutex
*rec_mutex
);
201 GLIB_AVAILABLE_IN_2_32
202 void g_cond_init (GCond
*cond
);
203 GLIB_AVAILABLE_IN_2_32
204 void g_cond_clear (GCond
*cond
);
205 GLIB_AVAILABLE_IN_ALL
206 void g_cond_wait (GCond
*cond
,
208 GLIB_AVAILABLE_IN_ALL
209 void g_cond_signal (GCond
*cond
);
210 GLIB_AVAILABLE_IN_ALL
211 void g_cond_broadcast (GCond
*cond
);
212 GLIB_AVAILABLE_IN_2_32
213 gboolean
g_cond_wait_until (GCond
*cond
,
217 GLIB_AVAILABLE_IN_ALL
218 gpointer
g_private_get (GPrivate
*key
);
219 GLIB_AVAILABLE_IN_ALL
220 void g_private_set (GPrivate
*key
,
222 GLIB_AVAILABLE_IN_2_32
223 void g_private_replace (GPrivate
*key
,
226 GLIB_AVAILABLE_IN_ALL
227 gpointer
g_once_impl (GOnce
*once
,
230 GLIB_AVAILABLE_IN_ALL
231 gboolean
g_once_init_enter (volatile void *location
);
232 GLIB_AVAILABLE_IN_ALL
233 void g_once_init_leave (volatile void *location
,
236 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
237 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
238 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
239 # define g_once(once, func, arg) \
240 (((once)->status == G_ONCE_STATUS_READY) ? \
242 g_once_impl ((once), (func), (arg)))
243 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
246 # define g_once_init_enter(location) \
247 (G_GNUC_EXTENSION ({ \
248 G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
249 (void) (0 ? (gpointer) *(location) : 0); \
250 (!g_atomic_pointer_get (location) && \
251 g_once_init_enter (location)); \
253 # define g_once_init_leave(location, result) \
254 (G_GNUC_EXTENSION ({ \
255 G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
256 (void) (0 ? *(location) = (result) : 0); \
257 g_once_init_leave ((location), (gsize) (result)); \
260 # define g_once_init_enter(location) \
261 (g_once_init_enter((location)))
262 # define g_once_init_leave(location, result) \
263 (g_once_init_leave((location), (gsize) (result)))
266 GLIB_AVAILABLE_IN_2_36
267 guint
g_get_num_processors (void);
271 #endif /* __G_THREAD_H__ */