00001 /* 00002 * $Id: Mutex.cpp,v 1.2 2002/07/08 04:58:12 mindstorm2600 Exp $ 00003 * 00004 * This classes handle everything about thread synchronization 00005 * 00006 * $Log: Mutex.cpp,v $ 00007 * Revision 1.2 2002/07/08 04:58:12 mindstorm2600 00008 * Adding every class to the clawsoft namespace 00009 * 00010 * Revision 1.1 2002/07/06 03:31:27 mindstorm2600 00011 * Filenames and classnames changed succesfully 00012 * 00013 * Revision 1.6 2002/07/01 04:15:33 mindstorm2600 00014 * Adding an appropiated destructor and the requested exceptions 00015 * 00016 * Revision 1.5 2002/07/01 03:37:48 mindstorm2600 00017 * Adding more thread abstraction, now using SDL's threads interface 00018 * 00019 * Revision 1.4 2002/07/01 03:01:36 mindstorm2600 00020 * Starting SDL support 00021 * 00022 * Revision 1.3 2002/06/26 06:14:39 mindstorm2600 00023 * Agregue la opcion para activar el mutex debugger 00024 * 00025 * Revision 1.2 2002/06/26 05:48:30 mindstorm2600 00026 * cambie el nombre de la clase 00027 * 00028 * Revision 1.1 2002/06/26 05:45:50 mindstorm2600 00029 * These are the thread synchronization classes 00030 * 00031 */ 00032 #include<iostream.h> 00033 #include<errno.h> 00034 #include<errno.h> 00035 #ifdef USE_GNUPTH 00036 #include<pth.h> 00037 #else 00038 #include<SDL/SDL.h> 00039 #endif 00040 #include"Mutex.h" 00041 00042 namespace clawsoft{ 00043 00044 Mutex::Mutex(int autoinit){ 00045 setClassName("Mutex"); 00046 if(autoinit) 00047 init(); 00048 } 00049 00050 #ifdef USE_GNUPTH 00051 void Mutex::init(){ 00052 pth_mutex_init(&mutex); 00053 #else 00054 void Mutex::init(){ 00055 mutex = SDL_CreateMutex(); 00056 #endif 00057 } 00058 00059 void Mutex::lock(){ 00060 #ifdef USE_GNUPTH 00061 pth_mutex_acquire(&mutex); 00062 #ifdef DEBUG_MUTEXES 00063 cout << "Mutex::lock " << (unsigned int)&mutex << " in " << (unsigned int)pth_self() << endl; 00064 #endif 00065 #else 00066 if(SDL_mutexP(mutex) == -1) 00067 throw UnableToLockMutexException(); 00068 #ifdef DEBUG_MUTEXES 00069 cout << "Mutex::lock " << (unsigned int)mutex << " in " << (unsigned int)SDL_ThreadID() << endl; 00070 #endif 00071 #endif 00072 } 00073 00074 00075 void Mutex::unlock(){ 00076 #ifdef USE_GNUPTH 00077 pth_mutex_release(&mutex); 00078 #ifdef DEBUG_MUTEXES 00079 cout << "Mutex::unlock " << (unsigned int)&mutex << " in " << (unsigned int)pth_self() << endl; 00080 #endif 00081 #else 00082 if(SDL_mutexV(mutex) == -1) 00083 throw UnableToUnlockMutexException(); 00084 #ifdef DEBUG_MUTEXES 00085 cout << "Mutex::unlock " << (unsigned int)&mutex << " in " << (unsigned int)SDL_ThreadID() << endl; 00086 #endif 00087 #endif 00088 } 00089 00090 void Mutex::destroy(){ 00091 #ifndef USE_GNUPTH 00092 SDL_DestroyMutex(mutex); 00093 #endif 00094 } 00095 00096 };
Powered by: