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  * 	- glib.TimeVal
48  * structWrap:
49  * 	- GMutex* -> Mutex
50  * 	- GTimeVal* -> TimeVal
51  * module aliases:
52  * local aliases:
53  * overrides:
54  */
55 
56 module gthread.Cond;
57 
58 public  import gtkc.gthreadtypes;
59 
60 private import gtkc.gthread;
61 private import glib.ConstructionException;
62 
63 
64 private import gthread.Mutex;
65 private import glib.TimeVal;
66 
67 
68 
69 
70 /**
71  * Description
72  * Threads act almost like processes, but unlike processes all threads
73  * of one process share the same memory. This is good, as it provides
74  * easy communication between the involved threads via this shared
75  * memory, and it is bad, because strange things (so called
76  * "Heisenbugs") might happen if the program is not carefully designed.
77  * In particular, due to the concurrent nature of threads, no
78  * assumptions on the order of execution of code running in different
79  * threads can be made, unless order is explicitly forced by the
80  * programmer through synchronization primitives.
81  * The aim of the thread related functions in GLib is to provide a
82  * portable means for writing multi-threaded software. There are
83  * primitives for mutexes to protect the access to portions of memory
84  * (GMutex, GStaticMutex, G_LOCK_DEFINE, GStaticRecMutex and
85  * GStaticRWLock). There are primitives for condition variables to
86  * allow synchronization of threads (GCond). There are primitives for
87  * thread-private data - data that every thread has a private instance
88  * of (GPrivate, GStaticPrivate). Last but definitely not least there
89  * are primitives to portably create and manage threads (GThread).
90  * The threading system is initialized with g_thread_init(), which
91  * takes an optional custom thread implementation or NULL for the
92  * default implementation. If you want to call g_thread_init() with a
93  * non-NULL argument this must be done before executing any other GLib
94  * functions (except g_mem_set_vtable()). This is a requirement even if
95  * no threads are in fact ever created by the process.
96  * Calling g_thread_init() with a NULL argument is somewhat more
97  * relaxed. You may call any other glib functions in the main thread
98  * before g_thread_init() as long as g_thread_init() is not called from
99  * a glib callback, or with any locks held. However, many libraries
100  * above glib does not support late initialization of threads, so doing
101  * this should be avoided if possible.
102  * Please note that since version 2.24 the GObject initialization
103  * function g_type_init() initializes threads (with a NULL argument),
104  * so most applications, including those using Gtk+ will run with
105  * threads enabled. If you want a special thread implementation, make
106  * sure you call g_thread_init() before g_type_init() is called.
107  * After calling g_thread_init(), GLib is completely thread safe (all
108  * global data is automatically locked), but individual data structure
109  * instances are not automatically locked for performance reasons. So,
110  * for example you must coordinate accesses to the same GHashTable
111  * from multiple threads. The two notable exceptions from this rule
112  * are GMainLoop and GAsyncQueue, which are
113  * threadsafe and need no further application-level locking to be
114  * accessed from multiple threads.
115  * To help debugging problems in multithreaded applications, GLib
116  * supports error-checking mutexes that will give you helpful error
117  * messages on common problems. To use error-checking mutexes, define
118  * the symbol G_ERRORCHECK_MUTEXES when compiling the application.
119  */
120 public class Cond
121 {
122 	
123 	/** the main Gtk struct */
124 	protected GCond* gCond;
125 	
126 	
127 	public GCond* getCondStruct()
128 	{
129 		return gCond;
130 	}
131 	
132 	
133 	/** the main Gtk struct as a void* */
134 	protected void* getStruct()
135 	{
136 		return cast(void*)gCond;
137 	}
138 	
139 	/**
140 	 * Sets our main struct and passes it to the parent class
141 	 */
142 	public this (GCond* gCond)
143 	{
144 		this.gCond = gCond;
145 	}
146 	
147 	/**
148 	 */
149 	
150 	/**
151 	 * Creates a new GCond. This function will abort, if g_thread_init()
152 	 * has not been called yet.
153 	 * Throws: ConstructionException GTK+ fails to create the object.
154 	 */
155 	public this ()
156 	{
157 		// GCond* g_cond_new ();
158 		auto p = g_cond_new();
159 		if(p is null)
160 		{
161 			throw new ConstructionException("null returned by g_cond_new()");
162 		}
163 		this(cast(GCond*) p);
164 	}
165 	
166 	/**
167 	 * If threads are waiting for cond, exactly one of them is woken up.
168 	 * It is good practice to hold the same lock as the waiting thread
169 	 * while calling this function, though not required.
170 	 * This function can be used even if g_thread_init() has not yet been
171 	 * called, and, in that case, will do nothing.
172 	 */
173 	public void signal()
174 	{
175 		// void g_cond_signal (GCond *cond);
176 		g_cond_signal(gCond);
177 	}
178 	
179 	/**
180 	 * If threads are waiting for cond, all of them are woken up. It is
181 	 * good practice to lock the same mutex as the waiting threads, while
182 	 * calling this function, though not required.
183 	 * This function can be used even if g_thread_init() has not yet been
184 	 * called, and, in that case, will do nothing.
185 	 */
186 	public void broadcast()
187 	{
188 		// void g_cond_broadcast (GCond *cond);
189 		g_cond_broadcast(gCond);
190 	}
191 	
192 	/**
193 	 * Waits until this thread is woken up on cond. The mutex is unlocked
194 	 * before falling asleep and locked again before resuming.
195 	 * This function can be used even if g_thread_init() has not yet been
196 	 * called, and, in that case, will immediately return.
197 	 * Params:
198 	 * mutex = a GMutex, that is currently locked.
199 	 */
200 	public void wait(Mutex mutex)
201 	{
202 		// void g_cond_wait (GCond *cond,  GMutex *mutex);
203 		g_cond_wait(gCond, (mutex is null) ? null : mutex.getMutexStruct());
204 	}
205 	
206 	/**
207 	 * Waits until this thread is woken up on cond, but not longer than
208 	 * until the time specified by abs_time. The mutex is unlocked before
209 	 * falling asleep and locked again before resuming.
210 	 * If abs_time is NULL, g_cond_timed_wait() acts like g_cond_wait().
211 	 * This function can be used even if g_thread_init() has not yet been
212 	 * called, and, in that case, will immediately return TRUE.
213 	 * To easily calculate abs_time a combination of g_get_current_time()
214 	 * and g_time_val_add() can be used.
215 	 * Params:
216 	 * mutex = a GMutex that is currently locked.
217 	 * absTime = a GTimeVal, determining the final time.
218 	 * Returns: TRUE if cond was signalled, or FALSE on timeout.
219 	 */
220 	public int timedWait(Mutex mutex, TimeVal absTime)
221 	{
222 		// gboolean g_cond_timed_wait (GCond *cond,  GMutex *mutex,  GTimeVal *abs_time);
223 		return g_cond_timed_wait(gCond, (mutex is null) ? null : mutex.getMutexStruct(), (absTime is null) ? null : absTime.getTimeValStruct());
224 	}
225 	
226 	/**
227 	 * Destroys the GCond.
228 	 */
229 	public void free()
230 	{
231 		// void g_cond_free (GCond *cond);
232 		g_cond_free(gCond);
233 	}
234 }