bpo_lock - Inter-Process Locking Routines

Since multiple processes can access a block of shared memory at any one time there needs to be some form of locking mechanism to avoid a number of processes modifying the structure at once, and putting the structure into an indeterminate state.

There are a number of variations of locking available with different characteristics.


Process Tracking Locks

These main class of lock functions are those that deal with process locks. These functions behave like traditional binary access locks but with the additional capability that the pid currently owning the lock is held within the lock structure so that if the process dies this can be deteced by other processes attempting to obtain the lock and allowing the first process to detect this a way of obtaining the lock.

This group of functions is the prefered locking mechanism since it allows recovery from a number of exception conditions which cannot be handled by a simple bit-lock.

    struct bpo_pid_lock * bpo_Init_pid_lock( struct bpo_pid_lock *bplp,
					     struct timeval *udtp );

    void bpo_pid_Lock( struct bpo_pid_lock *bplp );

    void bpo_pid_Unlock( struct bpo_pid_lock *bplp );
The initialisation function takes a pointer to the lock and a pointer to a timeval structure which should contain a timeout value to be used to determine how often a process should check whether the current owner has died. If the timeval pointer is NULL the timeout value will default to one second.

The lock and unlock functions perform as one would expect.


The Binary Lock

The binary locking routines consist of a complementary pair of lock and unlock routines. These both take as their sole parameter a pointer to a b_lock structure. The routines perform processor atomic instructions or equivalent on this structure to ensure lock consistency.

    void b_Init( b_lock *lockp );
This function sets the lock up for use by the other functions. If this is used on a lock already in use then unexpected behaviour may occur.

    void b_Lock( b_lock *lockp );
This function waits until it can gain exclusive access to the structure. It waits by using the select call with a small timeout.

    void b_Unlock( b_lock *lockp );
This function clears the lock, freeing it up for other users.

    int b_TryLock( b_lock_t *lockp );
This function attempts to obtain the lock. It returns zero if it succeeds, and non-zero if it failed.

    long b_Lockval( b_lock *lockp );
This function returns a non-zero value if the lock is currently held by someone.


There is also a third method of locking whose use is not recommended. Details of it can be found here.


Sleep functions

The lock routines normally use the following routines when waiting to obtain access to the locks described above.

    int bpo_nap_a_while( long secs, long usecs );
This function uses the select system call to causes the process to sleep for the specified time.

    int bpo_take_a_nap( void );
This function call the one above to sleep for a pre-defined time, usually 5mS.