Class StaticRWLock
- All Implemented Interfaces:
Proxy
GStaticRWLock struct represents a read-write lock. A read-write
lock can be used for protecting data that some portions of code only
read from, while others also write. In such situations it is
desirable that several readers can read at once, whereas of course
only one writer may write at a time.
Take a look at the following example:
GStaticRWLock rwlock = G_STATIC_RW_LOCK_INIT;
GPtrArray *array;
gpointer
my_array_get (guint index)
{
gpointer retval = NULL;
if (!array)
return NULL;
g_static_rw_lock_reader_lock (&rwlock);
if (index < array->len)
retval = g_ptr_array_index (array, index);
g_static_rw_lock_reader_unlock (&rwlock);
return retval;
}
void
my_array_set (guint index, gpointer data)
{
g_static_rw_lock_writer_lock (&rwlock);
if (!array)
array = g_ptr_array_new ();
if (index >= array->len)
g_ptr_array_set_size (array, index + 1);
g_ptr_array_index (array, index) = data;
g_static_rw_lock_writer_unlock (&rwlock);
}
This example shows an array which can be accessed by many readers (the my_array_get() function) simultaneously, whereas the writers (the my_array_set() function) will only be allowed once at a time and only if no readers currently access the array. This is because of the potentially dangerous resizing of the array. Using these functions is fully multi-thread safe now.
Most of the time, writers should have precedence over readers. That means, for this implementation, that as soon as a writer wants to lock the data, no other reader is allowed to lock the data, whereas, of course, the readers that already have locked the data are allowed to finish their operation. As soon as the last reader unlocks the data, the writer will lock it.
Even though GStaticRWLock is not opaque, it should only be used
with the following functions.
All of the g_static_rw_lock_* functions can be used even if
g_thread_init() has not been called. Then they do nothing, apart
from g_static_rw_lock_*_trylock, which does nothing but returning true.
A read-write lock has a higher overhead than a mutex. For example, both
g_static_rw_lock_reader_lock() and g_static_rw_lock_reader_unlock() have
to lock and unlock a GStaticMutex, so it takes at least twice the time
to lock and unlock a GStaticRWLock that it does to lock and unlock a
GStaticMutex. So only data structures that are accessed by multiple
readers, and which keep the lock for a considerable time justify a
GStaticRWLock. The above example most probably would fare better with a
GStaticMutex.
-
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Allocate a new StaticRWLock.StaticRWLock(Arena arena) Deprecated.Allocate a new StaticRWLock.StaticRWLock(MemorySegment address) Deprecated.Create a StaticRWLock proxy instance for the provided memory address.StaticRWLock(StaticMutex mutex, Cond readCond, Cond writeCond, int readCounter, boolean haveWriter, int wantToRead, int wantToWrite) Deprecated.Allocate a new StaticRWLock with the fields set to the provided values.StaticRWLock(StaticMutex mutex, Cond readCond, Cond writeCond, int readCounter, boolean haveWriter, int wantToRead, int wantToWrite, Arena arena) Deprecated.Allocate a new StaticRWLock with the fields set to the provided values. -
Method Summary
Modifier and TypeMethodDescriptionvoidfree()Deprecated.Use aGRWLockinsteadstatic MemoryLayoutDeprecated.The memory layout of the native struct.voidinit()Deprecated.Use g_rw_lock_init() insteadvoidDeprecated.Use g_rw_lock_reader_lock() insteadbooleanDeprecated.Use g_rw_lock_reader_trylock() insteadvoidDeprecated.Use g_rw_lock_reader_unlock() insteadbooleanDeprecated.Read the value of the fieldhave_writer.@Nullable StaticMutexDeprecated.Read the value of the fieldmutex.Deprecated.Read the value of the fieldread_cond.intDeprecated.Read the value of the fieldread_counter.intDeprecated.Read the value of the fieldwant_to_read.intDeprecated.Read the value of the fieldwant_to_write.Deprecated.Read the value of the fieldwrite_cond.voidwriteHaveWriter(boolean haveWriter) Deprecated.Write a value in the fieldhave_writer.voidwriteMutex(@Nullable StaticMutex mutex) Deprecated.Write a value in the fieldmutex.voidwriteReadCond(Cond readCond) Deprecated.Write a value in the fieldread_cond.voidwriteReadCounter(int readCounter) Deprecated.Write a value in the fieldread_counter.voidDeprecated.Use g_rw_lock_writer_lock() insteadbooleanDeprecated.Use g_rw_lock_writer_trylock() insteadvoidDeprecated.Use g_rw_lock_writer_unlock() insteadvoidwriteWantToRead(int wantToRead) Deprecated.Write a value in the fieldwant_to_read.voidwriteWantToWrite(int wantToWrite) Deprecated.Write a value in the fieldwant_to_write.voidwriteWriteCond(Cond writeCond) Deprecated.Write a value in the fieldwrite_cond.Methods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
StaticRWLock
Deprecated.Create a StaticRWLock proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
StaticRWLock
Deprecated.Allocate a new StaticRWLock.- Parameters:
arena- to control the memory allocation scope
-
StaticRWLock
public StaticRWLock()Deprecated.Allocate a new StaticRWLock. The memory is allocated withArena.ofAuto(). -
StaticRWLock
public StaticRWLock(StaticMutex mutex, Cond readCond, Cond writeCond, int readCounter, boolean haveWriter, int wantToRead, int wantToWrite, Arena arena) Deprecated.Allocate a new StaticRWLock with the fields set to the provided values.- Parameters:
mutex- value for the fieldmutexreadCond- value for the fieldreadCondwriteCond- value for the fieldwriteCondreadCounter- value for the fieldreadCounterhaveWriter- value for the fieldhaveWriterwantToRead- value for the fieldwantToReadwantToWrite- value for the fieldwantToWritearena- to control the memory allocation scope
-
StaticRWLock
public StaticRWLock(StaticMutex mutex, Cond readCond, Cond writeCond, int readCounter, boolean haveWriter, int wantToRead, int wantToWrite) Deprecated.Allocate a new StaticRWLock with the fields set to the provided values. The memory is allocated withArena.ofAuto().- Parameters:
mutex- value for the fieldmutexreadCond- value for the fieldreadCondwriteCond- value for the fieldwriteCondreadCounter- value for the fieldreadCounterhaveWriter- value for the fieldhaveWriterwantToRead- value for the fieldwantToReadwantToWrite- value for the fieldwantToWrite
-
-
Method Details
-
getMemoryLayout
Deprecated.The memory layout of the native struct.- Returns:
- the memory layout
-
readMutex
Deprecated.Read the value of the fieldmutex.- Returns:
- The value of the field
mutex
-
writeMutex
Deprecated.Write a value in the fieldmutex.- Parameters:
mutex- The new value for the fieldmutex
-
readReadCond
Deprecated.Read the value of the fieldread_cond.- Returns:
- The value of the field
read_cond
-
writeReadCond
Deprecated.Write a value in the fieldread_cond.- Parameters:
readCond- The new value for the fieldread_cond
-
readWriteCond
Deprecated.Read the value of the fieldwrite_cond.- Returns:
- The value of the field
write_cond
-
writeWriteCond
Deprecated.Write a value in the fieldwrite_cond.- Parameters:
writeCond- The new value for the fieldwrite_cond
-
readReadCounter
public int readReadCounter()Deprecated.Read the value of the fieldread_counter.- Returns:
- The value of the field
read_counter
-
writeReadCounter
public void writeReadCounter(int readCounter) Deprecated.Write a value in the fieldread_counter.- Parameters:
readCounter- The new value for the fieldread_counter
-
readHaveWriter
public boolean readHaveWriter()Deprecated.Read the value of the fieldhave_writer.- Returns:
- The value of the field
have_writer
-
writeHaveWriter
public void writeHaveWriter(boolean haveWriter) Deprecated.Write a value in the fieldhave_writer.- Parameters:
haveWriter- The new value for the fieldhave_writer
-
readWantToRead
public int readWantToRead()Deprecated.Read the value of the fieldwant_to_read.- Returns:
- The value of the field
want_to_read
-
writeWantToRead
public void writeWantToRead(int wantToRead) Deprecated.Write a value in the fieldwant_to_read.- Parameters:
wantToRead- The new value for the fieldwant_to_read
-
readWantToWrite
public int readWantToWrite()Deprecated.Read the value of the fieldwant_to_write.- Returns:
- The value of the field
want_to_write
-
writeWantToWrite
public void writeWantToWrite(int wantToWrite) Deprecated.Write a value in the fieldwant_to_write.- Parameters:
wantToWrite- The new value for the fieldwant_to_write
-
free
Deprecated.Use aGRWLockinsteadReleases all resources allocated tolock.You don't have to call this functions for a
GStaticRWLockwith an unbounded lifetime, i.e. objects declared 'static', but if you have aGStaticRWLockas a member of a structure, and the structure is freed, you should also free theGStaticRWLock. -
init
Deprecated.Use g_rw_lock_init() insteadAGStaticRWLockmust be initialized with this function before it can be used. Alternatively you can initialize it withG_STATIC_RW_LOCK_INIT. -
readerLock
Deprecated.Use g_rw_lock_reader_lock() insteadLocks this StaticRWLock for reading. There may be unlimited concurrent locks for reading of aGStaticRWLockat the same time. If this StaticRWLock is already locked for writing by another thread or if another thread is already waiting to lock this StaticRWLock for writing, this function will block until this StaticRWLock is unlocked by the other writing thread and no other writing threads want to locklock.This lock has to be unlocked by g_static_rw_lock_reader_unlock().GStaticRWLockis not recursive. It might seem to be possible to recursively lock for reading, but that can result in a deadlock, due to writer preference. -
readerTrylock
Deprecated.Use g_rw_lock_reader_trylock() insteadTries to lock this StaticRWLock for reading. If this StaticRWLock is already locked for writing by another thread or if another thread is already waiting to lock this StaticRWLock for writing, immediately returnsfalse. Otherwise locks this StaticRWLock for reading and returnstrue. This lock has to be unlocked by g_static_rw_lock_reader_unlock().- Returns:
true, if this StaticRWLock could be locked for reading
-
readerUnlock
Deprecated.Use g_rw_lock_reader_unlock() insteadUnlockslock.If a thread waits to lock this StaticRWLock for writing and all locks for reading have been unlocked, the waiting thread is woken up and can lock this StaticRWLock for writing. -
writerLock
Deprecated.Use g_rw_lock_writer_lock() insteadLocks this StaticRWLock for writing. If this StaticRWLock is already locked for writing or reading by other threads, this function will block until this StaticRWLock is completely unlocked and then lock this StaticRWLock for writing. While this functions waits to locklock,no other thread can lock this StaticRWLock for reading. When this StaticRWLock is locked for writing, no other thread can lock this StaticRWLock (neither for reading nor writing). This lock has to be unlocked by g_static_rw_lock_writer_unlock(). -
writerTrylock
Deprecated.Use g_rw_lock_writer_trylock() insteadTries to lock this StaticRWLock for writing. If this StaticRWLock is already locked (for either reading or writing) by another thread, it immediately returnsfalse. Otherwise it locks this StaticRWLock for writing and returnstrue. This lock has to be unlocked by g_static_rw_lock_writer_unlock().- Returns:
true, if this StaticRWLock could be locked for writing
-
writerUnlock
Deprecated.Use g_rw_lock_writer_unlock() insteadUnlockslock.If a thread is waiting to lock this StaticRWLock for writing and all locks for reading have been unlocked, the waiting thread is woken up and can lock this StaticRWLock for writing. If no thread is waiting to lock this StaticRWLock for writing, and some thread or threads are waiting to lock this StaticRWLock for reading, the waiting threads are woken up and can lock this StaticRWLock for reading.
-
GRWLockinstead