00001 #ifndef __J2K__Semaphore_CPP__ 00002 #define __J2K__Semaphore_CPP__ 00003 00004 #define DEADLOCK_SEM 1000 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