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