Move typedef GStaticPrivate to deprecated/
[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
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 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but 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
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
30
31 #ifndef __G_THREAD_H__
32 #define __G_THREAD_H__
33
34 #include <glib/gerror.h>
35 #include <glib/gutils.h>        /* for G_INLINE_FUNC */
36 #include <glib/gatomic.h>       /* for g_atomic_pointer_get */
37
38 G_BEGIN_DECLS
39
40 /* GLib Thread support
41  */
42
43 extern GQuark g_thread_error_quark (void);
44 #define G_THREAD_ERROR g_thread_error_quark ()
45
46 typedef enum
47 {
48   G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
49 } GThreadError;
50
51 typedef gpointer (*GThreadFunc) (gpointer data);
52
53 typedef struct _GThread         GThread;
54
55 typedef union  _GMutex          GMutex;
56 typedef struct _GRecMutex       GRecMutex;
57 typedef struct _GRWLock         GRWLock;
58 typedef struct _GCond           GCond;
59 typedef struct _GPrivate        GPrivate;
60
61 union _GMutex
62 {
63   /*< private >*/
64   gpointer p;
65   guint i[2];
66 };
67
68 struct _GRWLock
69 {
70   /*< private >*/
71   gpointer p;
72   guint i[2];
73 };
74
75 struct _GCond
76 {
77   /*< private >*/
78   gpointer p;
79   guint i[2];
80 };
81
82 struct _GRecMutex
83 {
84   /*< private >*/
85   gpointer p;
86   guint i[2];
87 };
88
89 #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
90 struct _GPrivate
91 {
92   gpointer       p;
93   GDestroyNotify notify;
94   gpointer future[2];
95 };
96
97 GThread *g_thread_new                    (const gchar  *name,
98                                           GThreadFunc   func,
99                                           gpointer      data,
100                                           gboolean      joinable,
101                                           GError      **error);
102
103 GThread *g_thread_new_full               (const gchar  *name,
104                                           GThreadFunc   func,
105                                           gpointer      data,
106                                           gboolean      joinable,
107                                           gsize         stack_size,
108                                           GError      **error);
109
110 GThread* g_thread_self                   (void);
111 void     g_thread_exit                   (gpointer      retval);
112 gpointer g_thread_join                   (GThread      *thread);
113 void     g_thread_yield                  (void);
114
115 typedef enum
116 {
117   G_ONCE_STATUS_NOTCALLED,
118   G_ONCE_STATUS_PROGRESS,
119   G_ONCE_STATUS_READY
120 } GOnceStatus;
121
122 typedef struct _GOnce GOnce;
123 struct _GOnce
124 {
125   volatile GOnceStatus status;
126   volatile gpointer retval;
127 };
128
129 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
130
131 gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
132
133 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
134 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
135 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
136 # define g_once(once, func, arg) \
137   (((once)->status == G_ONCE_STATUS_READY) ? \
138    (once)->retval : \
139    g_once_impl ((once), (func), (arg)))
140 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
141
142 /* initialize-once guards, keyed by location */
143 gboolean        g_once_init_enter       (volatile void *location);
144 void            g_once_init_leave       (volatile void *location,
145                                          gsize          result);
146
147 #ifdef __GNUC__
148 # define g_once_init_enter(location) \
149   (G_GNUC_EXTENSION ({                                               \
150     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));       \
151     (void) (0 ? (gpointer) *(location) : 0);                         \
152     (!g_atomic_pointer_get (location) &&                             \
153      g_once_init_enter (location));                                  \
154   }))
155 # define g_once_init_leave(location, result) \
156   (G_GNUC_EXTENSION ({                                               \
157     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));       \
158     (void) (0 ? *(location) = (result) : 0);                         \
159     g_once_init_leave ((location), (gsize) (result));                \
160   }))
161 #else
162 # define g_once_init_enter(location) \
163   (g_once_init_enter((location)))
164 # define g_once_init_leave(location, result) \
165   (g_once_init_leave((location), (gsize) (result)))
166 #endif
167
168 #define G_LOCK_NAME(name)               g__ ## name ## _lock
169 #define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
170 #define G_LOCK_DEFINE(name)           GMutex G_LOCK_NAME (name)
171 #define G_LOCK_EXTERN(name)           extern GMutex G_LOCK_NAME (name)
172
173 #ifdef G_DEBUG_LOCKS
174 #  define G_LOCK(name)                G_STMT_START{             \
175       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
176              "file %s: line %d (%s): locking: %s ",             \
177              __FILE__,        __LINE__, G_STRFUNC,              \
178              #name);                                            \
179       g_mutex_lock (&G_LOCK_NAME (name));                       \
180    }G_STMT_END
181 #  define G_UNLOCK(name)              G_STMT_START{             \
182       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
183              "file %s: line %d (%s): unlocking: %s ",           \
184              __FILE__,        __LINE__, G_STRFUNC,              \
185              #name);                                            \
186      g_mutex_unlock (&G_LOCK_NAME (name));                      \
187    }G_STMT_END
188 #  define G_TRYLOCK(name)                                       \
189       (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                  \
190              "file %s: line %d (%s): try locking: %s ",         \
191              __FILE__,        __LINE__, G_STRFUNC,              \
192              #name), g_mutex_trylock (&G_LOCK_NAME (name)))
193 #else  /* !G_DEBUG_LOCKS */
194 #  define G_LOCK(name) g_mutex_lock       (&G_LOCK_NAME (name))
195 #  define G_UNLOCK(name) g_mutex_unlock   (&G_LOCK_NAME (name))
196 #  define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
197 #endif /* !G_DEBUG_LOCKS */
198
199
200 void                    g_mutex_init                                    (GMutex         *mutex);
201 void                    g_mutex_clear                                   (GMutex         *mutex);
202
203 void                    g_mutex_lock                                    (GMutex         *mutex);
204 void                    g_mutex_unlock                                  (GMutex         *mutex);
205 gboolean                g_mutex_trylock                                 (GMutex         *mutex);
206
207 void                    g_rw_lock_init                                  (GRWLock        *rw_lock);
208 void                    g_rw_lock_clear                                 (GRWLock        *rw_lock);
209 void                    g_rw_lock_writer_lock                           (GRWLock        *rw_lock);
210 gboolean                g_rw_lock_writer_trylock                        (GRWLock        *rw_lock);
211 void                    g_rw_lock_writer_unlock                         (GRWLock        *rw_lock);
212 void                    g_rw_lock_reader_lock                           (GRWLock        *rw_lock);
213 gboolean                g_rw_lock_reader_trylock                        (GRWLock        *rw_lock);
214 void                    g_rw_lock_reader_unlock                         (GRWLock        *rw_lock);
215
216 void                    g_rec_mutex_init                                (GRecMutex      *rec_mutex);
217 void                    g_rec_mutex_clear                               (GRecMutex      *rec_mutex);
218 void                    g_rec_mutex_lock                                (GRecMutex      *rec_mutex);
219 gboolean                g_rec_mutex_trylock                             (GRecMutex      *rec_mutex);
220 void                    g_rec_mutex_unlock                              (GRecMutex      *rec_mutex);
221
222 void                    g_cond_init                                     (GCond          *cond);
223 void                    g_cond_clear                                    (GCond          *cond);
224
225 void                    g_cond_wait                                     (GCond          *cond,
226                                                                          GMutex         *mutex);
227 void                    g_cond_signal                                   (GCond          *cond);
228 void                    g_cond_broadcast                                (GCond          *cond);
229 gboolean                g_cond_timed_wait                               (GCond          *cond,
230                                                                          GMutex         *mutex,
231                                                                          GTimeVal       *timeval);
232 gboolean                g_cond_timedwait                                (GCond          *cond,
233                                                                          GMutex         *mutex,
234                                                                          gint64          abs_time);
235
236 gpointer                g_private_get                                   (GPrivate       *key);
237 void                    g_private_set                                   (GPrivate       *key,
238                                                                          gpointer        value);
239 void                    g_private_replace                               (GPrivate       *key,
240                                                                          gpointer        value);
241
242 G_END_DECLS
243
244 #endif /* __G_THREAD_H__ */