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.AsyncQueue;
26 
27 private import glib.ConstructionException;
28 private import glib.TimeVal;
29 private import glib.c.functions;
30 public  import glib.c.types;
31 private import gtkd.Loader;
32 
33 
34 /**
35  * The GAsyncQueue struct is an opaque data structure which represents
36  * an asynchronous queue. It should only be accessed through the
37  * g_async_queue_* functions.
38  */
39 public class AsyncQueue
40 {
41 	/** the main Gtk struct */
42 	protected GAsyncQueue* gAsyncQueue;
43 	protected bool ownedRef;
44 
45 	/** Get the main Gtk struct */
46 	public GAsyncQueue* getAsyncQueueStruct(bool transferOwnership = false)
47 	{
48 		if (transferOwnership)
49 			ownedRef = false;
50 		return gAsyncQueue;
51 	}
52 
53 	/** the main Gtk struct as a void* */
54 	protected void* getStruct()
55 	{
56 		return cast(void*)gAsyncQueue;
57 	}
58 
59 	/**
60 	 * Sets our main struct and passes it to the parent class.
61 	 */
62 	public this (GAsyncQueue* gAsyncQueue, bool ownedRef = false)
63 	{
64 		this.gAsyncQueue = gAsyncQueue;
65 		this.ownedRef = ownedRef;
66 	}
67 
68 	~this ()
69 	{
70 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
71 			g_async_queue_unref(gAsyncQueue);
72 	}
73 
74 
75 	/**
76 	 * Returns the length of the queue.
77 	 *
78 	 * Actually this function returns the number of data items in
79 	 * the queue minus the number of waiting threads, so a negative
80 	 * value means waiting threads, and a positive value means available
81 	 * entries in the @queue. A return value of 0 could mean n entries
82 	 * in the queue and n threads waiting. This can happen due to locking
83 	 * of the queue or due to scheduling.
84 	 *
85 	 * Returns: the length of the @queue
86 	 */
87 	public int length()
88 	{
89 		return g_async_queue_length(gAsyncQueue);
90 	}
91 
92 	/**
93 	 * Returns the length of the queue.
94 	 *
95 	 * Actually this function returns the number of data items in
96 	 * the queue minus the number of waiting threads, so a negative
97 	 * value means waiting threads, and a positive value means available
98 	 * entries in the @queue. A return value of 0 could mean n entries
99 	 * in the queue and n threads waiting. This can happen due to locking
100 	 * of the queue or due to scheduling.
101 	 *
102 	 * This function must be called while holding the @queue's lock.
103 	 *
104 	 * Returns: the length of the @queue.
105 	 */
106 	public int lengthUnlocked()
107 	{
108 		return g_async_queue_length_unlocked(gAsyncQueue);
109 	}
110 
111 	/**
112 	 * Acquires the @queue's lock. If another thread is already
113 	 * holding the lock, this call will block until the lock
114 	 * becomes available.
115 	 *
116 	 * Call g_async_queue_unlock() to drop the lock again.
117 	 *
118 	 * While holding the lock, you can only call the
119 	 * g_async_queue_*_unlocked() functions on @queue. Otherwise,
120 	 * deadlock may occur.
121 	 */
122 	public void lock()
123 	{
124 		g_async_queue_lock(gAsyncQueue);
125 	}
126 
127 	/**
128 	 * Pops data from the @queue. If @queue is empty, this function
129 	 * blocks until data becomes available.
130 	 *
131 	 * Returns: data from the queue
132 	 */
133 	public void* pop()
134 	{
135 		return g_async_queue_pop(gAsyncQueue);
136 	}
137 
138 	/**
139 	 * Pops data from the @queue. If @queue is empty, this function
140 	 * blocks until data becomes available.
141 	 *
142 	 * This function must be called while holding the @queue's lock.
143 	 *
144 	 * Returns: data from the queue.
145 	 */
146 	public void* popUnlocked()
147 	{
148 		return g_async_queue_pop_unlocked(gAsyncQueue);
149 	}
150 
151 	/**
152 	 * Pushes the @data into the @queue. @data must not be %NULL.
153 	 *
154 	 * Params:
155 	 *     data = @data to push into the @queue
156 	 */
157 	public void push(void* data)
158 	{
159 		g_async_queue_push(gAsyncQueue, data);
160 	}
161 
162 	/**
163 	 * Pushes the @item into the @queue. @item must not be %NULL.
164 	 * In contrast to g_async_queue_push(), this function
165 	 * pushes the new item ahead of the items already in the queue,
166 	 * so that it will be the next one to be popped off the queue.
167 	 *
168 	 * Params:
169 	 *     item = data to push into the @queue
170 	 *
171 	 * Since: 2.46
172 	 */
173 	public void pushFront(void* item)
174 	{
175 		g_async_queue_push_front(gAsyncQueue, item);
176 	}
177 
178 	/**
179 	 * Pushes the @item into the @queue. @item must not be %NULL.
180 	 * In contrast to g_async_queue_push_unlocked(), this function
181 	 * pushes the new item ahead of the items already in the queue,
182 	 * so that it will be the next one to be popped off the queue.
183 	 *
184 	 * This function must be called while holding the @queue's lock.
185 	 *
186 	 * Params:
187 	 *     item = data to push into the @queue
188 	 *
189 	 * Since: 2.46
190 	 */
191 	public void pushFrontUnlocked(void* item)
192 	{
193 		g_async_queue_push_front_unlocked(gAsyncQueue, item);
194 	}
195 
196 	/**
197 	 * Inserts @data into @queue using @func to determine the new
198 	 * position.
199 	 *
200 	 * This function requires that the @queue is sorted before pushing on
201 	 * new elements, see g_async_queue_sort().
202 	 *
203 	 * This function will lock @queue before it sorts the queue and unlock
204 	 * it when it is finished.
205 	 *
206 	 * For an example of @func see g_async_queue_sort().
207 	 *
208 	 * Params:
209 	 *     data = the @data to push into the @queue
210 	 *     func = the #GCompareDataFunc is used to sort @queue
211 	 *     userData = user data passed to @func.
212 	 *
213 	 * Since: 2.10
214 	 */
215 	public void pushSorted(void* data, GCompareDataFunc func, void* userData)
216 	{
217 		g_async_queue_push_sorted(gAsyncQueue, data, func, userData);
218 	}
219 
220 	/**
221 	 * Inserts @data into @queue using @func to determine the new
222 	 * position.
223 	 *
224 	 * The sort function @func is passed two elements of the @queue.
225 	 * It should return 0 if they are equal, a negative value if the
226 	 * first element should be higher in the @queue or a positive value
227 	 * if the first element should be lower in the @queue than the second
228 	 * element.
229 	 *
230 	 * This function requires that the @queue is sorted before pushing on
231 	 * new elements, see g_async_queue_sort().
232 	 *
233 	 * This function must be called while holding the @queue's lock.
234 	 *
235 	 * For an example of @func see g_async_queue_sort().
236 	 *
237 	 * Params:
238 	 *     data = the @data to push into the @queue
239 	 *     func = the #GCompareDataFunc is used to sort @queue
240 	 *     userData = user data passed to @func.
241 	 *
242 	 * Since: 2.10
243 	 */
244 	public void pushSortedUnlocked(void* data, GCompareDataFunc func, void* userData)
245 	{
246 		g_async_queue_push_sorted_unlocked(gAsyncQueue, data, func, userData);
247 	}
248 
249 	/**
250 	 * Pushes the @data into the @queue. @data must not be %NULL.
251 	 *
252 	 * This function must be called while holding the @queue's lock.
253 	 *
254 	 * Params:
255 	 *     data = @data to push into the @queue
256 	 */
257 	public void pushUnlocked(void* data)
258 	{
259 		g_async_queue_push_unlocked(gAsyncQueue, data);
260 	}
261 
262 	alias doref = ref_;
263 	/**
264 	 * Increases the reference count of the asynchronous @queue by 1.
265 	 * You do not need to hold the lock to call this function.
266 	 *
267 	 * Returns: the @queue that was passed in (since 2.6)
268 	 */
269 	public AsyncQueue ref_()
270 	{
271 		auto __p = g_async_queue_ref(gAsyncQueue);
272 
273 		if(__p is null)
274 		{
275 			return null;
276 		}
277 
278 		return new AsyncQueue(cast(GAsyncQueue*) __p);
279 	}
280 
281 	/**
282 	 * Increases the reference count of the asynchronous @queue by 1.
283 	 *
284 	 * Deprecated: Reference counting is done atomically.
285 	 * so g_async_queue_ref() can be used regardless of the @queue's
286 	 * lock.
287 	 */
288 	public void refUnlocked()
289 	{
290 		g_async_queue_ref_unlocked(gAsyncQueue);
291 	}
292 
293 	/**
294 	 * Remove an item from the queue.
295 	 *
296 	 * Params:
297 	 *     item = the data to remove from the @queue
298 	 *
299 	 * Returns: %TRUE if the item was removed
300 	 *
301 	 * Since: 2.46
302 	 */
303 	public bool remove(void* item)
304 	{
305 		return g_async_queue_remove(gAsyncQueue, item) != 0;
306 	}
307 
308 	/**
309 	 * Remove an item from the queue.
310 	 *
311 	 * This function must be called while holding the @queue's lock.
312 	 *
313 	 * Params:
314 	 *     item = the data to remove from the @queue
315 	 *
316 	 * Returns: %TRUE if the item was removed
317 	 *
318 	 * Since: 2.46
319 	 */
320 	public bool removeUnlocked(void* item)
321 	{
322 		return g_async_queue_remove_unlocked(gAsyncQueue, item) != 0;
323 	}
324 
325 	/**
326 	 * Sorts @queue using @func.
327 	 *
328 	 * The sort function @func is passed two elements of the @queue.
329 	 * It should return 0 if they are equal, a negative value if the
330 	 * first element should be higher in the @queue or a positive value
331 	 * if the first element should be lower in the @queue than the second
332 	 * element.
333 	 *
334 	 * This function will lock @queue before it sorts the queue and unlock
335 	 * it when it is finished.
336 	 *
337 	 * If you were sorting a list of priority numbers to make sure the
338 	 * lowest priority would be at the top of the queue, you could use:
339 	 * |[<!-- language="C" -->
340 	 * gint32 id1;
341 	 * gint32 id2;
342 	 *
343 	 * id1 = GPOINTER_TO_INT (element1);
344 	 * id2 = GPOINTER_TO_INT (element2);
345 	 *
346 	 * return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
347 	 * ]|
348 	 *
349 	 * Params:
350 	 *     func = the #GCompareDataFunc is used to sort @queue
351 	 *     userData = user data passed to @func
352 	 *
353 	 * Since: 2.10
354 	 */
355 	public void sort(GCompareDataFunc func, void* userData)
356 	{
357 		g_async_queue_sort(gAsyncQueue, func, userData);
358 	}
359 
360 	/**
361 	 * Sorts @queue using @func.
362 	 *
363 	 * The sort function @func is passed two elements of the @queue.
364 	 * It should return 0 if they are equal, a negative value if the
365 	 * first element should be higher in the @queue or a positive value
366 	 * if the first element should be lower in the @queue than the second
367 	 * element.
368 	 *
369 	 * This function must be called while holding the @queue's lock.
370 	 *
371 	 * Params:
372 	 *     func = the #GCompareDataFunc is used to sort @queue
373 	 *     userData = user data passed to @func
374 	 *
375 	 * Since: 2.10
376 	 */
377 	public void sortUnlocked(GCompareDataFunc func, void* userData)
378 	{
379 		g_async_queue_sort_unlocked(gAsyncQueue, func, userData);
380 	}
381 
382 	/**
383 	 * Pops data from the @queue. If the queue is empty, blocks until
384 	 * @end_time or until data becomes available.
385 	 *
386 	 * If no data is received before @end_time, %NULL is returned.
387 	 *
388 	 * To easily calculate @end_time, a combination of g_get_real_time()
389 	 * and g_time_val_add() can be used.
390 	 *
391 	 * Deprecated: use g_async_queue_timeout_pop().
392 	 *
393 	 * Params:
394 	 *     endTime = a #GTimeVal, determining the final time
395 	 *
396 	 * Returns: data from the queue or %NULL, when no data is
397 	 *     received before @end_time.
398 	 */
399 	public void* timedPop(TimeVal endTime)
400 	{
401 		return g_async_queue_timed_pop(gAsyncQueue, (endTime is null) ? null : endTime.getTimeValStruct());
402 	}
403 
404 	/**
405 	 * Pops data from the @queue. If the queue is empty, blocks until
406 	 * @end_time or until data becomes available.
407 	 *
408 	 * If no data is received before @end_time, %NULL is returned.
409 	 *
410 	 * To easily calculate @end_time, a combination of g_get_real_time()
411 	 * and g_time_val_add() can be used.
412 	 *
413 	 * This function must be called while holding the @queue's lock.
414 	 *
415 	 * Deprecated: use g_async_queue_timeout_pop_unlocked().
416 	 *
417 	 * Params:
418 	 *     endTime = a #GTimeVal, determining the final time
419 	 *
420 	 * Returns: data from the queue or %NULL, when no data is
421 	 *     received before @end_time.
422 	 */
423 	public void* timedPopUnlocked(TimeVal endTime)
424 	{
425 		return g_async_queue_timed_pop_unlocked(gAsyncQueue, (endTime is null) ? null : endTime.getTimeValStruct());
426 	}
427 
428 	/**
429 	 * Pops data from the @queue. If the queue is empty, blocks for
430 	 * @timeout microseconds, or until data becomes available.
431 	 *
432 	 * If no data is received before the timeout, %NULL is returned.
433 	 *
434 	 * Params:
435 	 *     timeout = the number of microseconds to wait
436 	 *
437 	 * Returns: data from the queue or %NULL, when no data is
438 	 *     received before the timeout.
439 	 */
440 	public void* timeoutPop(ulong timeout)
441 	{
442 		return g_async_queue_timeout_pop(gAsyncQueue, timeout);
443 	}
444 
445 	/**
446 	 * Pops data from the @queue. If the queue is empty, blocks for
447 	 * @timeout microseconds, or until data becomes available.
448 	 *
449 	 * If no data is received before the timeout, %NULL is returned.
450 	 *
451 	 * This function must be called while holding the @queue's lock.
452 	 *
453 	 * Params:
454 	 *     timeout = the number of microseconds to wait
455 	 *
456 	 * Returns: data from the queue or %NULL, when no data is
457 	 *     received before the timeout.
458 	 */
459 	public void* timeoutPopUnlocked(ulong timeout)
460 	{
461 		return g_async_queue_timeout_pop_unlocked(gAsyncQueue, timeout);
462 	}
463 
464 	/**
465 	 * Tries to pop data from the @queue. If no data is available,
466 	 * %NULL is returned.
467 	 *
468 	 * Returns: data from the queue or %NULL, when no data is
469 	 *     available immediately.
470 	 */
471 	public void* tryPop()
472 	{
473 		return g_async_queue_try_pop(gAsyncQueue);
474 	}
475 
476 	/**
477 	 * Tries to pop data from the @queue. If no data is available,
478 	 * %NULL is returned.
479 	 *
480 	 * This function must be called while holding the @queue's lock.
481 	 *
482 	 * Returns: data from the queue or %NULL, when no data is
483 	 *     available immediately.
484 	 */
485 	public void* tryPopUnlocked()
486 	{
487 		return g_async_queue_try_pop_unlocked(gAsyncQueue);
488 	}
489 
490 	/**
491 	 * Releases the queue's lock.
492 	 *
493 	 * Calling this function when you have not acquired
494 	 * the with g_async_queue_lock() leads to undefined
495 	 * behaviour.
496 	 */
497 	public void unlock()
498 	{
499 		g_async_queue_unlock(gAsyncQueue);
500 	}
501 
502 	/**
503 	 * Decreases the reference count of the asynchronous @queue by 1.
504 	 *
505 	 * If the reference count went to 0, the @queue will be destroyed
506 	 * and the memory allocated will be freed. So you are not allowed
507 	 * to use the @queue afterwards, as it might have disappeared.
508 	 * You do not need to hold the lock to call this function.
509 	 */
510 	public void unref()
511 	{
512 		g_async_queue_unref(gAsyncQueue);
513 	}
514 
515 	/**
516 	 * Decreases the reference count of the asynchronous @queue by 1
517 	 * and releases the lock. This function must be called while holding
518 	 * the @queue's lock. If the reference count went to 0, the @queue
519 	 * will be destroyed and the memory allocated will be freed.
520 	 *
521 	 * Deprecated: Reference counting is done atomically.
522 	 * so g_async_queue_unref() can be used regardless of the @queue's
523 	 * lock.
524 	 */
525 	public void unrefAndUnlock()
526 	{
527 		g_async_queue_unref_and_unlock(gAsyncQueue);
528 	}
529 
530 	/**
531 	 * Creates a new asynchronous queue.
532 	 *
533 	 * Returns: a new #GAsyncQueue. Free with g_async_queue_unref()
534 	 *
535 	 * Throws: ConstructionException GTK+ fails to create the object.
536 	 */
537 	public this()
538 	{
539 		auto __p = g_async_queue_new();
540 
541 		if(__p is null)
542 		{
543 			throw new ConstructionException("null returned by new");
544 		}
545 
546 		this(cast(GAsyncQueue*) __p);
547 	}
548 
549 	/**
550 	 * Creates a new asynchronous queue and sets up a destroy notify
551 	 * function that is used to free any remaining queue items when
552 	 * the queue is destroyed after the final unref.
553 	 *
554 	 * Params:
555 	 *     itemFreeFunc = function to free queue elements
556 	 *
557 	 * Returns: a new #GAsyncQueue. Free with g_async_queue_unref()
558 	 *
559 	 * Since: 2.16
560 	 *
561 	 * Throws: ConstructionException GTK+ fails to create the object.
562 	 */
563 	public this(GDestroyNotify itemFreeFunc)
564 	{
565 		auto __p = g_async_queue_new_full(itemFreeFunc);
566 
567 		if(__p is null)
568 		{
569 			throw new ConstructionException("null returned by new_full");
570 		}
571 
572 		this(cast(GAsyncQueue*) __p);
573 	}
574 }