alternative to assert
[gtkD.git] / gtkD / src / gthread / Cond.d
blob783789bd1adcfec5b262b84592149f2fc84c9b8e
1 /*
2 * This file is part of gtkD.
4 * gtkD is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * gtkD 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
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with gtkD; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // generated automatically - do not change
20 // find conversion definition on APILookup.txt
21 // implement new conversion functionalities on the wrap.utils pakage
24 * Conversion parameters:
25 * inFile =
26 * outPack = gthread
27 * outFile = Cond
28 * strct = GCond
29 * realStrct=
30 * ctorStrct=
31 * clss = Cond
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_cond_
40 * omit structs:
41 * omit prefixes:
42 * omit code:
43 * imports:
44 * - glib.Date
45 * - gthread.Mutex
46 * structWrap:
47 * - GDate* -> Date
48 * - GMutex* -> Mutex
49 * module aliases:
50 * local aliases:
53 module gthread.Cond;
55 version(noAssert)
57 version(Tango)
59 import tango.io.Stdout; // use the tango loging?
63 private import gtkc.gthreadtypes;
65 private import gtkc.gthread;
68 private import glib.Date;
69 private import gthread.Mutex;
74 /**
75 * Description
76 * Threads act almost like processes, but unlike processes all threads of
77 * one process share the same memory. This is good, as it provides easy
78 * communication between the involved threads via this shared memory, and
79 * it is bad, because strange things (so called "Heisenbugs") might
80 * happen if the program is not carefully designed. In particular, due to
81 * the concurrent nature of threads, no assumptions on the order of
82 * execution of code running in different threads can be made, unless
83 * order is explicitly forced by the programmer through synchronization
84 * primitives.
85 * The aim of the thread related functions in GLib is to provide a
86 * portable means for writing multi-threaded software. There are
87 * primitives for mutexes to protect the access to portions of memory
88 * (GMutex, GStaticMutex, G_LOCK_DEFINE, GStaticRecMutex and
89 * GStaticRWLock). There are primitives for condition variables to allow
90 * synchronization of threads (GCond). There are primitives
91 * for thread-private data - data that every thread has a private instance of
92 * (GPrivate, GStaticPrivate). Last but definitely not least there are
93 * primitives to portably create and manage threads (GThread).
94 * You must call g_thread_init() before executing any other GLib
95 * functions in a threaded GLib program. After that, GLib is completely
96 * thread safe (all global data is automatically locked), but individual
97 * data structure instances are not automatically locked for performance
98 * reasons. So, for example you must coordinate accesses to the same
99 * GHashTable from multiple threads. The two notable exceptions from
100 * this rule are GMainLoop and GAsyncQueue,
101 * which are threadsafe and needs no further
102 * application-level locking to be accessed from multiple threads.
104 public class Cond
107 /** the main Gtk struct */
108 protected GCond* gCond;
111 public GCond* getCondStruct()
113 return gCond;
117 /** the main Gtk struct as a void* */
118 protected void* getStruct()
120 return cast(void*)gCond;
124 * Sets our main struct and passes it to the parent class
126 public this (GCond* gCond)
128 version(noAssert)
130 if ( gCond is null )
132 int zero = 0;
133 version(Tango)
135 Stdout("struct gCond is null on constructor").newline;
137 else
139 printf("struct gCond is null on constructor");
141 zero = zero / zero;
144 else
146 assert(gCond !is null, "struct gCond is null on constructor");
148 this.gCond = gCond;
214 * Creates a new GCond. This function will abort, if g_thread_init()
215 * has not been called yet.
216 * Returns:
217 * a new GCond.
219 public this ()
221 // GCond* g_cond_new ();
222 this(cast(GCond*)g_cond_new() );
226 * If threads are waiting for cond, exactly one of them is woken up. It
227 * is good practice to hold the same lock as the waiting thread while
228 * calling this function, though not required.
229 * This function can be used even if g_thread_init() has not yet been called,
230 * and, in that case, will do nothing.
231 * cond:
232 * a GCond.
234 public void signal()
236 // void g_cond_signal (GCond *cond);
237 g_cond_signal(gCond);
241 * If threads are waiting for cond, all of them are woken up. It is good
242 * practice to lock the same mutex as the waiting threads, while calling
243 * this function, though not required.
244 * This function can be used even if g_thread_init() has not yet been called,
245 * and, in that case, will do nothing.
246 * cond:
247 * a GCond.
249 public void broadcast()
251 // void g_cond_broadcast (GCond *cond);
252 g_cond_broadcast(gCond);
256 * Waits until this thread is woken up on cond. The mutex is unlocked
257 * before falling asleep and locked again before resuming.
258 * This function can be used even if g_thread_init() has not yet been
259 * called, and, in that case, will immediately return.
260 * cond:
261 * a GCond.
262 * mutex:
263 * a GMutex, that is currently locked.
265 public void wait(Mutex mutex)
267 // void g_cond_wait (GCond *cond, GMutex *mutex);
268 g_cond_wait(gCond, (mutex is null) ? null : mutex.getMutexStruct());
272 * Waits until this thread is woken up on cond, but not longer than
273 * until the time specified by abs_time. The mutex is
274 * unlocked before falling asleep and locked again before resuming.
275 * If abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
276 * This function can be used even if g_thread_init() has not yet been
277 * called, and, in that case, will immediately return TRUE.
278 * To easily calculate abs_time a combination of g_get_current_time()
279 * and g_time_val_add() can be used.
280 * cond:
281 * a GCond.
282 * mutex:
283 * a GMutex that is currently locked.
284 * abs_time:
285 * a GTimeVal, determining the final time.
286 * Returns:
287 * TRUE if cond was signalled, or FALSE on timeout.
289 public int timedWait(Mutex mutex, GTimeVal* absTime)
291 // gboolean g_cond_timed_wait (GCond *cond, GMutex *mutex, GTimeVal *abs_time);
292 return g_cond_timed_wait(gCond, (mutex is null) ? null : mutex.getMutexStruct(), absTime);
296 * Destroys the GCond.
297 * cond:
298 * a GCond.
300 public void free()
302 // void g_cond_free (GCond *cond);
303 g_cond_free(gCond);