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

C:/temp/src/j2k/nto/Semaphore.cpp

Go to the documentation of this file.
00001 #ifndef  __J2K__Semaphore_CPP__
00002 #define  __J2K__Semaphore_CPP__
00003 
00004 #define DEADLOCK_SEM 100000
00005 
00006 static int cnt = 0;
00007 
00008 #include <j2k/nto/Semaphore.hpp>
00009 
00010   Semaphore::Semaphore( SCHAR init, const char* sem_name, int c ) 
00011     : state( init ), color( c ), fg( 0 )
00012   { 
00013     name = (char*)sem_name;
00014     if ( c > 15 ) { color = c - 16; fg++; }
00015     if ( sem_name[0] == 's' ) { fg = 1; }
00016   }
00017 
00018   void Semaphore::P() 
00019   {
00020     m.lock();
00021     while ( state < 1 ) 
00022     {
00023       m.unlock();
00024         if ( ++cnt > DEADLOCK_SEM ) { 
00025           printf( "Deadlock(%s=%d|%d)", name, cnt, state ); fflush( stdout );
00026           exit( 1 );
00027         }
00028         sched_yield();
00029       m.lock();
00030     }
00031     --state;  
00032     m.unlock();
00033   }
00034 
00035   void Semaphore::V() {
00036     m.lock();
00037       ++state;
00038     m.unlock();
00039   }     
00040 
00041   SCHAR Semaphore::get() {
00042     register SCHAR s = 0;
00043     m.lock();
00044       s = state;
00045     m.unlock();
00046 
00047     return s;
00048   }     
00049 
00050   void Semaphore::set( SCHAR value ) 
00051   {
00052     m.lock();
00053       state = value;
00054     m.unlock();
00055   }     
00056 
00057 #endif

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