gi-glib-0.2.46.13: GLib bindings

CopyrightWill Thompson, Iñaki García Etxebarria and Jonas Platte
LicenseLGPL-2.1
MaintainerIñaki García Etxebarria (garetxe@gmail.com)
Safe HaskellNone
LanguageHaskell2010

GI.GLib.Structs.Cond

Contents

Description

The #GCond struct is an opaque data structure that represents a condition. Threads can block on a #GCond if they find a certain condition to be false. If other threads change the state of this condition they signal the #GCond, and that causes the waiting threads to be woken up.

Consider the following example of a shared variable. One or more threads can wait for data to be published to the variable and when another thread publishes the data, it can signal one of the waiting threads to wake up to collect the data.

Here is an example for using GCond to block a thread until a condition is satisfied: |[language="C" -- gpointer current_data = NULL; GMutex data_mutex; GCond data_cond;

void push_data (gpointer data) { g_mutex_lock (&data_mutex); current_data = data; g_cond_signal (&data_cond); g_mutex_unlock (&data_mutex); }

gpointer pop_data (void) { gpointer data;

g_mutex_lock (&data_mutex); while (!current_data) g_cond_wait (&data_cond, &data_mutex); data = current_data; current_data = NULL; g_mutex_unlock (&data_mutex);

return data; } ]| Whenever a thread calls pop_data() now, it will wait until current_data is non-%NULL, i.e. until some other thread has called push_data().

The example shows that use of a condition variable must always be paired with a mutex. Without the use of a mutex, there would be a race between the check of current_data by the while loop in pop_data() and waiting. Specifically, another thread could set current_data after the check, and signal the cond (with nobody waiting on it) before the first thread goes to sleep. #GCond is specifically useful for its ability to release the mutex and go to sleep atomically.

It is also important to use the g_cond_wait() and g_cond_wait_until() functions only inside a loop which checks for the condition to be true. See g_cond_wait() for an explanation of why the condition may not be true even after it returns.

If a #GCond is allocated in static storage then it can be used without initialisation. Otherwise, you should call g_cond_init() on it and g_cond_clear() when done.

A #GCond should only be accessed via the g_cond_ functions.

Synopsis

Exported types

newtype Cond Source

Constructors

Cond (ForeignPtr Cond) 

Instances

((~) * info (ResolveCondMethod t Cond), MethodInfo * info Cond p) => IsLabel t (Cond -> p) Source 

Methods

fromLabel :: Proxy# Symbol t -> Cond -> p

((~) * info (ResolveCondMethod t Cond), MethodInfo * info Cond p) => IsLabelProxy t (Cond -> p) Source 

Methods

fromLabelProxy :: Proxy Symbol t -> Cond -> p

((~) (TYPE Lifted) signature (Mutex -> Int64 -> m Bool), MonadIO m) => MethodInfo (TYPE Lifted) CondWaitUntilMethodInfo Cond signature Source 
((~) (TYPE Lifted) signature (Mutex -> m ()), MonadIO m) => MethodInfo (TYPE Lifted) CondWaitMethodInfo Cond signature Source 
((~) (TYPE Lifted) signature (m ()), MonadIO m) => MethodInfo (TYPE Lifted) CondSignalMethodInfo Cond signature Source 
((~) (TYPE Lifted) signature (m ()), MonadIO m) => MethodInfo (TYPE Lifted) CondInitMethodInfo Cond signature Source 
((~) (TYPE Lifted) signature (m ()), MonadIO m) => MethodInfo (TYPE Lifted) CondClearMethodInfo Cond signature Source 
((~) (TYPE Lifted) signature (m ()), MonadIO m) => MethodInfo (TYPE Lifted) CondBroadcastMethodInfo Cond signature Source 

newZeroCond :: MonadIO m => m Cond Source

Construct a Cond struct initialized to zero.

Methods

condBroadcast

condClear

data CondClearMethodInfo Source

Instances

((~) (TYPE Lifted) signature (m ()), MonadIO m) => MethodInfo (TYPE Lifted) CondClearMethodInfo Cond signature Source 

condClear :: MonadIO m => Cond -> m () Source

condInit

data CondInitMethodInfo Source

Instances

((~) (TYPE Lifted) signature (m ()), MonadIO m) => MethodInfo (TYPE Lifted) CondInitMethodInfo Cond signature Source 

condInit :: MonadIO m => Cond -> m () Source

condSignal

data CondSignalMethodInfo Source

Instances

((~) (TYPE Lifted) signature (m ()), MonadIO m) => MethodInfo (TYPE Lifted) CondSignalMethodInfo Cond signature Source 

condSignal :: MonadIO m => Cond -> m () Source

condWait

data CondWaitMethodInfo Source

Instances

((~) (TYPE Lifted) signature (Mutex -> m ()), MonadIO m) => MethodInfo (TYPE Lifted) CondWaitMethodInfo Cond signature Source 

condWait :: MonadIO m => Cond -> Mutex -> m () Source

condWaitUntil