/src/wrappers/glib/library/core/glib_asynchronous_queues.e
Specman e | 189 lines | 2 code | 55 blank | 132 comment | 0 complexity | 03a88520956b4e21d1c0116606d99030 MD5 | raw file
1class GLIB_ASYNCHRONOUS_QUEUES 2-- Prev Up Home GLib Reference Manual Next 3-- Top | Description 4-- Asynchronous Queues 5 6-- Asynchronous Queues %G รข€”%@ asynchronous communication between threads. 7 8-- Synopsis 9 10-- #include <glib.h> 11 12 13-- GAsyncQueue; 14-- GAsyncQueue* g_async_queue_new (void); 15-- GAsyncQueue* g_async_queue_ref (GAsyncQueue *queue); 16-- void g_async_queue_unref (GAsyncQueue *queue); 17-- void g_async_queue_push (GAsyncQueue *queue, 18-- gpointer data); 19-- gpointer g_async_queue_pop (GAsyncQueue *queue); 20-- gpointer g_async_queue_try_pop (GAsyncQueue *queue); 21-- gpointer g_async_queue_timed_pop (GAsyncQueue *queue, 22-- GTimeVal *end_time); 23-- gint g_async_queue_length (GAsyncQueue *queue); 24 25-- void g_async_queue_lock (GAsyncQueue *queue); 26-- void g_async_queue_unlock (GAsyncQueue *queue); 27-- void g_async_queue_ref_unlocked (GAsyncQueue *queue); 28-- void g_async_queue_unref_and_unlock (GAsyncQueue *queue); 29-- void g_async_queue_push_unlocked (GAsyncQueue *queue, 30-- gpointer data); 31-- gpointer g_async_queue_pop_unlocked (GAsyncQueue *queue); 32-- gpointer g_async_queue_try_pop_unlocked (GAsyncQueue *queue); 33-- gpointer g_async_queue_timed_pop_unlocked 34-- (GAsyncQueue *queue, 35-- GTimeVal *end_time); 36-- gint g_async_queue_length_unlocked (GAsyncQueue *queue); 37 38-- Description 39 40-- Often you need to communicate between different threads. In general it's safer not to do this by shared memory, but by explicit message passing. These messages only make sense asynchronously for multi-threaded applications though, as a synchronous operation could as well be done in the same thread. 41 42-- Asynchronous queues are an exception from most other GLib data structures, as they can be used simultaneously from multiple threads without explicit locking and they bring their own builtin reference counting. This is because the nature of an asynchronous queue is that it will always be used by at least 2 concurrent threads. 43 44-- For using an asynchronous queue you first have to create one with g_async_queue_new(). A newly-created queue will get the reference count 1. Whenever another thread is creating a new reference of (that is, pointer to) the queue, it has to increase the reference count (using g_async_queue_ref()). Also, before removing this reference, the reference count has to be decreased (using g_async_queue_unref()). After that the queue might no longer exist so you must not access it after that point. 45 46-- A thread, which wants to send a message to that queue simply calls g_async_queue_push() to push the message to the queue. 47 48-- A thread, which is expecting messages from an asynchronous queue simply calls g_async_queue_pop() for that queue. If no message is available in the queue at that point, the thread is now put to sleep until a message arrives. The message will be removed from the queue and returned. The functions g_async_queue_try_pop() and g_async_queue_timed_pop() can be used to only check for the presence of messages or to only wait a certain time for messages respectively. 49 50-- For almost every function there exist two variants, one that locks the queue and one that doesn't. That way you can hold the queue lock (acquire it with g_async_queue_lock() and release it with g_async_queue_unlock()) over multiple queue accessing instructions. This can be necessary to ensure the integrity of the queue, but should only be used when really necessary, as it can make your life harder if used unwisely. Normally you should only use the locking function variants (those without the suffix _unlocked) 51-- Details 52-- GAsyncQueue 53 54-- typedef struct _GAsyncQueue GAsyncQueue; 55 56-- The GAsyncQueue struct is an opaque data structure, which represents an asynchronous queue. It should only be accessed through the g_async_queue_* functions. 57-- g_async_queue_new () 58 59-- GAsyncQueue* g_async_queue_new (void); 60 61-- Creates a new asynchronous queue with the initial reference count of 1. 62-- Returns : the new GAsyncQueue. 63-- g_async_queue_ref () 64 65-- GAsyncQueue* g_async_queue_ref (GAsyncQueue *queue); 66 67-- Increases the reference count of the asynchronous queue by 1. You do not need to hold the lock to call this function. 68-- queue : a GAsyncQueue. 69-- Returns : the queue that was passed in (since 2.6) 70-- g_async_queue_unref () 71 72-- void g_async_queue_unref (GAsyncQueue *queue); 73 74-- Decreases the reference count of the asynchronous queue by 1. If the reference count went to 0, the queue will be destroyed and the memory allocated will be freed. So you are not allowed to use the queue afterwards, as it might have disappeared. You do not need to hold the lock to call this function. 75-- queue : a GAsyncQueue. 76-- g_async_queue_push () 77 78-- void g_async_queue_push (GAsyncQueue *queue, 79-- gpointer data); 80 81-- Pushes the data into the queue. data must not be NULL. 82-- queue : a GAsyncQueue. 83-- data : data to push into the queue. 84-- g_async_queue_pop () 85 86-- gpointer g_async_queue_pop (GAsyncQueue *queue); 87 88-- Pops data from the queue. This function blocks until data become available. 89-- queue : a GAsyncQueue. 90-- Returns : data from the queue. 91-- g_async_queue_try_pop () 92 93-- gpointer g_async_queue_try_pop (GAsyncQueue *queue); 94 95-- Tries to pop data from the queue. If no data is available, NULL is returned. 96-- queue : a GAsyncQueue. 97-- Returns : data from the queue or NULL, when no data is available immediately. 98-- g_async_queue_timed_pop () 99 100-- gpointer g_async_queue_timed_pop (GAsyncQueue *queue, 101-- GTimeVal *end_time); 102 103-- Pops data from the queue. If no data is received before end_time, NULL is returned. 104 105-- To easily calculate end_time a combination of g_get_current_time() and g_time_val_add() can be used. 106-- queue : a GAsyncQueue. 107-- end_time : a GTimeVal, determining the final time. 108-- Returns : data from the queue or NULL, when no data is received before end_time. 109-- g_async_queue_length () 110 111-- gint g_async_queue_length (GAsyncQueue *queue); 112 113-- Returns the length of the queue, negative values mean waiting threads, positive values mean available entries in the queue. Actually this function returns the number of data items in the queue minus the number of waiting threads. Thus a return value of 0 could mean 'n' entries in the queue and 'n' thread waiting. That can happen due to locking of the queue or due to scheduling. 114-- queue : a GAsyncQueue. 115-- Returns : the length of the queue. 116-- g_async_queue_lock () 117 118-- void g_async_queue_lock (GAsyncQueue *queue); 119 120-- Acquires the queue's lock. After that you can only call the g_async_queue_*_unlocked() function variants on that queue. Otherwise it will deadlock. 121-- queue : a GAsyncQueue. 122-- g_async_queue_unlock () 123 124-- void g_async_queue_unlock (GAsyncQueue *queue); 125 126-- Releases the queue's lock. 127-- queue : a GAsyncQueue. 128-- g_async_queue_ref_unlocked () 129 130-- void g_async_queue_ref_unlocked (GAsyncQueue *queue); 131 132-- Warning 133 134-- g_async_queue_ref_unlocked is deprecated and should not be used in newly-written code. 135 136-- Increases the reference count of the asynchronous queue by 1. 137-- queue : a GAsyncQueue. 138-- g_async_queue_unref_and_unlock () 139 140-- void g_async_queue_unref_and_unlock (GAsyncQueue *queue); 141 142-- Warning 143 144-- g_async_queue_unref_and_unlock is deprecated and should not be used in newly-written code. 145 146-- Decreases the reference count of the asynchronous queue by 1 and releases the lock. This function must be called while holding the queue's lock. If the reference count went to 0, the queue will be destroyed and the memory allocated will be freed. 147-- queue : a GAsyncQueue. 148-- g_async_queue_push_unlocked () 149 150-- void g_async_queue_push_unlocked (GAsyncQueue *queue, 151-- gpointer data); 152 153-- Pushes the data into the queue. data must not be NULL. This function must be called while holding the queue's lock. 154-- queue : a GAsyncQueue. 155-- data : data to push into the queue. 156-- g_async_queue_pop_unlocked () 157 158-- gpointer g_async_queue_pop_unlocked (GAsyncQueue *queue); 159 160-- Pops data from the queue. This function blocks until data become available. This function must be called while holding the queue's lock. 161-- queue : a GAsyncQueue. 162-- Returns : data from the queue. 163-- g_async_queue_try_pop_unlocked () 164 165-- gpointer g_async_queue_try_pop_unlocked (GAsyncQueue *queue); 166 167-- Tries to pop data from the queue. If no data is available, NULL is returned. This function must be called while holding the queue's lock. 168-- queue : a GAsyncQueue. 169-- Returns : data from the queue or NULL, when no data is available immediately. 170-- g_async_queue_timed_pop_unlocked () 171 172-- gpointer g_async_queue_timed_pop_unlocked 173-- (GAsyncQueue *queue, 174-- GTimeVal *end_time); 175 176-- Pops data from the queue. If no data is received before end_time, NULL is returned. This function must be called while holding the queue's lock. 177 178-- To easily calculate end_time a combination of g_get_current_time() and g_time_val_add() can be used. 179-- queue : a GAsyncQueue. 180-- end_time : a GTimeVal, determining the final time. 181-- Returns : data from the queue or NULL, when no data is received before end_time. 182-- g_async_queue_length_unlocked () 183 184-- gint g_async_queue_length_unlocked (GAsyncQueue *queue); 185 186-- Returns the length of the queue, negative values mean waiting threads, positive values mean available entries in the queue. Actually this function returns the number of data items in the queue minus the number of waiting threads. Thus a return value of 0 could mean 'n' entries in the queue and 'n' thread waiting. That can happen due to locking of the queue or due to scheduling. This function must be called while holding the queue's lock. 187-- queue : a GAsyncQueue. 188-- Returns : the length of the queue. 189end