Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/nto/bakJuly/RWLock.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__RWLock_CPP__
00002 #define __J2K__RWLock_CPP__
00003 
00004 #include <j2k/nto/RWLock.hpp>
00005 
00006 //
00007 // Classical Implementation of Reader/Writer lock using Mutex
00008 // Don't use pthread_rwlock, since it's not implemented on every platforms;
00009 // however, pthread_mutex is more common than pthread_rwlock, 
00010 // so use it to implement the other.
00011 //
00012 // It's also simpler to code then to play with C structs...
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

Generated on Sun Oct 14 18:46:24 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001