00001 #ifndef __J2K__RWLock_CPP__
00002 #define __J2K__RWLock_CPP__
00003
00004 #include <j2k/nto/RWLock.hpp>
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 void RWLock::read() {
00016 readfail = 1;
00017 do {
00018 if ( loop ) sched_yield();
00019 loop = 1;
00020 mutex.lock();
00021 if ( readers >= 0 ) {
00022 readers++;
00023 readfail = 0;
00024 }
00025 mutex.unlock();
00026 } while( readfail );
00027 }
00028
00029 void RWLock::releaseRead() {
00030 mutex.lock();
00031 if ( readers >= 0 ) {
00032 readers--;
00033 }
00034 mutex.unlock();
00035 }
00036
00037 void RWLock::releaseWrite() {
00038 mutex.lock();
00039 if ( readers < 0 ) {
00040 readers = 0;
00041 }
00042 mutex.unlock();
00043 }
00044
00045 void RWLock::write() {
00046 writefail = 1;
00047 do {
00048 if ( loop ) sched_yield();
00049 loop = 1;
00050 mutex.lock();
00051 if ( readers == 0 ) {
00052 readers = -1;
00053 writefail = 0;
00054 }
00055 mutex.unlock();
00056 } while( writefail );
00057 }
00058
00059 #endif