Skip to content

GLib.RecMutex

record (struct)

The GRecMutex struct is an opaque data structure to represent a recursive mutex. It is similar to a Mutex 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 RecMutex is allocated in static storage then it can be used without initialisation. Otherwise, you should call RecMutex.init on it and RecMutex.clear when done.

A GRecMutex should only be accessed with the g_rec_mutex_ functions.

Methods

clear

def clear(self) -> None

Frees the resources allocated to a recursive mutex with RecMutex.init.

This function should not be used with a RecMutex that has been statically allocated.

Calling RecMutex.clear on a locked recursive mutex leads to undefined behaviour.

init

def init(self) -> None

Initializes a RecMutex so that it can be used.

This function is useful to initialize a recursive mutex that has been allocated on the stack, or as part of a larger structure.

It is not necessary to initialise a recursive mutex that has been statically allocated.

typedef struct {
    GRecMutex m;
    ...
  } Blob;

Blob *b;

b = g_new (Blob, 1);
g_rec_mutex_init (&b->m);

Calling RecMutex.init on an already initialized RecMutex leads to undefined behaviour.

To undo the effect of RecMutex.init when a recursive mutex is no longer needed, use RecMutex.clear.

lock

def lock(self) -> None

Locks rec_mutex. If rec_mutex is already locked by another thread, the current thread will block until rec_mutex is unlocked by the other thread. If rec_mutex is already locked by the current thread, the 'lock count' of rec_mutex is increased. The mutex will only become available again when it is unlocked as many times as it has been locked.

trylock

def trylock(self) -> bool

Tries to lock rec_mutex. If rec_mutex is already locked by another thread, it immediately returns False. Otherwise it locks rec_mutex and returns True.

unlock

def unlock(self) -> None

Unlocks rec_mutex. If another thread is blocked in a RecMutex.lock call for rec_mutex, it will become unblocked and can lock rec_mutex itself.

Calling RecMutex.unlock on a recursive mutex that is not locked by the current thread leads to undefined behaviour.

Properties

p

p: int  # read/write

i

i: list[int]  # read/write