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 gtkc.glib;
30 public  import gtkc.glibtypes;
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 	/**
263 	 * Increases the reference count of the asynchronous @queue by 1.
264 	 * You do not need to hold the lock to call this function.
265 	 *
266 	 * Returns: the @queue that was passed in (since 2.6)
267 	 */
268 	public AsyncQueue doref()
269 	{
270 		auto p = g_async_queue_ref(gAsyncQueue);
271 		
272 		if(p is null)
273 		{
274 			return null;
275 		}
276 		
277 		return new AsyncQueue(cast(GAsyncQueue*) p);
278 	}
279 
280 	/**
281 	 * Increases the reference count of the asynchronous @queue by 1.
282 	 *
283 	 * Deprecated: Reference counting is done atomically.
284 	 * so g_async_queue_ref() can be used regardless of the @queue's
285 	 * lock.
286 	 */
287 	public void refUnlocked()
288 	{
289 		g_async_queue_ref_unlocked(gAsyncQueue);
290 	}
291 
292 	/**
293 	 * Remove an item from the queue.
294 	 *
295 	 * Params:
296 	 *     item = the data to remove from the @queue
297 	 *
298 	 * Returns: %TRUE if the item was removed
299 	 *
300 	 * Since: 2.46
301 	 */
302 	public bool remove(void* item)
303 	{
304 		return g_async_queue_remove(gAsyncQueue, item) != 0;
305 	}
306 
307 	/**
308 	 * Remove an item from the queue.
309 	 *
310 	 * This function must be called while holding the @queue's lock.
311 	 *
312 	 * Params:
313 	 *     item = the data to remove from the @queue
314 	 *
315 	 * Returns: %TRUE if the item was removed
316 	 *
317 	 * Since: 2.46
318 	 */
319 	public bool removeUnlocked(void* item)
320 	{
321 		return g_async_queue_remove_unlocked(gAsyncQueue, item) != 0;
322 	}
323 
324 	/**
325 	 * Sorts @queue using @func.
326 	 *
327 	 * The sort function @func is passed two elements of the @queue.
328 	 * It should return 0 if they are equal, a negative value if the
329 	 * first element should be higher in the @queue or a positive value
330 	 * if the first element should be lower in the @queue than the second
331 	 * element.
332 	 *
333 	 * This function will lock @queue before it sorts the queue and unlock
334 	 * it when it is finished.
335 	 *
336 	 * If you were sorting a list of priority numbers to make sure the
337 	 * lowest priority would be at the top of the queue, you could use:
338 	 * |[<!-- language="C" -->
339 	 * gint32 id1;
340 	 * gint32 id2;
341 	 *
342 	 * id1 = GPOINTER_TO_INT (element1);
343 	 * id2 = GPOINTER_TO_INT (element2);
344 	 *
345 	 * return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
346 	 * ]|
347 	 *
348 	 * Params:
349 	 *     func = the #GCompareDataFunc is used to sort @queue
350 	 *     userData = user data passed to @func
351 	 *
352 	 * Since: 2.10
353 	 */
354 	public void sort(GCompareDataFunc func, void* userData)
355 	{
356 		g_async_queue_sort(gAsyncQueue, func, userData);
357 	}
358 
359 	/**
360 	 * Sorts @queue using @func.
361 	 *
362 	 * The sort function @func is passed two elements of the @queue.
363 	 * It should return 0 if they are equal, a negative value if the
364 	 * first element should be higher in the @queue or a positive value
365 	 * if the first element should be lower in the @queue than the second
366 	 * element.
367 	 *
368 	 * This function must be called while holding the @queue's lock.
369 	 *
370 	 * Params:
371 	 *     func = the #GCompareDataFunc is used to sort @queue
372 	 *     userData = user data passed to @func
373 	 *
374 	 * Since: 2.10
375 	 */
376 	public void sortUnlocked(GCompareDataFunc func, void* userData)
377 	{
378 		g_async_queue_sort_unlocked(gAsyncQueue, func, userData);
379 	}
380 
381 	/**
382 	 * Pops data from the @queue. If the queue is empty, blocks until
383 	 * @end_time or until data becomes available.
384 	 *
385 	 * If no data is received before @end_time, %NULL is returned.
386 	 *
387 	 * To easily calculate @end_time, a combination of g_get_current_time()
388 	 * and g_time_val_add() can be used.
389 	 *
390 	 * Deprecated: use g_async_queue_timeout_pop().
391 	 *
392 	 * Params:
393 	 *     endTime = a #GTimeVal, determining the final time
394 	 *
395 	 * Returns: data from the queue or %NULL, when no data is
396 	 *     received before @end_time.
397 	 */
398 	public void* timedPop(TimeVal endTime)
399 	{
400 		return g_async_queue_timed_pop(gAsyncQueue, (endTime is null) ? null : endTime.getTimeValStruct());
401 	}
402 
403 	/**
404 	 * Pops data from the @queue. If the queue is empty, blocks until
405 	 * @end_time or until data becomes available.
406 	 *
407 	 * If no data is received before @end_time, %NULL is returned.
408 	 *
409 	 * To easily calculate @end_time, a combination of g_get_current_time()
410 	 * and g_time_val_add() can be used.
411 	 *
412 	 * This function must be called while holding the @queue's lock.
413 	 *
414 	 * Deprecated: use g_async_queue_timeout_pop_unlocked().
415 	 *
416 	 * Params:
417 	 *     endTime = a #GTimeVal, determining the final time
418 	 *
419 	 * Returns: data from the queue or %NULL, when no data is
420 	 *     received before @end_time.
421 	 */
422 	public void* timedPopUnlocked(TimeVal endTime)
423 	{
424 		return g_async_queue_timed_pop_unlocked(gAsyncQueue, (endTime is null) ? null : endTime.getTimeValStruct());
425 	}
426 
427 	/**
428 	 * Pops data from the @queue. If the queue is empty, blocks for
429 	 * @timeout microseconds, or until data becomes available.
430 	 *
431 	 * If no data is received before the timeout, %NULL is returned.
432 	 *
433 	 * Params:
434 	 *     timeout = the number of microseconds to wait
435 	 *
436 	 * Returns: data from the queue or %NULL, when no data is
437 	 *     received before the timeout.
438 	 */
439 	public void* timeoutPop(ulong timeout)
440 	{
441 		return g_async_queue_timeout_pop(gAsyncQueue, timeout);
442 	}
443 
444 	/**
445 	 * Pops data from the @queue. If the queue is empty, blocks for
446 	 * @timeout microseconds, or until data becomes available.
447 	 *
448 	 * If no data is received before the timeout, %NULL is returned.
449 	 *
450 	 * This function must be called while holding the @queue's lock.
451 	 *
452 	 * Params:
453 	 *     timeout = the number of microseconds to wait
454 	 *
455 	 * Returns: data from the queue or %NULL, when no data is
456 	 *     received before the timeout.
457 	 */
458 	public void* timeoutPopUnlocked(ulong timeout)
459 	{
460 		return g_async_queue_timeout_pop_unlocked(gAsyncQueue, timeout);
461 	}
462 
463 	/**
464 	 * Tries to pop data from the @queue. If no data is available,
465 	 * %NULL is returned.
466 	 *
467 	 * Returns: data from the queue or %NULL, when no data is
468 	 *     available immediately.
469 	 */
470 	public void* tryPop()
471 	{
472 		return g_async_queue_try_pop(gAsyncQueue);
473 	}
474 
475 	/**
476 	 * Tries to pop data from the @queue. If no data is available,
477 	 * %NULL is returned.
478 	 *
479 	 * This function must be called while holding the @queue's lock.
480 	 *
481 	 * Returns: data from the queue or %NULL, when no data is
482 	 *     available immediately.
483 	 */
484 	public void* tryPopUnlocked()
485 	{
486 		return g_async_queue_try_pop_unlocked(gAsyncQueue);
487 	}
488 
489 	/**
490 	 * Releases the queue's lock.
491 	 *
492 	 * Calling this function when you have not acquired
493 	 * the with g_async_queue_lock() leads to undefined
494 	 * behaviour.
495 	 */
496 	public void unlock()
497 	{
498 		g_async_queue_unlock(gAsyncQueue);
499 	}
500 
501 	/**
502 	 * Decreases the reference count of the asynchronous @queue by 1.
503 	 *
504 	 * If the reference count went to 0, the @queue will be destroyed
505 	 * and the memory allocated will be freed. So you are not allowed
506 	 * to use the @queue afterwards, as it might have disappeared.
507 	 * You do not need to hold the lock to call this function.
508 	 */
509 	public void unref()
510 	{
511 		g_async_queue_unref(gAsyncQueue);
512 	}
513 
514 	/**
515 	 * Decreases the reference count of the asynchronous @queue by 1
516 	 * and releases the lock. This function must be called while holding
517 	 * the @queue's lock. If the reference count went to 0, the @queue
518 	 * will be destroyed and the memory allocated will be freed.
519 	 *
520 	 * Deprecated: Reference counting is done atomically.
521 	 * so g_async_queue_unref() can be used regardless of the @queue's
522 	 * lock.
523 	 */
524 	public void unrefAndUnlock()
525 	{
526 		g_async_queue_unref_and_unlock(gAsyncQueue);
527 	}
528 
529 	/**
530 	 * Creates a new asynchronous queue.
531 	 *
532 	 * Returns: a new #GAsyncQueue. Free with g_async_queue_unref()
533 	 *
534 	 * Throws: ConstructionException GTK+ fails to create the object.
535 	 */
536 	public this()
537 	{
538 		auto p = g_async_queue_new();
539 		
540 		if(p is null)
541 		{
542 			throw new ConstructionException("null returned by new");
543 		}
544 		
545 		this(cast(GAsyncQueue*) p);
546 	}
547 
548 	/**
549 	 * Creates a new asynchronous queue and sets up a destroy notify
550 	 * function that is used to free any remaining queue items when
551 	 * the queue is destroyed after the final unref.
552 	 *
553 	 * Params:
554 	 *     itemFreeFunc = function to free queue elements
555 	 *
556 	 * Returns: a new #GAsyncQueue. Free with g_async_queue_unref()
557 	 *
558 	 * Since: 2.16
559 	 *
560 	 * Throws: ConstructionException GTK+ fails to create the object.
561 	 */
562 	public this(GDestroyNotify itemFreeFunc)
563 	{
564 		auto p = g_async_queue_new_full(itemFreeFunc);
565 		
566 		if(p is null)
567 		{
568 			throw new ConstructionException("null returned by new_full");
569 		}
570 		
571 		this(cast(GAsyncQueue*) p);
572 	}
573 }