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  = glib-Asynchronous-Queues.html
27  * outPack = glib
28  * outFile = AsyncQueue
29  * strct   = GAsyncQueue
30  * realStrct=
31  * ctorStrct=
32  * clss    = AsyncQueue
33  * interf  = 
34  * class Code: No
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_async_queue_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * structWrap:
47  * 	- GAsyncQueue* -> AsyncQueue
48  * module aliases:
49  * local aliases:
50  * overrides:
51  */
52 
53 module glib.AsyncQueue;
54 
55 public  import gtkc.glibtypes;
56 
57 private import gtkc.glib;
58 private import glib.ConstructionException;
59 
60 
61 
62 
63 
64 
65 /**
66  * Often you need to communicate between different threads. In general
67  * it's safer not to do this by shared memory, but by explicit message
68  * passing. These messages only make sense asynchronously for
69  * multi-threaded applications though, as a synchronous operation could
70  * as well be done in the same thread.
71  *
72  * Asynchronous queues are an exception from most other GLib data
73  * structures, as they can be used simultaneously from multiple threads
74  * without explicit locking and they bring their own builtin reference
75  * counting. This is because the nature of an asynchronous queue is that
76  * it will always be used by at least 2 concurrent threads.
77  *
78  * For using an asynchronous queue you first have to create one with
79  * g_async_queue_new(). GAsyncQueue structs are reference counted,
80  * use g_async_queue_ref() and g_async_queue_unref() to manage your
81  * references.
82  *
83  * A thread which wants to send a message to that queue simply calls
84  * g_async_queue_push() to push the message to the queue.
85  *
86  * A thread which is expecting messages from an asynchronous queue
87  * simply calls g_async_queue_pop() for that queue. If no message is
88  * available in the queue at that point, the thread is now put to sleep
89  * until a message arrives. The message will be removed from the queue
90  * and returned. The functions g_async_queue_try_pop() and
91  * g_async_queue_timeout_pop() can be used to only check for the presence
92  * of messages or to only wait a certain time for messages respectively.
93  *
94  * For almost every function there exist two variants, one that locks
95  * the queue and one that doesn't. That way you can hold the queue lock
96  * (acquire it with g_async_queue_lock() and release it with
97  * g_async_queue_unlock()) over multiple queue accessing instructions.
98  * This can be necessary to ensure the integrity of the queue, but should
99  * only be used when really necessary, as it can make your life harder
100  * if used unwisely. Normally you should only use the locking function
101  * variants (those without the _unlocked suffix).
102  *
103  * In many cases, it may be more convenient to use GThreadPool when
104  * you need to distribute work to a set of worker threads instead of
105  * using GAsyncQueue manually. GThreadPool uses a GAsyncQueue
106  * internally.
107  */
108 public class AsyncQueue
109 {
110 	
111 	/** the main Gtk struct */
112 	protected GAsyncQueue* gAsyncQueue;
113 	
114 	
115 	public GAsyncQueue* getAsyncQueueStruct()
116 	{
117 		return gAsyncQueue;
118 	}
119 	
120 	
121 	/** the main Gtk struct as a void* */
122 	protected void* getStruct()
123 	{
124 		return cast(void*)gAsyncQueue;
125 	}
126 	
127 	/**
128 	 * Sets our main struct and passes it to the parent class
129 	 */
130 	public this (GAsyncQueue* gAsyncQueue)
131 	{
132 		this.gAsyncQueue = gAsyncQueue;
133 	}
134 	
135 	/**
136 	 */
137 	
138 	/**
139 	 * Creates a new asynchronous queue.
140 	 * Throws: ConstructionException GTK+ fails to create the object.
141 	 */
142 	public this ()
143 	{
144 		// GAsyncQueue * g_async_queue_new (void);
145 		auto p = g_async_queue_new();
146 		if(p is null)
147 		{
148 			throw new ConstructionException("null returned by g_async_queue_new()");
149 		}
150 		this(cast(GAsyncQueue*) p);
151 	}
152 	
153 	/**
154 	 * Creates a new asynchronous queue and sets up a destroy notify
155 	 * function that is used to free any remaining queue items when
156 	 * the queue is destroyed after the final unref.
157 	 * Since 2.16
158 	 * Params:
159 	 * itemFreeFunc = function to free queue elements
160 	 * Throws: ConstructionException GTK+ fails to create the object.
161 	 */
162 	public this (GDestroyNotify itemFreeFunc)
163 	{
164 		// GAsyncQueue * g_async_queue_new_full (GDestroyNotify item_free_func);
165 		auto p = g_async_queue_new_full(itemFreeFunc);
166 		if(p is null)
167 		{
168 			throw new ConstructionException("null returned by g_async_queue_new_full(itemFreeFunc)");
169 		}
170 		this(cast(GAsyncQueue*) p);
171 	}
172 	
173 	/**
174 	 * Increases the reference count of the asynchronous queue by 1.
175 	 * You do not need to hold the lock to call this function.
176 	 * Returns: the queue that was passed in (since 2.6)
177 	 */
178 	public AsyncQueue doref()
179 	{
180 		// GAsyncQueue * g_async_queue_ref (GAsyncQueue *queue);
181 		auto p = g_async_queue_ref(gAsyncQueue);
182 		
183 		if(p is null)
184 		{
185 			return null;
186 		}
187 		
188 		return new AsyncQueue(cast(GAsyncQueue*) p);
189 	}
190 	
191 	/**
192 	 * Decreases the reference count of the asynchronous queue by 1.
193 	 * If the reference count went to 0, the queue will be destroyed
194 	 * and the memory allocated will be freed. So you are not allowed
195 	 * to use the queue afterwards, as it might have disappeared.
196 	 * You do not need to hold the lock to call this function.
197 	 */
198 	public void unref()
199 	{
200 		// void g_async_queue_unref (GAsyncQueue *queue);
201 		g_async_queue_unref(gAsyncQueue);
202 	}
203 	
204 	/**
205 	 * Pushes the data into the queue. data must not be NULL.
206 	 * Params:
207 	 * data = data to push into the queue
208 	 */
209 	public void push(void* data)
210 	{
211 		// void g_async_queue_push (GAsyncQueue *queue,  gpointer data);
212 		g_async_queue_push(gAsyncQueue, data);
213 	}
214 	
215 	/**
216 	 * Inserts data into queue using func to determine the new
217 	 * position.
218 	 * This function requires that the queue is sorted before pushing on
219 	 * new elements, see g_async_queue_sort().
220 	 * This function will lock queue before it sorts the queue and unlock
221 	 * it when it is finished.
222 	 * For an example of func see g_async_queue_sort().
223 	 * Since 2.10
224 	 * Params:
225 	 * data = the data to push into the queue
226 	 * func = the GCompareDataFunc is used to sort queue
227 	 * userData = user data passed to func.
228 	 */
229 	public void pushSorted(void* data, GCompareDataFunc func, void* userData)
230 	{
231 		// void g_async_queue_push_sorted (GAsyncQueue *queue,  gpointer data,  GCompareDataFunc func,  gpointer user_data);
232 		g_async_queue_push_sorted(gAsyncQueue, data, func, userData);
233 	}
234 	
235 	/**
236 	 * Pops data from the queue. If queue is empty, this function
237 	 * blocks until data becomes available.
238 	 * Returns: data from the queue
239 	 */
240 	public void* pop()
241 	{
242 		// gpointer g_async_queue_pop (GAsyncQueue *queue);
243 		return g_async_queue_pop(gAsyncQueue);
244 	}
245 	
246 	/**
247 	 * Tries to pop data from the queue. If no data is available,
248 	 * NULL is returned.
249 	 * Returns: data from the queue or NULL, when no data is available immediately.
250 	 */
251 	public void* tryPop()
252 	{
253 		// gpointer g_async_queue_try_pop (GAsyncQueue *queue);
254 		return g_async_queue_try_pop(gAsyncQueue);
255 	}
256 	
257 	/**
258 	 * Pops data from the queue. If the queue is empty, blocks for
259 	 * timeout microseconds, or until data becomes available.
260 	 * If no data is received before the timeout, NULL is returned.
261 	 * Params:
262 	 * timeout = the number of microseconds to wait
263 	 * Returns: data from the queue or NULL, when no data is received before the timeout.
264 	 */
265 	public void* timeoutPop(ulong timeout)
266 	{
267 		// gpointer g_async_queue_timeout_pop (GAsyncQueue *queue,  guint64 timeout);
268 		return g_async_queue_timeout_pop(gAsyncQueue, timeout);
269 	}
270 	
271 	/**
272 	 * Returns the length of the queue.
273 	 * Actually this function returns the number of data items in
274 	 * the queue minus the number of waiting threads, so a negative
275 	 * value means waiting threads, and a positive value means available
276 	 * entries in the queue. A return value of 0 could mean n entries
277 	 * in the queue and n threads waiting. This can happen due to locking
278 	 * of the queue or due to scheduling.
279 	 * Returns: the length of the queue
280 	 */
281 	public int length()
282 	{
283 		// gint g_async_queue_length (GAsyncQueue *queue);
284 		return g_async_queue_length(gAsyncQueue);
285 	}
286 	
287 	/**
288 	 * Sorts queue using func.
289 	 * The sort function func is passed two elements of the queue.
290 	 * It should return 0 if they are equal, a negative value if the
291 	 * first element should be higher in the queue or a positive value
292 	 * if the first element should be lower in the queue than the second
293 	 * element.
294 	 * This function will lock queue before it sorts the queue and unlock
295 	 * it when it is finished.
296 	 * If you were sorting a list of priority numbers to make sure the
297 	 * Since 2.10
298 	 * Params:
299 	 * func = the GCompareDataFunc is used to sort queue
300 	 * userData = user data passed to func
301 	 */
302 	public void sort(GCompareDataFunc func, void* userData)
303 	{
304 		// void g_async_queue_sort (GAsyncQueue *queue,  GCompareDataFunc func,  gpointer user_data);
305 		g_async_queue_sort(gAsyncQueue, func, userData);
306 	}
307 	
308 	/**
309 	 * Acquires the queue's lock. If another thread is already
310 	 * holding the lock, this call will block until the lock
311 	 * becomes available.
312 	 * Call g_async_queue_unlock() to drop the lock again.
313 	 * While holding the lock, you can only call the
314 	 * g_async_queue_*_unlocked() functions
315 	 * on queue. Otherwise, deadlock may occur.
316 	 */
317 	public void lock()
318 	{
319 		// void g_async_queue_lock (GAsyncQueue *queue);
320 		g_async_queue_lock(gAsyncQueue);
321 	}
322 	
323 	/**
324 	 * Releases the queue's lock.
325 	 * Calling this function when you have not acquired
326 	 * the with g_async_queue_lock() leads to undefined
327 	 * behaviour.
328 	 */
329 	public void unlock()
330 	{
331 		// void g_async_queue_unlock (GAsyncQueue *queue);
332 		g_async_queue_unlock(gAsyncQueue);
333 	}
334 	
335 	/**
336 	 * Warning
337 	 * g_async_queue_ref_unlocked has been deprecated since version 2.8 and should not be used in newly-written code. Reference counting is done atomically.
338 	 * so g_async_queue_ref() can be used regardless of the queue's
339 	 * lock.
340 	 * Increases the reference count of the asynchronous queue by 1.
341 	 */
342 	public void refUnlocked()
343 	{
344 		// void g_async_queue_ref_unlocked (GAsyncQueue *queue);
345 		g_async_queue_ref_unlocked(gAsyncQueue);
346 	}
347 	
348 	/**
349 	 * Warning
350 	 * g_async_queue_unref_and_unlock has been deprecated since version 2.8 and should not be used in newly-written code. Reference counting is done atomically.
351 	 * so g_async_queue_unref() can be used regardless of the queue's
352 	 * lock.
353 	 * Decreases the reference count of the asynchronous queue by 1
354 	 * and releases the lock. This function must be called while holding
355 	 * the queue's lock. If the reference count went to 0, the queue
356 	 * will be destroyed and the memory allocated will be freed.
357 	 */
358 	public void unrefAndUnlock()
359 	{
360 		// void g_async_queue_unref_and_unlock (GAsyncQueue *queue);
361 		g_async_queue_unref_and_unlock(gAsyncQueue);
362 	}
363 	
364 	/**
365 	 * Pushes the data into the queue. data must not be NULL.
366 	 * This function must be called while holding the queue's lock.
367 	 * Params:
368 	 * data = data to push into the queue
369 	 */
370 	public void pushUnlocked(void* data)
371 	{
372 		// void g_async_queue_push_unlocked (GAsyncQueue *queue,  gpointer data);
373 		g_async_queue_push_unlocked(gAsyncQueue, data);
374 	}
375 	
376 	/**
377 	 * Inserts data into queue using func to determine the new
378 	 * position.
379 	 * The sort function func is passed two elements of the queue.
380 	 * It should return 0 if they are equal, a negative value if the
381 	 * first element should be higher in the queue or a positive value
382 	 * if the first element should be lower in the queue than the second
383 	 * element.
384 	 * This function requires that the queue is sorted before pushing on
385 	 * new elements, see g_async_queue_sort().
386 	 * This function must be called while holding the queue's lock.
387 	 * For an example of func see g_async_queue_sort().
388 	 * Since 2.10
389 	 * Params:
390 	 * data = the data to push into the queue
391 	 * func = the GCompareDataFunc is used to sort queue
392 	 * userData = user data passed to func.
393 	 */
394 	public void pushSortedUnlocked(void* data, GCompareDataFunc func, void* userData)
395 	{
396 		// void g_async_queue_push_sorted_unlocked (GAsyncQueue *queue,  gpointer data,  GCompareDataFunc func,  gpointer user_data);
397 		g_async_queue_push_sorted_unlocked(gAsyncQueue, data, func, userData);
398 	}
399 	
400 	/**
401 	 * Pops data from the queue. If queue is empty, this function
402 	 * blocks until data becomes available.
403 	 * This function must be called while holding the queue's lock.
404 	 * Returns: data from the queue.
405 	 */
406 	public void* popUnlocked()
407 	{
408 		// gpointer g_async_queue_pop_unlocked (GAsyncQueue *queue);
409 		return g_async_queue_pop_unlocked(gAsyncQueue);
410 	}
411 	
412 	/**
413 	 * Tries to pop data from the queue. If no data is available,
414 	 * NULL is returned.
415 	 * This function must be called while holding the queue's lock.
416 	 * Returns: data from the queue or NULL, when no data is available immediately.
417 	 */
418 	public void* tryPopUnlocked()
419 	{
420 		// gpointer g_async_queue_try_pop_unlocked (GAsyncQueue *queue);
421 		return g_async_queue_try_pop_unlocked(gAsyncQueue);
422 	}
423 	
424 	/**
425 	 * Pops data from the queue. If the queue is empty, blocks for
426 	 * timeout microseconds, or until data becomes available.
427 	 * If no data is received before the timeout, NULL is returned.
428 	 * This function must be called while holding the queue's lock.
429 	 * Params:
430 	 * timeout = the number of microseconds to wait
431 	 * Returns: data from the queue or NULL, when no data is received before the timeout.
432 	 */
433 	public void* timeoutPopUnlocked(ulong timeout)
434 	{
435 		// gpointer g_async_queue_timeout_pop_unlocked (GAsyncQueue *queue,  guint64 timeout);
436 		return g_async_queue_timeout_pop_unlocked(gAsyncQueue, timeout);
437 	}
438 	
439 	/**
440 	 * Returns the length of the queue.
441 	 * Actually this function returns the number of data items in
442 	 * the queue minus the number of waiting threads, so a negative
443 	 * value means waiting threads, and a positive value means available
444 	 * entries in the queue. A return value of 0 could mean n entries
445 	 * in the queue and n threads waiting. This can happen due to locking
446 	 * of the queue or due to scheduling.
447 	 * This function must be called while holding the queue's lock.
448 	 * Returns: the length of the queue.
449 	 */
450 	public int lengthUnlocked()
451 	{
452 		// gint g_async_queue_length_unlocked (GAsyncQueue *queue);
453 		return g_async_queue_length_unlocked(gAsyncQueue);
454 	}
455 	
456 	/**
457 	 * Sorts queue using func.
458 	 * The sort function func is passed two elements of the queue.
459 	 * It should return 0 if they are equal, a negative value if the
460 	 * first element should be higher in the queue or a positive value
461 	 * if the first element should be lower in the queue than the second
462 	 * element.
463 	 * This function must be called while holding the queue's lock.
464 	 * Since 2.10
465 	 * Params:
466 	 * func = the GCompareDataFunc is used to sort queue
467 	 * userData = user data passed to func
468 	 */
469 	public void sortUnlocked(GCompareDataFunc func, void* userData)
470 	{
471 		// void g_async_queue_sort_unlocked (GAsyncQueue *queue,  GCompareDataFunc func,  gpointer user_data);
472 		g_async_queue_sort_unlocked(gAsyncQueue, func, userData);
473 	}
474 	
475 	/**
476 	 * Warning
477 	 * g_async_queue_timed_pop is deprecated and should not be used in newly-written code. use g_async_queue_timeout_pop().
478 	 * Pops data from the queue. If the queue is empty, blocks until
479 	 * end_time or until data becomes available.
480 	 * If no data is received before end_time, NULL is returned.
481 	 * To easily calculate end_time, a combination of g_get_current_time()
482 	 * and g_time_val_add() can be used.
483 	 * Params:
484 	 * endTime = a GTimeVal, determining the final time
485 	 * Returns: data from the queue or NULL, when no data is received before end_time.
486 	 */
487 	public void* timedPop(ref GTimeVal endTime)
488 	{
489 		// gpointer g_async_queue_timed_pop (GAsyncQueue *queue,  GTimeVal *end_time);
490 		return g_async_queue_timed_pop(gAsyncQueue, &endTime);
491 	}
492 	
493 	/**
494 	 * Warning
495 	 * g_async_queue_timed_pop_unlocked is deprecated and should not be used in newly-written code. use g_async_queue_timeout_pop_unlocked().
496 	 * Pops data from the queue. If the queue is empty, blocks until
497 	 * end_time or until data becomes available.
498 	 * If no data is received before end_time, NULL is returned.
499 	 * To easily calculate end_time, a combination of g_get_current_time()
500 	 * and g_time_val_add() can be used.
501 	 * This function must be called while holding the queue's lock.
502 	 * Params:
503 	 * endTime = a GTimeVal, determining the final time
504 	 * Returns: data from the queue or NULL, when no data is received before end_time.
505 	 */
506 	public void* timedPopUnlocked(ref GTimeVal endTime)
507 	{
508 		// gpointer g_async_queue_timed_pop_unlocked (GAsyncQueue *queue,  GTimeVal *end_time);
509 		return g_async_queue_timed_pop_unlocked(gAsyncQueue, &endTime);
510 	}
511 }