Advertisement
Guest User

MikelSV Mutex

a guest
Feb 2nd, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. // MikelSV Mutex
  2.  
  3. class TLock{
  4. #ifdef WIN32
  5.     CRITICAL_SECTION shared_mutex;
  6. #else
  7.     pthread_mutex_t shared_mutex;
  8. #endif
  9.     DWORD threadid;
  10. public:
  11.     int lock; bool flock;
  12.  
  13. TLock(){
  14.     memset(this, 0, sizeof(*this));
  15. #ifdef WIN32
  16. #ifndef WINCE
  17.     if(shared_mutex.LockCount>=0 && !shared_mutex.OwningThread) { InitializeCriticalSection(&shared_mutex); flock=1; }
  18. #else
  19.     if(shared_mutex.LockCount==0 && !shared_mutex.hCrit){ InitializeCriticalSection(&shared_mutex); flock=1; }
  20. #endif
  21. #else
  22.     pthread_mutex_init(&shared_mutex, NULL);
  23. #endif
  24. return ;
  25. }
  26.  
  27. ~TLock(){
  28.     if(lock) UnLock();
  29. #ifdef WIN32
  30.     DeleteCriticalSection(&shared_mutex);
  31. #else
  32.     pthread_mutex_destroy(&shared_mutex);
  33. #endif
  34. }
  35.  
  36. bool IsLock(){
  37.     DWORD thid=GetCurrentThreadId();
  38.     if(lock && thid!=threadid){
  39.         return 1;
  40.     }
  41.  
  42.     return 0;
  43. }
  44.  
  45. bool Lock(){
  46. #ifdef WIN32
  47.     if(!flock){
  48.     tlocklock.Lock();
  49. #ifndef WINCE
  50.     if(shared_mutex.LockCount>=0 && !shared_mutex.OwningThread && !shared_mutex.LockSemaphore){
  51.         InitializeCriticalSection(&shared_mutex); flock=1; lock=0; }
  52. #else
  53.     if(shared_mutex.LockCount==0 && !shared_mutex.hCrit && !shared_mutex.LockSemaphore){ InitializeCriticalSection(&shared_mutex); flock=1; lock=0; }
  54. #endif
  55.     tlocklock.UnLock();
  56.     }
  57. #endif
  58.  
  59.     DWORD thid=GetCurrentThreadId();
  60.     if(lock && thid==threadid){
  61.         lock++; return 1;
  62.     }
  63.  
  64. #ifdef WIN32
  65.     int llc=shared_mutex.LockCount;
  66.     HANDLE t=shared_mutex.OwningThread;
  67.  
  68.     try{
  69.     EnterCriticalSection(&shared_mutex);
  70.     }catch(...){
  71.         return 0;
  72.     }
  73.     if(lock || !shared_mutex.OwningThread){ // || t
  74.         globalerror("GLOCK Lock!\r\n");
  75.     }
  76. #else
  77.     pthread_mutex_lock(&shared_mutex);
  78.     if(lock){
  79.         globalerror("GLOCK Lock!\r\n");
  80.     }
  81. #endif
  82.  
  83.     lock=1; threadid=thid; return 1;
  84. }
  85.  
  86. bool UnLock(){
  87.     DWORD thid=GetCurrentThreadId();
  88.  
  89.     if(lock>1){
  90.         if(thid!=threadid){
  91.             globalerror("TLOCK UnLock thread id!\r\n"); return 0;
  92.         }
  93.         lock--; return 1;
  94.     }
  95.  
  96. #ifdef WIN32
  97.     if(!lock || !shared_mutex.OwningThread){
  98.         globalerror("GLOCK UnLock!\r\n");
  99.     }
  100.  
  101.     lock=0; threadid=0;
  102.     LeaveCriticalSection(&shared_mutex);
  103. #else
  104.     if(!lock){
  105.         globalerror("GLOCK UnLock!\r\n");
  106.     }
  107.     lock=0; threadid=0;
  108.     pthread_mutex_unlock(&shared_mutex);
  109. #endif
  110. return 1;
  111. }
  112.  
  113. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement