1 module glib.gthread;
2 
3 import glib.gtypes;
4 import glib.gatomic;
5 import glib.gerror;
6 
7 
8 //enum G_THREAD_ERROR = g_thread_error_quark();
9 
10 extern(C) GQuark g_thread_error_quark ();
11 
12 enum GThreadError
13 {
14   G_THREAD_ERROR_AGAIN /* Resource temporarily unavailable */
15 }
16 
17 extern(C) alias GThreadFunc = gpointer function(gpointer data);
18 
19 struct GThread;
20 
21 union GMutex
22 {
23   gpointer p;
24   guint[2] i;
25 }
26 
27 struct GRWLock
28 {
29   gpointer p;
30   guint[2] i;
31 }
32 
33 struct GCond
34 {
35   gpointer p;
36   guint[2] i;
37 }
38 
39 struct GRecMutex
40 {
41   gpointer p;
42   guint[2] i;
43 }
44 
45 auto G_PRIVATE_INIT(N)(N notify) {
46     return GPrivate(null, notify, [ null, null ]);
47 }
48 
49 struct GPrivate
50 {
51   gpointer       p;
52   GDestroyNotify notify;
53   gpointer[2] future;
54 }
55 
56 enum GOnceStatus
57 {
58   G_ONCE_STATUS_NOTCALLED,
59   G_ONCE_STATUS_PROGRESS,
60   G_ONCE_STATUS_READY
61 }
62 
63 
64 auto G_ONCE_INIT() {
65     return GOnce(GOnceStatus.G_ONCE_STATUS_NOTCALLED, null);
66 }
67 
68 struct GOnce
69 {
70   GOnceStatus status;
71   gpointer retval;
72 }
73 
74 // #define G_LOCK_NAME(name)             g__ ## name ## _lock
75 // #define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
76 // #define G_LOCK_DEFINE(name)           GMutex G_LOCK_NAME (name)
77 // #define G_LOCK_EXTERN(name)           extern GMutex G_LOCK_NAME (name)
78 //
79 // #ifdef G_DEBUG_LOCKS
80 // #  define G_LOCK(name)                G_STMT_START{             \
81 //       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
82 //              "file %s: line %d (%s): locking: %s ",             \
83 //              __FILE__,        __LINE__, G_STRFUNC,              \
84 //              #name);                                            \
85 //       g_mutex_lock (&G_LOCK_NAME (name));                       \
86 //    }G_STMT_END
87 // #  define G_UNLOCK(name)              G_STMT_START{             \
88 //       g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
89 //              "file %s: line %d (%s): unlocking: %s ",           \
90 //              __FILE__,        __LINE__, G_STRFUNC,              \
91 //              #name);                                            \
92 //      g_mutex_unlock (&G_LOCK_NAME (name));                      \
93 //    }G_STMT_END
94 // #  define G_TRYLOCK(name)                                       \
95 //       (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                  \
96 //              "file %s: line %d (%s): try locking: %s ",         \
97 //              __FILE__,        __LINE__, G_STRFUNC,              \
98 //              #name), g_mutex_trylock (&G_LOCK_NAME (name)))
99 // #else  /* !G_DEBUG_LOCKS */
100 // #  define G_LOCK(name) g_mutex_lock       (&G_LOCK_NAME (name))
101 // #  define G_UNLOCK(name) g_mutex_unlock   (&G_LOCK_NAME (name))
102 // #  define G_TRYLOCK(name) g_mutex_trylock (&G_LOCK_NAME (name))
103 // #endif /* !G_DEBUG_LOCKS */
104 
105 extern(C) {
106 
107     GThread * g_thread_ref (GThread *thread);
108 
109     void g_thread_unref (GThread *thread);
110 
111     GThread * g_thread_new (const(gchar) *name,
112                             GThreadFunc func,
113                             gpointer data);
114 
115     GThread * g_thread_try_new (const(gchar) *name,
116                                 GThreadFunc func,
117                                 gpointer data,
118                                 GError **error);
119 
120     GThread * g_thread_self ();
121 
122     void g_thread_exit (gpointer retval);
123 
124     gpointer g_thread_join (GThread *thread);
125 
126     void g_thread_yield ();
127 
128 
129 
130     void g_mutex_init (GMutex *mutex);
131 
132     void g_mutex_clear (GMutex *mutex);
133 
134     void g_mutex_lock (GMutex *mutex);
135 
136     gboolean g_mutex_trylock (GMutex *mutex);
137 
138     void g_mutex_unlock (GMutex *mutex);
139 
140 
141     void g_rw_lock_init (GRWLock *rw_lock);
142 
143     void g_rw_lock_clear (GRWLock *rw_lock);
144 
145     void g_rw_lock_writer_lock (GRWLock *rw_lock);
146 
147     gboolean g_rw_lock_writer_trylock (GRWLock *rw_lock);
148 
149     void g_rw_lock_writer_unlock (GRWLock *rw_lock);
150 
151     void g_rw_lock_reader_lock (GRWLock *rw_lock);
152 
153     gboolean g_rw_lock_reader_trylock (GRWLock *rw_lock);
154 
155     void g_rw_lock_reader_unlock (GRWLock *rw_lock);
156 
157 
158     void g_rec_mutex_init (GRecMutex *rec_mutex);
159 
160     void g_rec_mutex_clear (GRecMutex *rec_mutex);
161 
162     void g_rec_mutex_lock (GRecMutex *rec_mutex);
163 
164     gboolean g_rec_mutex_trylock (GRecMutex *rec_mutex);
165 
166     void g_rec_mutex_unlock (GRecMutex *rec_mutex);
167 
168 
169     void g_cond_init (GCond *cond);
170 
171     void g_cond_clear (GCond *cond);
172 
173     void g_cond_wait (GCond *cond, GMutex *mutex);
174 
175     void g_cond_signal (GCond *cond);
176 
177     void g_cond_broadcast (GCond *cond);
178 
179     gboolean g_cond_wait_until (GCond *cond, GMutex *mutex, gint64 end_time);
180 
181 
182     gpointer g_private_get (GPrivate *key);
183 
184     void g_private_set (GPrivate *key, gpointer value);
185 
186     void g_private_replace (GPrivate *key, gpointer value);
187 
188 
189     gpointer g_once_impl (GOnce *once, GThreadFunc func, gpointer arg);
190 
191     gboolean g_once_init_enter (void  *location);
192 
193     void g_once_init_leave (void  *location, gsize result);
194 
195 // #ifdef G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
196 // # define g_once(once, func, arg) g_once_impl ((once), (func), (arg))
197 // #else /* !G_ATOMIC_OP_MEMORY_BARRIER_NEEDED*/
198 // # define g_once(once, func, arg) \
199 //   (((once)->status == G_ONCE_STATUS_READY) ? \
200 //    (once)->retval : \
201 //    g_once_impl ((once), (func), (arg)))
202 // #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */
203 //
204 // #ifdef __GNUC__
205 // # define g_once_init_enter(location) \
206 //   (G_GNUC_EXTENSION ({                                               \
207 //     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));       \
208 //     (void) (0 ? (gpointer) *(location) : 0);                         \
209 //     (!g_atomic_pointer_get (location) &&                             \
210 //      g_once_init_enter (location));                                  \
211 //   }))
212 // # define g_once_init_leave(location, result) \
213 //   (G_GNUC_EXTENSION ({                                               \
214 //     G_STATIC_ASSERT (sizeof *(location) == sizeof (gpointer));       \
215 //     (void) (0 ? *(location) = (result) : 0);                         \
216 //     g_once_init_leave ((location), (gsize) (result));                \
217 //   }))
218 // #else
219 // # define g_once_init_enter(location) \
220 //   (g_once_init_enter((location)))
221 // # define g_once_init_leave(location, result) \
222 //   (g_once_init_leave((location), (gsize) (result)))
223 // #endif
224 
225 
226     guint g_get_num_processors ();
227 
228 } // extern(C)