[ RCOS.java Home | RCOS Home | David's Home ]

Lock CRC

Name:

Lock

Comment:

The following class defines a "lock". A lock can be BUSY or FREE. There are only two operations allowed on a lock:

Acquire -- wait until the lock is FREE, then set it to BUSY

Release -- set lock to be FREE, waking up a thread waiting in Acquire if necessary

In addition, by convention, only the thread that acquired the lock may release it. As with semaphores, you can't read the lock value (because the value might change immediately after you read it).

Collaborators:

Responsibilities:

class Lock {
  public:
    Lock(char* debugName);              // initialize lock to be FREE
    ~Lock();                            // deallocate lock
    char* getName() { return name; }    // debugging assist

    void Acquire(); // these are the only operations on a lock
    void Release(); // they are both *atomic*

    bool isHeldByCurrentThread();       // true if the current thread
                                        // holds this lock.  Useful for
                                        // checking in Release, and in
                                        // Condition variable ops below.

  private:
    char* name;                         // for debugging
    // plus some other stuff you'll need to define
};