1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19  
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 /*
25  * Conversion parameters:
26  * inFile  = 
27  * outPack = gthread
28  * outFile = Cond
29  * strct   = GCond
30  * realStrct=
31  * ctorStrct=
32  * clss    = Cond
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_cond_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- gthread.Mutex
47  * structWrap:
48  * 	- GMutex* -> Mutex
49  * module aliases:
50  * local aliases:
51  * overrides:
52  */
53 
54 module gthread.Cond;
55 
56 public  import gtkc.gthreadtypes;
57 
58 private import gtkc.gthread;
59 private import glib.ConstructionException;
60 
61 
62 private import gthread.Mutex;
63 
64 
65 
66 
67 /**
68  * Threads act almost like processes, but unlike processes all threads
69  * of one process share the same memory. This is good, as it provides
70  * easy communication between the involved threads via this shared
71  * memory, and it is bad, because strange things (so called
72  * "Heisenbugs") might happen if the program is not carefully designed.
73  * In particular, due to the concurrent nature of threads, no
74  * assumptions on the order of execution of code running in different
75  * threads can be made, unless order is explicitly forced by the
76  * programmer through synchronization primitives.
77  *
78  * The aim of the thread-related functions in GLib is to provide a
79  * portable means for writing multi-threaded software. There are
80  * primitives for mutexes to protect the access to portions of memory
81  * (GMutex, GRecMutex and GRWLock). There is a facility to use
82  * individual bits for locks (g_bit_lock()). There are primitives
83  * for condition variables to allow synchronization of threads (GCond).
84  * There are primitives for thread-private data - data that every
85  * thread has a private instance of (GPrivate). There are facilities
86  * for one-time initialization (GOnce, g_once_init_enter()). Finally,
87  * there are primitives to create and manage threads (GThread).
88  *
89  * The GLib threading system used to be initialized with g_thread_init().
90  * This is no longer necessary. Since version 2.32, the GLib threading
91  * system is automatically initialized at the start of your program,
92  * and all thread-creation functions and synchronization primitives
93  * are available right away.
94  *
95  * Note that it is not safe to assume that your program has no threads
96  * even if you don't call g_thread_new() yourself. GLib and GIO can
97  * and will create threads for their own purposes in some cases, such
98  * as when using g_unix_signal_source_new() or when using GDBus.
99  *
100  * Originally, UNIX did not have threads, and therefore some traditional
101  * UNIX APIs are problematic in threaded programs. Some notable examples
102  * are
103  *
104  *  C library functions that return data in statically allocated
105  *  buffers, such as strtok() or strerror(). For many of these,
106  *  there are thread-safe variants with a _r suffix, or you can
107  *  look at corresponding GLib APIs (like g_strsplit() or g_strerror()).
108  *
109  * setenv() and unsetenv() manipulate the process environment in
110  *  a not thread-safe way, and may interfere with getenv() calls
111  *  in other threads. Note that getenv() calls may be
112  *  “hidden” behind other APIs. For example, GNU gettext()
113  *  calls getenv() under the covers. In general, it is best to treat
114  *  the environment as readonly. If you absolutely have to modify the
115  *  environment, do it early in main(), when no other threads are around yet.
116  *
117  * setlocale() changes the locale for the entire process, affecting
118  *  all threads. Temporary changes to the locale are often made to
119  *  change the behavior of string scanning or formatting functions
120  *  like scanf() or printf(). GLib offers a number of string APIs
121  *  (like g_ascii_formatd() or g_ascii_strtod()) that can often be
122  *  used as an alternative. Or you can use the uselocale() function
123  *  to change the locale only for the current thread.
124  *
125  * fork() only takes the calling thread into the child's copy of the
126  *  process image. If other threads were executing in critical
127  *  sections they could have left mutexes locked which could easily
128  *  cause deadlocks in the new child. For this reason, you should
129  *  call exit() or exec() as soon as possible in the child and only
130  *  make signal-safe library calls before that.
131  *
132  * daemon() uses fork() in a way contrary to what is described
133  *  above. It should not be used with GLib programs.
134  *
135  * GLib itself is internally completely thread-safe (all global data is
136  * automatically locked), but individual data structure instances are
137  * not automatically locked for performance reasons. For example,
138  * you must coordinate accesses to the same GHashTable from multiple
139  * threads. The two notable exceptions from this rule are GMainLoop
140  * and GAsyncQueue, which are thread-safe and
141  * need no further application-level locking to be accessed from
142  * multiple threads. Most refcounting functions such as g_object_ref()
143  * are also thread-safe.
144  */
145 public class Cond
146 {
147 	
148 	/** the main Gtk struct */
149 	protected GCond* gCond;
150 	
151 	
152 	public GCond* getCondStruct()
153 	{
154 		return gCond;
155 	}
156 	
157 	
158 	/** the main Gtk struct as a void* */
159 	protected void* getStruct()
160 	{
161 		return cast(void*)gCond;
162 	}
163 	
164 	/**
165 	 * Sets our main struct and passes it to the parent class
166 	 */
167 	public this (GCond* gCond)
168 	{
169 		this.gCond = gCond;
170 	}
171 	
172 	/**
173 	 */
174 	
175 	/**
176 	 * Initialises a GCond so that it can be used.
177 	 * This function is useful to initialise a GCond that has been
178 	 * allocated as part of a larger structure. It is not necessary to
179 	 * initialise a GCond that has been statically allocated.
180 	 * To undo the effect of g_cond_init() when a GCond is no longer
181 	 * needed, use g_cond_clear().
182 	 * Calling g_cond_init() on an already-initialised GCond leads
183 	 * to undefined behaviour.
184 	 * Since 2.32
185 	 */
186 	public void init()
187 	{
188 		// void g_cond_init (GCond *cond);
189 		g_cond_init(gCond);
190 	}
191 	
192 	/**
193 	 * Frees the resources allocated to a GCond with g_cond_init().
194 	 * This function should not be used with a GCond that has been
195 	 * statically allocated.
196 	 * Calling g_cond_clear() for a GCond on which threads are
197 	 * blocking leads to undefined behaviour.
198 	 * Since 2.32
199 	 */
200 	public void clear()
201 	{
202 		// void g_cond_clear (GCond *cond);
203 		g_cond_clear(gCond);
204 	}
205 	
206 	/**
207 	 * Atomically releases mutex and waits until cond is signalled.
208 	 * When this function returns, mutex is locked again and owned by the
209 	 * calling thread.
210 	 * When using condition variables, it is possible that a spurious wakeup
211 	 * may occur (ie: g_cond_wait() returns even though g_cond_signal() was
212 	 * not called). It's also possible that a stolen wakeup may occur.
213 	 * This is when g_cond_signal() is called, but another thread acquires
214 	 * mutex before this thread and modifies the state of the program in
215 	 * such a way that when g_cond_wait() is able to return, the expected
216 	 * condition is no longer met.
217 	 * For this reason, g_cond_wait() must always be used in a loop. See
218 	 * the documentation for GCond for a complete example.
219 	 * Params:
220 	 * mutex = a GMutex that is currently locked
221 	 */
222 	public void wait(Mutex mutex)
223 	{
224 		// void g_cond_wait (GCond *cond,  GMutex *mutex);
225 		g_cond_wait(gCond, (mutex is null) ? null : mutex.getMutexStruct());
226 	}
227 	
228 	/**
229 	 * Warning
230 	 * g_cond_timed_wait has been deprecated since version 2.32 and should not be used in newly-written code. Use g_cond_wait_until() instead.
231 	 * Waits until this thread is woken up on cond, but not longer than
232 	 * until the time specified by abs_time. The mutex is unlocked before
233 	 * falling asleep and locked again before resuming.
234 	 * If abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
235 	 * This function can be used even if g_thread_init() has not yet been
236 	 * called, and, in that case, will immediately return TRUE.
237 	 * To easily calculate abs_time a combination of g_get_current_time()
238 	 * and g_time_val_add() can be used.
239 	 * Params:
240 	 * mutex = a GMutex that is currently locked
241 	 * absTime = a GTimeVal, determining the final time
242 	 * Returns: TRUE if cond was signalled, or FALSE on timeout
243 	 */
244 	public int timedWait(Mutex mutex, ref GTimeVal absTime)
245 	{
246 		// gboolean g_cond_timed_wait (GCond *cond,  GMutex *mutex,  GTimeVal *abs_time);
247 		return g_cond_timed_wait(gCond, (mutex is null) ? null : mutex.getMutexStruct(), &absTime);
248 	}
249 	
250 	/**
251 	 * Waits until either cond is signalled or end_time has passed.
252 	 * As with g_cond_wait() it is possible that a spurious or stolen wakeup
253 	 * could occur. For that reason, waiting on a condition variable should
254 	 * always be in a loop, based on an explicitly-checked predicate.
255 	 * TRUE is returned if the condition variable was signalled (or in the
256 	 * case of a spurious wakeup). FALSE is returned if end_time has
257 	 * passed.
258 	 * The following code shows how to correctly perform a timed wait on a
259 	 * condition variable (extended the example presented in the
260 	 * Since 2.32
261 	 * Params:
262 	 * mutex = a GMutex that is currently locked
263 	 * endTime = the monotonic time to wait until
264 	 * Returns: TRUE on a signal, FALSE on a timeout
265 	 */
266 	public int waitUntil(Mutex mutex, long endTime)
267 	{
268 		// gboolean g_cond_wait_until (GCond *cond,  GMutex *mutex,  gint64 end_time);
269 		return g_cond_wait_until(gCond, (mutex is null) ? null : mutex.getMutexStruct(), endTime);
270 	}
271 	
272 	/**
273 	 * If threads are waiting for cond, at least one of them is unblocked.
274 	 * If no threads are waiting for cond, this function has no effect.
275 	 * It is good practice to hold the same lock as the waiting thread
276 	 * while calling this function, though not required.
277 	 */
278 	public void signal()
279 	{
280 		// void g_cond_signal (GCond *cond);
281 		g_cond_signal(gCond);
282 	}
283 	
284 	/**
285 	 * If threads are waiting for cond, all of them are unblocked.
286 	 * If no threads are waiting for cond, this function has no effect.
287 	 * It is good practice to lock the same mutex as the waiting threads
288 	 * while calling this function, though not required.
289 	 */
290 	public void broadcast()
291 	{
292 		// void g_cond_broadcast (GCond *cond);
293 		g_cond_broadcast(gCond);
294 	}
295 }