#include "semaphore.h" /* * This class provides the reader and writer lock controls using semaphores * as described in the text. */ class readerwriter { public: readerwriter(): m_readcount(0), m_writesem(1), m_mutex(1) { } /* * Operations to synchronize readers and writers. */ void enter_read() { // This locks the mutex until it is destroyed at method exit. m_mutex.down(); if(++m_readcount == 1) m_writesem.down(); m_mutex.up(); } void leave_read() { m_mutex.down(); if(--m_readcount == 0) m_writesem.up(); m_mutex.up(); } void enter_write() { m_writesem.down(); } void leave_write() { m_writesem.up(); } private: int m_readcount; semaphore m_mutex; semaphore m_writesem; };