tamer::mutex Class Reference

An event-based mutual-exclusion lock. More...

#include <tamer/lock.hh>

List of all members.

Public Member Functions

 mutex ()
 Default constructor creates a mutex.
void acquire (const event<> &done)
 Acquire the mutex for exclusive access.
void release ()
 Release a mutex acquired for exclusive access.
void acquire_shared (const event<> &done)
 Acquire the mutex for shared access.
void release_shared ()
 Release a mutex acquired for shared access.


Detailed Description

An event-based mutual-exclusion lock.

A mutex object is an event-based mutual-exclusion and/or read/write lock. Acquire the mutex with the acquire() method, which triggers its event<> argument when the mutex is acquired. Later, release it with the release() method.

The mutex class also provides shared access, as for a read lock. Acquire the mutex for shared access with the acquire_shared() method, and release it thereafter with the release_shared() method. More than one callback function can acquire_shared() a mutex at once, but all the shared acquirers must release_shared() the mutex before a subsequent acquire() will succeed.

Acquire requests are served strictly in order of their arrival. For example, the following code:

     tamer::mutex m;
     tamed void exclusive(int which) {
         twait { m.acquire(make_event()); }
         printf("%d: acquired\n", which);
         twait { tamer::at_delay_sec(1, make_event()); }
         m.release();
         printf("%d: released\n", which);
     }
     tamed void shared(int which) {
         twait { m.acquire_shared(make_event()); }
         printf("%d: acquired shared\n", which);
         twait { tamer::at_delay_sec(1, make_event()); }
         m.release_shared();
         printf("%d: released shared\n", which);
     }
     int main(int argc, char *argv[]) {
         tamer::initialize();
         exclusive(1);
         shared(2);
         shared(3);
         exclusive(4);
         shared(5);
         tamer::loop();
     }

will generate this output:

     1: acquired
     1: released
     2: acquired shared
     3: acquired shared
     2: released shared
     3: released shared
     4: acquired
     4: released
     5: acquired shared
     5: released shared
  


Member Function Documentation

void tamer::mutex::acquire ( const event<> &  done  )  [inline]

Acquire the mutex for exclusive access.

Parameters:
done Event triggered when the mutex is acquired.
The mutex must later be released with the release() method.

void tamer::mutex::release (  )  [inline]

Release a mutex acquired for exclusive access.

Precondition:
The mutex must currently be acquired for exclusive access.
See also:
acquire()

void tamer::mutex::acquire_shared ( const event<> &  done  )  [inline]

Acquire the mutex for shared access.

Parameters:
done Event triggered when the mutex is acquired.
The mutex must later be released with the release_shared() method.

void tamer::mutex::release_shared (  )  [inline]

Release a mutex acquired for shared access.

Precondition:
The mutex must currently be acquired for shared access.
See also:
acquire_shared()


The documentation for this class was generated from the following file:
Generated on Wed Aug 22 17:55:29 2007 for Tamer by  doxygen 1.5.1