Add g_get_num_processors()
[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 #if !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/gatomic.h>
35 #include <glib/gerror.h>
36
37 G_BEGIN_DECLS
38
39 #define G_THREAD_ERROR g_thread_error_quark ()
40 GQuark g_thread_error_quark (void);
41
42 typedef enum
43 {
44   G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
45 } GThreadError;
46
47 typedef gpointer (*GThreadFunc) (gpointer data);
48
49 typedef struct _GThread         GThread;
50
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;
57
58 union _GMutex
59 {
60   /*< private >*/
61   gpointer p;
62   guint i[2];
63 };
64
65 struct _GRWLock
66 {
67   /*< private >*/
68   gpointer p;
69   guint i[2];
70 };
71
72 struct _GCond
73 {
74   /*< private >*/
75   gpointer p;
76   guint i[2];
77 };
78
79 struct _GRecMutex
80 {
81   /*< private >*/
82   gpointer p;
83   guint i[2];
84 };
85
86 #define G_PRIVATE_INIT(notify) { NULL, (notify), { NULL, NULL } }
87 struct _GPrivate
88 {
89   /*< private >*/
90   gpointer       p;
91   GDestroyNotify notify;
92   gpointer future[2];
93 };
94
95 typedef enum
96 {
97   G_ONCE_STATUS_NOTCALLED,
98   G_ONCE_STATUS_PROGRESS,
99   G_ONCE_STATUS_READY
100 } GOnceStatus;
101
102 #define G_ONCE_INIT { G_ONCE_STATUS_NOTCALLED, NULL }
103 struct _GOnce
104 {
105   volatile GOnceStatus status;
106   volatile gpointer retval;
107 };
108
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)
113
114 #ifdef G_DEBUG_LOCKS
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,              \
119              #name);                                            \
120       g_mutex_lock (&G_LOCK_NAME (name));                       \
121    }G_STMT_END
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,              \
126              #name);                                            \
127      g_mutex_unlock (&G_LOCK_NAME (name));                      \
128    }G_STMT_END
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 */
139
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,
146                                                  GThreadFunc     func,
147                                                  gpointer        data);
148 GLIB_AVAILABLE_IN_2_32
149 GThread *       g_thread_try_new                (const gchar    *name,
150                                                  GThreadFunc     func,
151                                                  gpointer        data,
152                                                  GError        **error);
153 GThread *       g_thread_self                   (void);
154 void            g_thread_exit                   (gpointer        retval);
155 gpointer        g_thread_join                   (GThread        *thread);
156 void            g_thread_yield                  (void);
157
158
159 GLIB_AVAILABLE_IN_2_32
160 void            g_mutex_init                    (GMutex         *mutex);
161 GLIB_AVAILABLE_IN_2_32
162 void            g_mutex_clear                   (GMutex         *mutex);
163 void            g_mutex_lock                    (GMutex         *mutex);
164 gboolean        g_mutex_trylock                 (GMutex         *mutex);
165 void            g_mutex_unlock                  (GMutex         *mutex);
166
167 GLIB_AVAILABLE_IN_2_32
168 void            g_rw_lock_init                  (GRWLock        *rw_lock);
169 GLIB_AVAILABLE_IN_2_32
170 void            g_rw_lock_clear                 (GRWLock        *rw_lock);
171 GLIB_AVAILABLE_IN_2_32
172 void            g_rw_lock_writer_lock           (GRWLock        *rw_lock);
173 GLIB_AVAILABLE_IN_2_32
174 gboolean        g_rw_lock_writer_trylock        (GRWLock        *rw_lock);
175 GLIB_AVAILABLE_IN_2_32
176 void            g_rw_lock_writer_unlock         (GRWLock        *rw_lock);
177 GLIB_AVAILABLE_IN_2_32
178 void            g_rw_lock_reader_lock           (GRWLock        *rw_lock);
179 GLIB_AVAILABLE_IN_2_32
180 gboolean        g_rw_lock_reader_trylock        (GRWLock        *rw_lock);
181 GLIB_AVAILABLE_IN_2_32
182 void            g_rw_lock_reader_unlock         (GRWLock        *rw_lock);
183
184 GLIB_AVAILABLE_IN_2_32
185 void            g_rec_mutex_init                (GRecMutex      *rec_mutex);
186 GLIB_AVAILABLE_IN_2_32
187 void            g_rec_mutex_clear               (GRecMutex      *rec_mutex);
188 GLIB_AVAILABLE_IN_2_32
189 void            g_rec_mutex_lock                (GRecMutex      *rec_mutex);
190 GLIB_AVAILABLE_IN_2_32
191 gboolean        g_rec_mutex_trylock             (GRecMutex      *rec_mutex);
192 GLIB_AVAILABLE_IN_2_32
193 void            g_rec_mutex_unlock              (GRecMutex      *rec_mutex);
194
195 GLIB_AVAILABLE_IN_2_32
196 void            g_cond_init                     (GCond          *cond);
197 GLIB_AVAILABLE_IN_2_32
198 void            g_cond_clear                    (GCond          *cond);
199 void            g_cond_wait                     (GCond          *cond,
200                                                  GMutex         *mutex);
201 void            g_cond_signal                   (GCond          *cond);
202 void            g_cond_broadcast                (GCond          *cond);
203 GLIB_AVAILABLE_IN_2_32
204 gboolean        g_cond_wait_until               (GCond          *cond,
205                                                  GMutex         *mutex,
206                                                  gint64          end_time);
207
208 gpointer        g_private_get                   (GPrivate       *key);
209 void            g_private_set                   (GPrivate       *key,
210                                                  gpointer        value);
211 GLIB_AVAILABLE_IN_2_32
212 void            g_private_replace               (GPrivate       *key,
213                                                  gpointer        value);
214
215 gpointer        g_once_impl                     (GOnce          *once,
216                                                  GThreadFunc     func,
217                                                  gpointer        arg);
218 gboolean        g_once_init_enter               (volatile void  *location);
219 void            g_once_init_leave               (volatile void  *location,
220                                                  gsize           result);
221
222 #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
223 # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
224 #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
225 # define g_once(once, func, arg) \
226   (((once)->status == G_ONCE_STATUS_READY) ? \
227    (once)->retval : \
228    g_once_impl ((once), (func), (arg)))
229 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
230
231 #ifdef __GNUC__
232 # define g_once_init_enter(location) \
233   (G_GNUC_EXTENSION ({                                               \
234     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));       \
235     (void) (0 ? (gpointer) *(location) : 0);                         \
236     (!g_atomic_pointer_get (location) &&                             \
237      g_once_init_enter (location));                                  \
238   }))
239 # define g_once_init_leave(location, result) \
240   (G_GNUC_EXTENSION ({                                               \
241     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));       \
242     (void) (0 ? *(location) = (result) : 0);                         \
243     g_once_init_leave ((location), (gsize) (result));                \
244   }))
245 #else
246 # define g_once_init_enter(location) \
247   (g_once_init_enter((location)))
248 # define g_once_init_leave(location, result) \
249   (g_once_init_leave((location), (gsize) (result)))
250 #endif
251
252 GLIB_AVAILABLE_IN_2_36
253 guint          g_get_num_processors (void);
254
255 G_END_DECLS
256
257 #endif /* __G_THREAD_H__ */