Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00031
00032
00033 #pragma once
00034
00035
00036 #include "../api_core.h"
00037
00038 #ifdef WIN32
00039 #include <windows.h>
00040 #else
00041 #ifndef __USE_UNIX98
00042 #define __USE_UNIX98
00043 #endif
00044 #include <pthread.h>
00045 #endif
00046
00050 class CL_API_CORE CL_Mutex
00051 {
00054
00055 public:
00057 CL_Mutex();
00058
00059 ~CL_Mutex();
00060
00061
00065
00066 public:
00067
00068
00072
00073 public:
00075 void lock();
00076
00078 bool try_lock();
00079
00081 void unlock();
00082
00083
00087
00088 private:
00089 #ifdef WIN32
00090 CRITICAL_SECTION critical_section;
00091 #else
00092 pthread_mutex_t handle;
00093 #endif
00094
00095 };
00096
00100 class CL_API_CORE CL_MutexSection
00101 {
00104
00105 public:
00107 CL_MutexSection(CL_Mutex *mutex, bool lock_mutex = true)
00108 : mutex(mutex), lock_count(0)
00109 {
00110 if (lock_mutex)
00111 lock();
00112 }
00113
00114 ~CL_MutexSection()
00115 {
00116 if (lock_count > 0 && mutex)
00117 mutex->unlock();
00118 lock_count = 0;
00119 }
00120
00121
00125
00126 public:
00128 int get_lock_count() const
00129 {
00130 return lock_count;
00131 }
00132
00133
00137
00138 public:
00140 void lock()
00141 {
00142 if (mutex)
00143 mutex->lock();
00144 lock_count++;
00145 }
00146
00148 bool try_lock()
00149 {
00150 if (mutex == 0 || mutex->try_lock())
00151 {
00152 lock_count++;
00153 return true;
00154 }
00155 return false;
00156 }
00157
00159 void unlock()
00160 {
00161 if (lock_count <= 0)
00162 return;
00163
00164 if (mutex)
00165 mutex->unlock();
00166 lock_count--;
00167 }
00168
00169
00173
00174 private:
00175 CL_Mutex *mutex;
00176
00177 int lock_count;
00179 };
00180
00181