00001 #ifndef __J2K__RWLock_HPP__ 00002 #define __J2K__RWLock_HPP__ 00003 00004 #include <j2k/Fred/Standard.hpp> 00005 #include <j2k/nto/Mutex.hpp> 00006 00007 // 00008 // Classical Implementation of Reader/Writer lock using Mutex 00009 // Don't use pthread_rwlock, since it's not implemented on every platforms; 00010 // however, pthread_mutex is more common than pthread_rwlock, 00011 // so use it to implement the other. 00012 // 00013 // It's also simpler to code then to play with C structs... 00014 // 00015 // Use your Comp346 book as a reference for the pseudo-code. 00016 // 00017 00018 class RWLock { 00019 public: 00020 inline RWLock(); 00021 inline virtual ~RWLock(); 00022 00023 void read(); 00024 void write(); 00025 00026 void releaseRead(); 00027 void releaseWrite(); 00028 00029 private: 00030 // -1 = writing 00031 // 0 = NotUsed (Available) 00032 // 1+ = reading 00033 00034 long readers; 00035 00036 int readfail; 00037 int writefail; 00038 int loop; 00039 00040 Mutex mutex; 00041 00042 00043 // Not implemented or needed. 00044 private: 00045 // Copy constructor 00046 inline RWLock( const RWLock& src ); 00047 00048 // Assign operator 00049 inline const RWLock& operator=( const RWLock& src ); 00050 }; 00051 00052 #include <j2k/nto/RWLock.inl> 00053 00054 #endif