Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

shm_locking.h File Reference

Implementation of a read-write lock. More...

Go to the source code of this file.

Defines

#define CF_LOCK_SEM   0
#define CF_READER_SEM   1
#define CF_WRITER_SEM   2

Functions

int shm_lock_exclusive (int semid)
 Try to lock a shared memory segment exclusive.

int shm_unlock_exclusive (int semid)
 Unlock an exclusive lock.

int shm_lock_shared (int semid)
 Try to lock a shared memory segment shared.

int shm_unlock_shared (int semid)
 Try to unlock a shared lock.


Detailed Description

Implementation of a read-write lock.

Author:
Christian Kruse, <cjk@wwwtech.de>
This module implements a read-write-lock of a shared memory segment using SysV semaphores. It consists of a semaphore set of three semaphores: one semaphore to lock the set, one semaphore to count readers and one semaphore to show write locking. If the write lock has been set, new readers will wait until the write lock has been released. So no writer could starve to death.

Definition in file shm_locking.h.


Define Documentation

#define CF_LOCK_SEM   0
 

The semaphore locking semaphore number

Definition at line 27 of file shm_locking.h.

#define CF_READER_SEM   1
 

The reader locking semaphore number

Definition at line 28 of file shm_locking.h.

#define CF_WRITER_SEM   2
 

The writer locking semaphore number

Definition at line 29 of file shm_locking.h.


Function Documentation

int shm_lock_exclusive int  semid  ) 
 

Try to lock a shared memory segment exclusive.

We want to lock the shared memory segment exclusively (e.g. for write access). Therefore we have to set the exclusive-bit and we have to wait until the shared counter is 0. The semaphore has to be locked during the exclusive operations, so we *DON'T* unlock it in this routine!

Definition at line 41 of file shm_locking.c.

int shm_lock_shared int  semid  ) 
 

Try to lock a shared memory segment shared.

We can lock a shared memory segment shared if the exclusive bit is not set and the semaphore is not locked. The shared lock itself is just a incrementation of the shared counter. The semaphore will be unlocked at the end of this routine, but the shared counter has to be decremented, too.

Definition at line 75 of file shm_locking.c.

int shm_unlock_exclusive int  semid  ) 
 

Unlock an exclusive lock.

We release an exclusive lock by setting the value of the writer-semaphore to 0.

Definition at line 63 of file shm_locking.c.

int shm_unlock_shared int  semid  ) 
 

Try to unlock a shared lock.

We release the shared lock by decrementing the shared counter.

Definition at line 84 of file shm_locking.c.


Generated on Sun Apr 25 16:37:39 2004 for Classic Forum by doxygen 1.3.5