{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (garetxe@gmail.com)

The GRecMutex struct is an opaque data structure to represent a
recursive mutex. It is similar to a #GMutex with the difference
that it is possible to lock a GRecMutex multiple times in the same
thread without deadlock. When doing so, care has to be taken to
unlock the recursive mutex as often as it has been locked.

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

A GRecMutex should only be accessed with the
g_rec_mutex_ functions.
-}

module GI.GLib.Structs.RecMutex
    ( 

-- * Exported types
    RecMutex(..)                            ,
    noRecMutex                              ,


 -- * Methods
-- ** recMutexClear
    recMutexClear                           ,


-- ** recMutexInit
    recMutexInit                            ,


-- ** recMutexLock
    recMutexLock                            ,


-- ** recMutexTrylock
    recMutexTrylock                         ,


-- ** recMutexUnlock
    recMutexUnlock                          ,




    ) where

import Prelude ()
import Data.GI.Base.ShortPrelude

import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map

import GI.GLib.Types
import GI.GLib.Callbacks

newtype RecMutex = RecMutex (ForeignPtr RecMutex)
noRecMutex :: Maybe RecMutex
noRecMutex = Nothing

-- method RecMutex::clear
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "RecMutex", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "RecMutex", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "g_rec_mutex_clear" g_rec_mutex_clear :: 
    Ptr RecMutex ->                         -- _obj : TInterface "GLib" "RecMutex"
    IO ()


recMutexClear ::
    (MonadIO m) =>
    RecMutex ->                             -- _obj
    m ()
recMutexClear _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    g_rec_mutex_clear _obj'
    touchManagedPtr _obj
    return ()

-- method RecMutex::init
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "RecMutex", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "RecMutex", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "g_rec_mutex_init" g_rec_mutex_init :: 
    Ptr RecMutex ->                         -- _obj : TInterface "GLib" "RecMutex"
    IO ()


recMutexInit ::
    (MonadIO m) =>
    RecMutex ->                             -- _obj
    m ()
recMutexInit _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    g_rec_mutex_init _obj'
    touchManagedPtr _obj
    return ()

-- method RecMutex::lock
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "RecMutex", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "RecMutex", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "g_rec_mutex_lock" g_rec_mutex_lock :: 
    Ptr RecMutex ->                         -- _obj : TInterface "GLib" "RecMutex"
    IO ()


recMutexLock ::
    (MonadIO m) =>
    RecMutex ->                             -- _obj
    m ()
recMutexLock _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    g_rec_mutex_lock _obj'
    touchManagedPtr _obj
    return ()

-- method RecMutex::trylock
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "RecMutex", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "RecMutex", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TBoolean
-- throws : False
-- Skip return : False

foreign import ccall "g_rec_mutex_trylock" g_rec_mutex_trylock :: 
    Ptr RecMutex ->                         -- _obj : TInterface "GLib" "RecMutex"
    IO CInt


recMutexTrylock ::
    (MonadIO m) =>
    RecMutex ->                             -- _obj
    m Bool
recMutexTrylock _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    result <- g_rec_mutex_trylock _obj'
    let result' = (/= 0) result
    touchManagedPtr _obj
    return result'

-- method RecMutex::unlock
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "RecMutex", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "RecMutex", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "g_rec_mutex_unlock" g_rec_mutex_unlock :: 
    Ptr RecMutex ->                         -- _obj : TInterface "GLib" "RecMutex"
    IO ()


recMutexUnlock ::
    (MonadIO m) =>
    RecMutex ->                             -- _obj
    m ()
recMutexUnlock _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    g_rec_mutex_unlock _obj'
    touchManagedPtr _obj
    return ()