RwLock

class RwLock(pointer: <Error class: unknown class><<Error class: unknown class>>, cleaner: <Error class: unknown class>? = null) : ProxyInstance

The GRWLock struct is an opaque data structure to represent a reader-writer lock. It is similar to a #GMutex in that it allows multiple threads to coordinate access to a shared resource.

The difference to a mutex is that a reader-writer lock discriminates between read-only ('reader') and full ('writer') access. While only one thread at a time is allowed write access (by holding the 'writer' lock via g_rw_lock_writer_lock()), multiple threads can gain simultaneous read-only access (by holding the 'reader' lock via g_rw_lock_reader_lock()).

It is unspecified whether readers or writers have priority in acquiring the lock when a reader already holds the lock and a writer is queued to acquire it.

Here is an example for an array with access functions: |[ GRWLock lock; GPtrArray *array;

gpointer my_array_get (guint index) { gpointer retval = NULL;

if (!array)
  return NULL;

g_rw_lock_reader_lock (&lock);
if (index < array->len)
  retval = g_ptr_array_index (array, index);
g_rw_lock_reader_unlock (&lock);

return retval;

}

void my_array_set (guint index, gpointer data) { g_rw_lock_writer_lock (&lock);

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_rw_lock_writer_unlock (&lock);

} ]| 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 one 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.

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

A GRWLock should only be accessed with the g_rw_lock_ functions.

Since

2.32

Constructors

Link copied to clipboard
constructor()

Allocate a new RwLock.

constructor(scope: <Error class: unknown class>)

Allocate a new RwLock using the provided AutofreeScope.

constructor(pointer: <Error class: unknown class><<Error class: unknown class>>, cleaner: <Error class: unknown class>? = null)

Properties

Link copied to clipboard
val glibRWLockPointer: <Error class: unknown class><<Error class: unknown class>>
Link copied to clipboard
open override val handle: <Error class: unknown class>?

Functions

Link copied to clipboard
fun clear()

Frees the resources allocated to a lock with g_rw_lock_init().

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Compare two proxy instances for equality. This will compare both the type of the instances, and their memory addresses.

Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
fun init()

Initializes a #GRWLock so that it can be used.

Link copied to clipboard

Obtain a read lock on @rw_lock. If another thread currently holds the write lock on @rw_lock, the current thread will block until the write lock was (held and) released. If another thread does not hold the write lock, but is waiting for it, it is implementation defined whether the reader or writer will block. Read locks can be taken recursively.

Link copied to clipboard

Tries to obtain a read lock on @rw_lock and returns true if the read lock was successfully obtained. Otherwise it returns false.

Link copied to clipboard

Release a read lock on @rw_lock.

Link copied to clipboard

Obtain a write lock on @rw_lock. If another thread currently holds a read or write lock on @rw_lock, the current thread will block until all other threads have dropped their locks on @rw_lock.

Link copied to clipboard

Tries to obtain a write lock on @rw_lock. If another thread currently holds a read or write lock on @rw_lock, it immediately returns false. Otherwise it locks @rw_lock and returns true.

Link copied to clipboard

Release a write lock on @rw_lock.