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