[kdbus] sync with kdbus (kdbus.h - commit: 5ae1ecac44cb)
[platform/upstream/glib.git] / glib / gthread.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 /*
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/.
23  */
24
25 #ifndef __G_THREAD_H__
26 #define __G_THREAD_H__
27
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
30 #endif
31
32 #include <glib/gatomic.h>
33 #include <glib/gerror.h>
34
35 G_BEGIN_DECLS
36
37 #define G_THREAD_ERROR g_thread_error_quark ()
38 GLIB_AVAILABLE_IN_ALL
39 GQuark g_thread_error_quark (void);
40
41 typedef enum
42 {
43   G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
44 } GThreadError;
45
46 typedef gpointer (*GThreadFunc) (gpointer data);
47
48 typedef struct _GThread         GThread;
49
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;
56
57 union _GMutex
58 {
59   /*< private >*/
60   gpointer p;
61   guint i[2];
62 };
63
64 struct _GRWLock
65 {
66   /*< private >*/
67   gpointer p;
68   guint i[2];
69 };
70
71 struct _GCond
72 {
73   /*< private >*/
74   gpointer p;
75   guint i[2];
76 };
77
78 struct _GRecMutex
79 {
80   /*< private >*/
81   gpointer p;
82   guint i[2];
83 };
84
85 #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
86 struct _GPrivate
87 {
88   /*< private >*/
89   gpointer       p;
90   GDestroyNotify notify;
91   gpointer future[2];
92 };
93
94 typedef enum
95 {
96   G_ONCE_STATUS_NOTCALLED,
97   G_ONCE_STATUS_PROGRESS,
98   G_ONCE_STATUS_READY
99 } GOnceStatus;
100
101 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
102 struct _GOnce
103 {
104   volatile GOnceStatus status;
105   volatile gpointer retval;
106 };
107
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)
112
113 #ifdef G_DEBUG_LOCKS
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,              \
118              #name);                                            \
119       g_mutex_lock (&G_LOCK_NAME (name));                       \
120    }G_STMT_END
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,              \
125              #name);                                            \
126      g_mutex_unlock (&G_LOCK_NAME (name));                      \
127    }G_STMT_END
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 */
138
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,
145                                                  GThreadFunc     func,
146                                                  gpointer        data);
147 GLIB_AVAILABLE_IN_2_32
148 GThread *       g_thread_try_new                (const gchar    *name,
149                                                  GThreadFunc     func,
150                                                  gpointer        data,
151                                                  GError        **error);
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);
160
161
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);
172
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);
189
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);
200
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,
207                                                  GMutex         *mutex);
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,
214                                                  GMutex         *mutex,
215                                                  gint64          end_time);
216
217 GLIB_AVAILABLE_IN_ALL
218 gpointer        g_private_get                   (GPrivate       *key);
219 GLIB_AVAILABLE_IN_ALL
220 void            g_private_set                   (GPrivate       *key,
221                                                  gpointer        value);
222 GLIB_AVAILABLE_IN_2_32
223 void            g_private_replace               (GPrivate       *key,
224                                                  gpointer        value);
225
226 GLIB_AVAILABLE_IN_ALL
227 gpointer        g_once_impl                     (GOnce          *once,
228                                                  GThreadFunc     func,
229                                                  gpointer        arg);
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,
234                                                  gsize           result);
235
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) ? \
241    (once)->retval : \
242    g_once_impl ((once), (func), (arg)))
243 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
244
245 #ifdef __GNUC__
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));                                  \
252   }))
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));                \
258   }))
259 #else
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)))
264 #endif
265
266 GLIB_AVAILABLE_IN_2_36
267 guint          g_get_num_processors (void);
268
269 G_END_DECLS
270
271 #endif /* __G_THREAD_H__ */