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
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, 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>
34 #include <glib/gutils.h>
38 #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 } }
91 GDestroyNotify notify
;
97 G_ONCE_STATUS_NOTCALLED
,
98 G_ONCE_STATUS_PROGRESS
,
102 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
105 volatile GOnceStatus status
;
106 volatile gpointer retval
;
109 #define G_LOCK_NAME(name) g__ ## name ## _lock
110 #define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
111 #define G_LOCK_DEFINE(name) GMutex G_LOCK_NAME (name)
112 #define G_LOCK_EXTERN(name) extern GMutex G_LOCK_NAME (name)
115 # define G_LOCK(name) G_STMT_START{ \
116 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
117 "file %s: line %d (%s): locking: %s ", \
118 __FILE__, __LINE__, G_STRFUNC, \
120 g_mutex_lock (&G_LOCK_NAME (name)); \
122 # define G_UNLOCK(name) G_STMT_START{ \
123 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
124 "file %s: line %d (%s): unlocking: %s ", \
125 __FILE__, __LINE__, G_STRFUNC, \
127 g_mutex_unlock (&G_LOCK_NAME (name)); \
129 # define G_TRYLOCK(name) \
130 (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
131 "file %s: line %d (%s): try locking: %s ", \
132 __FILE__, __LINE__, G_STRFUNC, \
133 #name), g_mutex_trylock (&G_LOCK_NAME (name)))
134 #else /* !G_DEBUG_LOCKS */
135 # define G_LOCK(name) g_mutex_lock (&G_LOCK_NAME (name))
136 # define G_UNLOCK(name) g_mutex_unlock (&G_LOCK_NAME (name))
137 # define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
138 #endif /* !G_DEBUG_LOCKS */
140 GLIB_AVAILABLE_IN_2_32
141 GThread
* g_thread_ref (GThread
*thread
);
142 GLIB_AVAILABLE_IN_2_32
143 void g_thread_unref (GThread
*thread
);
144 GLIB_AVAILABLE_IN_2_32
145 GThread
* g_thread_new (const gchar
*name
,
148 GLIB_AVAILABLE_IN_2_32
149 GThread
* g_thread_try_new (const gchar
*name
,
153 GLIB_AVAILABLE_IN_ALL
154 GThread
* g_thread_self (void);
155 GLIB_AVAILABLE_IN_ALL
156 void g_thread_exit (gpointer retval
);
157 GLIB_AVAILABLE_IN_ALL
158 gpointer
g_thread_join (GThread
*thread
);
159 GLIB_AVAILABLE_IN_ALL
160 void g_thread_yield (void);
163 GLIB_AVAILABLE_IN_2_32
164 void g_mutex_init (GMutex
*mutex
);
165 GLIB_AVAILABLE_IN_2_32
166 void g_mutex_clear (GMutex
*mutex
);
167 GLIB_AVAILABLE_IN_ALL
168 void g_mutex_lock (GMutex
*mutex
);
169 GLIB_AVAILABLE_IN_ALL
170 gboolean
g_mutex_trylock (GMutex
*mutex
);
171 GLIB_AVAILABLE_IN_ALL
172 void g_mutex_unlock (GMutex
*mutex
);
174 GLIB_AVAILABLE_IN_2_32
175 void g_rw_lock_init (GRWLock
*rw_lock
);
176 GLIB_AVAILABLE_IN_2_32
177 void g_rw_lock_clear (GRWLock
*rw_lock
);
178 GLIB_AVAILABLE_IN_2_32
179 void g_rw_lock_writer_lock (GRWLock
*rw_lock
);
180 GLIB_AVAILABLE_IN_2_32
181 gboolean
g_rw_lock_writer_trylock (GRWLock
*rw_lock
);
182 GLIB_AVAILABLE_IN_2_32
183 void g_rw_lock_writer_unlock (GRWLock
*rw_lock
);
184 GLIB_AVAILABLE_IN_2_32
185 void g_rw_lock_reader_lock (GRWLock
*rw_lock
);
186 GLIB_AVAILABLE_IN_2_32
187 gboolean
g_rw_lock_reader_trylock (GRWLock
*rw_lock
);
188 GLIB_AVAILABLE_IN_2_32
189 void g_rw_lock_reader_unlock (GRWLock
*rw_lock
);
191 GLIB_AVAILABLE_IN_2_32
192 void g_rec_mutex_init (GRecMutex
*rec_mutex
);
193 GLIB_AVAILABLE_IN_2_32
194 void g_rec_mutex_clear (GRecMutex
*rec_mutex
);
195 GLIB_AVAILABLE_IN_2_32
196 void g_rec_mutex_lock (GRecMutex
*rec_mutex
);
197 GLIB_AVAILABLE_IN_2_32
198 gboolean
g_rec_mutex_trylock (GRecMutex
*rec_mutex
);
199 GLIB_AVAILABLE_IN_2_32
200 void g_rec_mutex_unlock (GRecMutex
*rec_mutex
);
202 GLIB_AVAILABLE_IN_2_32
203 void g_cond_init (GCond
*cond
);
204 GLIB_AVAILABLE_IN_2_32
205 void g_cond_clear (GCond
*cond
);
206 GLIB_AVAILABLE_IN_ALL
207 void g_cond_wait (GCond
*cond
,
209 GLIB_AVAILABLE_IN_ALL
210 void g_cond_signal (GCond
*cond
);
211 GLIB_AVAILABLE_IN_ALL
212 void g_cond_broadcast (GCond
*cond
);
213 GLIB_AVAILABLE_IN_2_32
214 gboolean
g_cond_wait_until (GCond
*cond
,
218 GLIB_AVAILABLE_IN_ALL
219 gpointer
g_private_get (GPrivate
*key
);
220 GLIB_AVAILABLE_IN_ALL
221 void g_private_set (GPrivate
*key
,
223 GLIB_AVAILABLE_IN_2_32
224 void g_private_replace (GPrivate
*key
,
227 GLIB_AVAILABLE_IN_ALL
228 gpointer
g_once_impl (GOnce
*once
,
231 GLIB_AVAILABLE_IN_ALL
232 gboolean
g_once_init_enter (volatile void *location
);
233 GLIB_AVAILABLE_IN_ALL
234 void g_once_init_leave (volatile void *location
,
237 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
238 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
239 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
240 # define g_once(once, func, arg) \
241 (((once)->status == G_ONCE_STATUS_READY) ? \
243 g_once_impl ((once), (func), (arg)))
244 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
247 # define g_once_init_enter(location) \
248 (G_GNUC_EXTENSION ({ \
249 G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
250 (void) (0 ? (gpointer) *(location) : 0); \
251 (!g_atomic_pointer_get (location) && \
252 g_once_init_enter (location)); \
254 # define g_once_init_leave(location, result) \
255 (G_GNUC_EXTENSION ({ \
256 G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer)); \
257 (void) (0 ? *(location) = (result) : 0); \
258 g_once_init_leave ((location), (gsize) (result)); \
261 # define g_once_init_enter(location) \
262 (g_once_init_enter((location)))
263 # define g_once_init_leave(location, result) \
264 (g_once_init_leave((location), (gsize) (result)))
267 GLIB_AVAILABLE_IN_2_36
268 guint
g_get_num_processors (void);
273 * Opaque type. See g_mutex_locker_new() for details.
276 typedef void GMutexLocker
;
279 * g_mutex_locker_new:
280 * @mutex: a mutex to lock
282 * Lock @mutex and return a new #GMutexLocker. Unlock with
283 * g_mutex_locker_free(). Using g_mutex_unlock() on @mutex
284 * while a #GMutexLocker exists can lead to undefined behaviour.
286 * This is intended to be used with g_autoptr(). Note that g_autoptr()
287 * is only available when using GCC or clang, so the following example
288 * will only work with those compilers:
298 * my_object_do_stuff (MyObject *self)
300 * g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&self->mutex);
302 * // Code with mutex locked here
305 * // No need to unlock
308 * // Optionally early unlock
309 * g_clear_pointer (&locker, g_mutex_locker_free);
311 * // Code with mutex unlocked here
315 * Returns: a #GMutexLocker
318 static inline GMutexLocker
*
319 g_mutex_locker_new (GMutex
*mutex
)
321 g_mutex_lock (mutex
);
322 return (GMutexLocker
*) mutex
;
326 * g_mutex_locker_free:
327 * @locker: a GMutexLocker
329 * Unlock @locker's mutex. See g_mutex_locker_new() for details.
334 g_mutex_locker_free (GMutexLocker
*locker
)
336 g_mutex_unlock ((GMutex
*) locker
);
341 #endif /* __G_THREAD_H__ */