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