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 module glib.Cond;
26 
27 private import glib.Mutex;
28 private import glib.c.functions;
29 public  import glib.c.types;
30 public  import gtkc.glibtypes;
31 
32 
33 /**
34  * The #GCond struct is an opaque data structure that represents a
35  * condition. Threads can block on a #GCond if they find a certain
36  * condition to be false. If other threads change the state of this
37  * condition they signal the #GCond, and that causes the waiting
38  * threads to be woken up.
39  * 
40  * Consider the following example of a shared variable.  One or more
41  * threads can wait for data to be published to the variable and when
42  * another thread publishes the data, it can signal one of the waiting
43  * threads to wake up to collect the data.
44  * 
45  * Here is an example for using GCond to block a thread until a condition
46  * is satisfied:
47  * |[<!-- language="C" -->
48  * gpointer current_data = NULL;
49  * GMutex data_mutex;
50  * GCond data_cond;
51  * 
52  * void
53  * push_data (gpointer data)
54  * {
55  * g_mutex_lock (&data_mutex);
56  * current_data = data;
57  * g_cond_signal (&data_cond);
58  * g_mutex_unlock (&data_mutex);
59  * }
60  * 
61  * gpointer
62  * pop_data (void)
63  * {
64  * gpointer data;
65  * 
66  * g_mutex_lock (&data_mutex);
67  * while (!current_data)
68  * g_cond_wait (&data_cond, &data_mutex);
69  * data = current_data;
70  * current_data = NULL;
71  * g_mutex_unlock (&data_mutex);
72  * 
73  * return data;
74  * }
75  * ]|
76  * Whenever a thread calls pop_data() now, it will wait until
77  * current_data is non-%NULL, i.e. until some other thread
78  * has called push_data().
79  * 
80  * The example shows that use of a condition variable must always be
81  * paired with a mutex.  Without the use of a mutex, there would be a
82  * race between the check of @current_data by the while loop in
83  * pop_data() and waiting. Specifically, another thread could set
84  * @current_data after the check, and signal the cond (with nobody
85  * waiting on it) before the first thread goes to sleep. #GCond is
86  * specifically useful for its ability to release the mutex and go
87  * to sleep atomically.
88  * 
89  * It is also important to use the g_cond_wait() and g_cond_wait_until()
90  * functions only inside a loop which checks for the condition to be
91  * true.  See g_cond_wait() for an explanation of why the condition may
92  * not be true even after it returns.
93  * 
94  * If a #GCond is allocated in static storage then it can be used
95  * without initialisation.  Otherwise, you should call g_cond_init()
96  * on it and g_cond_clear() when done.
97  * 
98  * A #GCond should only be accessed via the g_cond_ functions.
99  */
100 public class Cond
101 {
102 	/** the main Gtk struct */
103 	protected GCond* gCond;
104 	protected bool ownedRef;
105 
106 	/** Get the main Gtk struct */
107 	public GCond* getCondStruct(bool transferOwnership = false)
108 	{
109 		if (transferOwnership)
110 			ownedRef = false;
111 		return gCond;
112 	}
113 
114 	/** the main Gtk struct as a void* */
115 	protected void* getStruct()
116 	{
117 		return cast(void*)gCond;
118 	}
119 
120 	/**
121 	 * Sets our main struct and passes it to the parent class.
122 	 */
123 	public this (GCond* gCond, bool ownedRef = false)
124 	{
125 		this.gCond = gCond;
126 		this.ownedRef = ownedRef;
127 	}
128 
129 
130 	/**
131 	 * If threads are waiting for @cond, all of them are unblocked.
132 	 * If no threads are waiting for @cond, this function has no effect.
133 	 * It is good practice to lock the same mutex as the waiting threads
134 	 * while calling this function, though not required.
135 	 */
136 	public void broadcast()
137 	{
138 		g_cond_broadcast(gCond);
139 	}
140 
141 	/**
142 	 * Frees the resources allocated to a #GCond with g_cond_init().
143 	 *
144 	 * This function should not be used with a #GCond that has been
145 	 * statically allocated.
146 	 *
147 	 * Calling g_cond_clear() for a #GCond on which threads are
148 	 * blocking leads to undefined behaviour.
149 	 *
150 	 * Since: 2.32
151 	 */
152 	public void clear()
153 	{
154 		g_cond_clear(gCond);
155 	}
156 
157 	/**
158 	 * Initialises a #GCond so that it can be used.
159 	 *
160 	 * This function is useful to initialise a #GCond that has been
161 	 * allocated as part of a larger structure.  It is not necessary to
162 	 * initialise a #GCond that has been statically allocated.
163 	 *
164 	 * To undo the effect of g_cond_init() when a #GCond is no longer
165 	 * needed, use g_cond_clear().
166 	 *
167 	 * Calling g_cond_init() on an already-initialised #GCond leads
168 	 * to undefined behaviour.
169 	 *
170 	 * Since: 2.32
171 	 */
172 	public void init()
173 	{
174 		g_cond_init(gCond);
175 	}
176 
177 	/**
178 	 * If threads are waiting for @cond, at least one of them is unblocked.
179 	 * If no threads are waiting for @cond, this function has no effect.
180 	 * It is good practice to hold the same lock as the waiting thread
181 	 * while calling this function, though not required.
182 	 */
183 	public void signal()
184 	{
185 		g_cond_signal(gCond);
186 	}
187 
188 	/**
189 	 * Atomically releases @mutex and waits until @cond is signalled.
190 	 * When this function returns, @mutex is locked again and owned by the
191 	 * calling thread.
192 	 *
193 	 * When using condition variables, it is possible that a spurious wakeup
194 	 * may occur (ie: g_cond_wait() returns even though g_cond_signal() was
195 	 * not called).  It's also possible that a stolen wakeup may occur.
196 	 * This is when g_cond_signal() is called, but another thread acquires
197 	 * @mutex before this thread and modifies the state of the program in
198 	 * such a way that when g_cond_wait() is able to return, the expected
199 	 * condition is no longer met.
200 	 *
201 	 * For this reason, g_cond_wait() must always be used in a loop.  See
202 	 * the documentation for #GCond for a complete example.
203 	 *
204 	 * Params:
205 	 *     mutex = a #GMutex that is currently locked
206 	 */
207 	public void wait(Mutex mutex)
208 	{
209 		g_cond_wait(gCond, (mutex is null) ? null : mutex.getMutexStruct());
210 	}
211 
212 	/**
213 	 * Waits until either @cond is signalled or @end_time has passed.
214 	 *
215 	 * As with g_cond_wait() it is possible that a spurious or stolen wakeup
216 	 * could occur.  For that reason, waiting on a condition variable should
217 	 * always be in a loop, based on an explicitly-checked predicate.
218 	 *
219 	 * %TRUE is returned if the condition variable was signalled (or in the
220 	 * case of a spurious wakeup).  %FALSE is returned if @end_time has
221 	 * passed.
222 	 *
223 	 * The following code shows how to correctly perform a timed wait on a
224 	 * condition variable (extending the example presented in the
225 	 * documentation for #GCond):
226 	 *
227 	 * |[<!-- language="C" -->
228 	 * gpointer
229 	 * pop_data_timed (void)
230 	 * {
231 	 * gint64 end_time;
232 	 * gpointer data;
233 	 *
234 	 * g_mutex_lock (&data_mutex);
235 	 *
236 	 * end_time = g_get_monotonic_time () + 5 * G_TIME_SPAN_SECOND;
237 	 * while (!current_data)
238 	 * if (!g_cond_wait_until (&data_cond, &data_mutex, end_time))
239 	 * {
240 	 * // timeout has passed.
241 	 * g_mutex_unlock (&data_mutex);
242 	 * return NULL;
243 	 * }
244 	 *
245 	 * // there is data for us
246 	 * data = current_data;
247 	 * current_data = NULL;
248 	 *
249 	 * g_mutex_unlock (&data_mutex);
250 	 *
251 	 * return data;
252 	 * }
253 	 * ]|
254 	 *
255 	 * Notice that the end time is calculated once, before entering the
256 	 * loop and reused.  This is the motivation behind the use of absolute
257 	 * time on this API -- if a relative time of 5 seconds were passed
258 	 * directly to the call and a spurious wakeup occurred, the program would
259 	 * have to start over waiting again (which would lead to a total wait
260 	 * time of more than 5 seconds).
261 	 *
262 	 * Params:
263 	 *     mutex = a #GMutex that is currently locked
264 	 *     endTime = the monotonic time to wait until
265 	 *
266 	 * Returns: %TRUE on a signal, %FALSE on a timeout
267 	 *
268 	 * Since: 2.32
269 	 */
270 	public bool waitUntil(Mutex mutex, long endTime)
271 	{
272 		return g_cond_wait_until(gCond, (mutex is null) ? null : mutex.getMutexStruct(), endTime) != 0;
273 	}
274 }