An Application Locking Facility (part 2)

In my last blog post, I presented the idea of an Application Locking Mechanism.  The features that we want to support are:

1)  Multiple readers.

2)  Zero or one writers.

3)  Multiple readers and writers without data corruption.

4)  Wait fairness.

5)  Ensure lock/unlock sanity.

6)  Speed.

So here is the prototype of what we want to do:

//
//  The lock structure.
//

struct lock_t
{
uintmagic;        // lock magic number

uintmutex;        // protects this data structure

intqueue[LOCK_QUEUE_SIZE];        // queue of users

inthead;                          // start of queue

inttail;                          // end of queue
};

//
//  proc = init_lock.
//  return = void.
//  param lock = struct lock_t * : lock structure.
//
//  Initialize a lock structure.
//

void init_lock (struct lock_t *lock);

//
//  proc = check_lock.
//  return = void.
//  param lock = struct lock_t * : lock structure.
//
//  Sanity check a lock structure.
//

void check_lock (struct lock_t *lock);

//
//  proc = queue_add.
//  return = int : the queue index for this item.
//  param lock = struct lock_t * : lock structure.
//  param type = int : lock type (LOCK_READ or LOCK_WRITE).
//
//  Add an item to a lock queue.
//

int queue_add (struct lock_t *lock, int type);

//
//  proc = queue_del.
//  return = void.
//  param lock = struct lock_t * : lock structure.
//  param index = int : queue index.
//
//  Removed an item from a lock queue.
//

void
queue_del (struct lock_t *lock, int index);

//
//  proc = read_lock.
//  return = int : queue index for token.
//  param lock = struct lock_t * : lock structure.
//
//  Perform a Read Lock.
//

int
read_lock (struct lock_t *lock);

//
//  proc = read_unlock.
//  return = void.
//  param lock = struct lock_t * : lock structure.
//  param index = int : queue index – return from read_lock().
//
//  Perform a Read Unlock.
//
void read_unlock (struct lock_t *lock, int index);

//
//  proc = write_lock.
//  return = int : queue index for token.
//  param lock = struct lock_t * : lock structure.
//
//  Perform a Write Lock.
//

int write_lock (struct lock_t *lock);

//
//  proc = write_unlock.
//  return = void.
//  param lock = struct lock_t * : lock structure.
//  param index = int : queue index – return from write_lock().
//
//  Perform a Write Unlock.
//

void write_unlock (struct lock_t *lock, int index);

That is all for this post.  In my next post, I will present the full implementation.