C-chat
Classes | Typedefs | Functions
synchronization.h File Reference

Provides types allowing to synchronize access to resources. More...

Go to the source code of this file.

Classes

class  Mutex
 A semaphore allowing mutual exclusion. More...
 
class  ReadWriteLock
 A read/write lock to synchronize resource access. More...
 

Typedefs

typedef struct Mutex Mutex
 
typedef struct ReadWriteLock ReadWriteLock
 

Functions

Mutex createMutex ()
 Creates a mutex. More...
 
void acquireMutex (Mutex mutex)
 Acquires the given mutex. More...
 
void releaseMutex (Mutex mutex)
 Releases the given mutex. More...
 
void destroyMutex (Mutex mutex)
 Destroys the given mutex. More...
 
ReadWriteLock createReadWriteLock ()
 Creates a read/write lock. More...
 
void acquireRead (ReadWriteLock lock)
 Acquires read lock on the given read/write lock. More...
 
void acquireWrite (ReadWriteLock lock)
 Acquires write lock on the given read/write lock. More...
 
void releaseRead (ReadWriteLock lock)
 Releases read lock on the given read/write lock. More...
 
void releaseWrite (ReadWriteLock lock)
 Releases write lock on the given read/write lock. More...
 
void destroyReadWriteLock (ReadWriteLock lock)
 Destroys the given read/write lock. More...
 

Detailed Description

Provides types allowing to synchronize access to resources.

Function Documentation

◆ acquireMutex()

void acquireMutex ( Mutex  mutex)

Acquires the given mutex.

Once acquired, a mutex MUST be released by the thread which acquired it before any thread can re-acquiring. This is a blocking-call.

Parameters
mutexThe mutex to acquire

◆ acquireRead()

void acquireRead ( ReadWriteLock  lock)

Acquires read lock on the given read/write lock.

This is a blocking call. It waits for the read lock to be available, this one can be unavailable if write lock is acquired.

Parameters
lockThe read/write lock to acquire read on

◆ acquireWrite()

void acquireWrite ( ReadWriteLock  lock)

Acquires write lock on the given read/write lock.

This is a blocking call. It wait for the read lock to be available, this one can be unavailable if a thread already acquired write lock or if any thread acquired read lock.

Parameters
lockThe read/write lock to acquire write on

◆ createMutex()

Mutex createMutex ( )

Creates a mutex.

Returns
a ready to use mutex

◆ createReadWriteLock()

ReadWriteLock createReadWriteLock ( )

Creates a read/write lock.

A read/write lock allow to protect access to a resource. When acquiring read lock, no thread acquire write lock but all threads can still acquire read lock. When acquiring write lock, no thread can acquire any lock before lock release.

It allows to ensure resources is not read while writing it.

Returns
a read/write lock

◆ destroyMutex()

void destroyMutex ( Mutex  mutex)

Destroys the given mutex.

WARNING : A mutex MUST NOT be destroyed twice. It would lead to unexpected behavior.

Parameters
mutexThe mutex to destroy

◆ destroyReadWriteLock()

void destroyReadWriteLock ( ReadWriteLock  lock)

Destroys the given read/write lock.

It frees allocated memory for the given read/write lock.

WARNING: This function MUST NOT be called twice for the same lock. It would lead to unexpected behavior.

Parameters
lockThe read/write lock to destroy

◆ releaseMutex()

void releaseMutex ( Mutex  mutex)

Releases the given mutex.

The given mutex MUST be acquired by the current thread before releasing.

Parameters
mutexThe mutex to release

◆ releaseRead()

void releaseRead ( ReadWriteLock  lock)

Releases read lock on the given read/write lock.

The lock MUST have been acquired by this thread before.

Parameters
lockThe read/write lock to release read of

◆ releaseWrite()

void releaseWrite ( ReadWriteLock  lock)

Releases write lock on the given read/write lock.

The lock MUST have been acquired by this thread before.

Parameters
lockThe read/write lock to release write of