Add some mainloop instrumentation
[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, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17  * USA.
18  */
19
20 /*
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/.
25  */
26
27 #ifndef __G_THREAD_H__
28 #define __G_THREAD_H__
29
30 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
31 #error "Only <glib.h> can be included directly."
32 #endif
33
34 #include <glib/gatomic.h>
35 #include <glib/gerror.h>
36
37 G_BEGIN_DECLS
38
39 #define G_THREAD_ERROR g_thread_error_quark ()
40 GLIB_AVAILABLE_IN_ALL
41 GQuark g_thread_error_quark (void);
42
43 typedef enum
44 {
45   G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
46 } GThreadError;
47
48 typedef gpointer (*GThreadFunc) (gpointer data);
49
50 typedef struct _GThread         GThread;
51
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;
58
59 union _GMutex
60 {
61   /*< private >*/
62   gpointer p;
63   guint i[2];
64 };
65
66 struct _GRWLock
67 {
68   /*< private >*/
69   gpointer p;
70   guint i[2];
71 };
72
73 struct _GCond
74 {
75   /*< private >*/
76   gpointer p;
77   guint i[2];
78 };
79
80 struct _GRecMutex
81 {
82   /*< private >*/
83   gpointer p;
84   guint i[2];
85 };
86
87 #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
88 struct _GPrivate
89 {
90   /*< private >*/
91   gpointer       p;
92   GDestroyNotify notify;
93   gpointer future[2];
94 };
95
96 typedef enum
97 {
98   G_ONCE_STATUS_NOTCALLED,
99   G_ONCE_STATUS_PROGRESS,
100   G_ONCE_STATUS_READY
101 } GOnceStatus;
102
103 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
104 struct _GOnce
105 {
106   volatile GOnceStatus status;
107   volatile gpointer retval;
108 };
109
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)
114
115 #ifdef G_DEBUG_LOCKS
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,              \
120              #name);                                            \
121       g_mutex_lock (&G_LOCK_NAME (name));                       \
122    }G_STMT_END
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,              \
127              #name);                                            \
128      g_mutex_unlock (&G_LOCK_NAME (name));                      \
129    }G_STMT_END
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 */
140
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,
147                                                  GThreadFunc     func,
148                                                  gpointer        data);
149 GLIB_AVAILABLE_IN_2_32
150 GThread *       g_thread_try_new                (const gchar    *name,
151                                                  GThreadFunc     func,
152                                                  gpointer        data,
153                                                  GError        **error);
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);
162
163
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);
174
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);
191
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);
202
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,
209                                                  GMutex         *mutex);
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,
216                                                  GMutex         *mutex,
217                                                  gint64          end_time);
218
219 GLIB_AVAILABLE_IN_ALL
220 gpointer        g_private_get                   (GPrivate       *key);
221 GLIB_AVAILABLE_IN_ALL
222 void            g_private_set                   (GPrivate       *key,
223                                                  gpointer        value);
224 GLIB_AVAILABLE_IN_2_32
225 void            g_private_replace               (GPrivate       *key,
226                                                  gpointer        value);
227
228 GLIB_AVAILABLE_IN_ALL
229 gpointer        g_once_impl                     (GOnce          *once,
230                                                  GThreadFunc     func,
231                                                  gpointer        arg);
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,
236                                                  gsize           result);
237
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) ? \
243    (once)->retval : \
244    g_once_impl ((once), (func), (arg)))
245 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
246
247 #ifdef __GNUC__
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));                                  \
254   }))
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));                \
260   }))
261 #else
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)))
266 #endif
267
268 GLIB_AVAILABLE_IN_2_36
269 guint          g_get_num_processors (void);
270
271 G_END_DECLS
272
273 #endif /* __G_THREAD_H__ */