Class RecMutex
- All Implemented Interfaces:
Proxy
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.
-
Constructor Summary
ConstructorDescriptionRecMutex()
Allocate a new RecMutex.Allocate a new RecMutex.RecMutex
(MemorySegment address) Create a RecMutex proxy instance for the provided memory address.RecMutex
(MemorySegment p, int[] i) Allocate a new RecMutex with the fields set to the provided values.RecMutex
(MemorySegment p, int[] i, Arena arena) Allocate a new RecMutex with the fields set to the provided values. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Frees the resources allocated to a recursive mutex with g_rec_mutex_init().static MemoryLayout
The memory layout of the native struct.void
init()
Initializes aGRecMutex
so that it can be used.void
lock()
Locks this RecMutex.int[]
readI()
Read the value of the fieldi
.readP()
Read the value of the fieldp
.boolean
trylock()
Tries to lock this RecMutex.void
unlock()
Unlocks this RecMutex.void
Write a value in the fieldi
.void
Write a value in the fieldp
.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
RecMutex
Create a RecMutex proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
RecMutex
Allocate a new RecMutex.- Parameters:
arena
- to control the memory allocation scope
-
RecMutex
public RecMutex()Allocate a new RecMutex. The memory is allocated withArena.ofAuto()
. -
RecMutex
Allocate a new RecMutex with the fields set to the provided values.- Parameters:
p
- value for the fieldp
i
- value for the fieldi
arena
- to control the memory allocation scope
-
RecMutex
Allocate a new RecMutex with the fields set to the provided values. The memory is allocated withArena.ofAuto()
.- Parameters:
p
- value for the fieldp
i
- value for the fieldi
-
-
Method Details
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readP
-
writeP
Write a value in the fieldp
.- Parameters:
p
- The new value for the fieldp
-
readI
public int[] readI()Read the value of the fieldi
.- Returns:
- The value of the field
i
-
writeI
Write a value in the fieldi
.- Parameters:
i
- The new value for the fieldi
_arena
- to control the memory allocation scope
-
clear
public void clear()Frees the resources allocated to a recursive mutex with g_rec_mutex_init().This function should not be used with a
GRecMutex
that has been statically allocated.Calling g_rec_mutex_clear() on a locked recursive mutex leads to undefined behaviour.
-
init
public void init()Initializes aGRecMutex
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 g_rec_mutex_init() on an already initialized
GRecMutex
leads to undefined behaviour.To undo the effect of g_rec_mutex_init() when a recursive mutex is no longer needed, use g_rec_mutex_clear().
-
lock
public void lock()Locks this RecMutex. If this RecMutex is already locked by another thread, the current thread will block until this RecMutex is unlocked by the other thread. If this RecMutex is already locked by the current thread, the 'lock count' of this RecMutex is increased. The mutex will only become available again when it is unlocked as many times as it has been locked. -
trylock
public boolean trylock()Tries to lock this RecMutex. If this RecMutex is already locked by another thread, it immediately returnsfalse
. Otherwise it locks this RecMutex and returnstrue
.- Returns:
true
if this RecMutex could be locked
-
unlock
public void unlock()Unlocks this RecMutex. If another thread is blocked in a g_rec_mutex_lock() call for this RecMutex, it will become unblocked and can lock this RecMutex itself.Calling g_rec_mutex_unlock() on a recursive mutex that is not locked by the current thread leads to undefined behaviour.
-