00001
00002
00003
00004
00005
00006
00007 #include <iostream>
00008 #include <errno.h>
00009 #include <pthread.h>
00010
00011 #include "SysPlusPlus/syscall.h"
00012 #include "ComPlusPlus/Mutex.h"
00013
00014
00018 compp::Mutex::Mutex(){
00019
00020
00021
00022
00023 if ( 0 == syspp::Call::Pthread_mutex_init( &MutexId, NULL ) ) {
00024
00025 }
00026
00027 }
00028
00032 compp::Mutex::~Mutex(){
00033
00034 syspp::Call::Pthread_mutex_destroy( &MutexId );
00035
00036 }
00037
00041 bool compp::Mutex::Lock(){
00042
00043 if ( 0 != syspp::Call::Pthread_mutex_lock ( &MutexId ) ) {
00044 return false;
00045 }
00046
00047 return true;
00048 }
00049
00055 bool compp::Mutex::TryLock(){
00056
00057 if ( 0 != syspp::Call::Pthread_mutex_trylock ( &MutexId ) ) {
00058 return false;
00059 }
00060
00061 return true;
00062 }
00063
00066 bool compp::Mutex::UnLock(){
00067
00068
00069 if ( 0 != syspp::Call::Pthread_mutex_unlock ( &MutexId ) ) {
00070 return false;
00071 }
00072
00073 return true;
00074
00075 }
00076
00077
00078 compp::CriticalSection::CriticalSection ( compp::Mutex & M ) {
00079 mMutex = &M;
00080 mMutex->Lock();
00081 }
00082
00083 compp::CriticalSection::~CriticalSection ( ) {
00084 mMutex->UnLock();
00085 }
00086
00087 #ifdef SPECIALDEBUG
00088
00089
00090 static compp:Mutex sMutex;
00091
00092
00093 void foo () {
00094 compp::CriticalSection cs ( sMutex );
00095
00096 std::cout << "----------- 5 -----------\n";
00097
00098
00099 }
00100
00101 int main ( int argc, char *argv [] ) {
00102 gen::GMutex *g = new gen::GMutex ();
00103
00104 for ( int i = 1 ; i < 100; ++ i ) {
00105
00106 if ( ! g->Lock())
00107 std::cout << "Error Lock\n" ;
00108
00109 if ( ! g->UnLock())
00110 std::cout << "Error UnLock\n" ;
00111
00112 }
00113
00114 return 0;
00115 }
00116
00117 #endif
00118
00119