00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _LOG4CPP_THREADING_MSTHREADS_HH
00011 #define _LOG4CPP_THREADING_MSTHREADS_HH
00012
00013
00014
00015
00016 #ifndef _WINDOWS_
00017 # ifndef NOGDI
00018 # define NOGDI // this will circumvent the ERROR #define in windows.h
00019 # define LOG4CPP_UNDEFINE_NOGDI
00020 # endif
00021
00022 # ifndef WIN32_LEAN_AND_MEAN
00023 # define WIN32_LEAN_AND_MEAN
00024 # define LOG4CPP_UNDEFINE_WIN32_LEAN_AND_MEAN
00025 # endif
00026
00027 # include <windows.h>
00028
00029 # ifdef LOG4CPP_UNDEFINE_NOGDI
00030 # undef NOGDI
00031 # endif
00032
00033 # ifdef LOG4CPP_UNDEFINE_WIN32_LEAN_AND_MEAN
00034 # undef WIN32_LEAN_AND_MEAN
00035 # endif
00036
00037 #endif // done dealing with ERROR #define
00038
00039 namespace log4cpp {
00040 namespace threading {
00046 static std::string getThreadId() {
00047 char buffer[16];
00048 sprintf(buffer, "%lu", GetCurrentThreadId());
00049 return std::string(buffer);
00050 };
00051
00055 class LOG4CPP_EXPORT MSMutex {
00056 public:
00057 MSMutex() { InitializeCriticalSection(&_criticalSection); }
00058 ~MSMutex() { DeleteCriticalSection(&_criticalSection); }
00059 inline LPCRITICAL_SECTION getCriticalSection() {
00060 return &_criticalSection;
00061 }
00062
00063 private:
00064 MSMutex(const MSMutex& other);
00065 CRITICAL_SECTION _criticalSection;
00066 };
00067
00071 typedef MSMutex Mutex;
00072
00077 class MSScopedLock {
00078 public:
00079 MSScopedLock(MSMutex& mutex) {
00080 _criticalSection = mutex.getCriticalSection();
00081 EnterCriticalSection(_criticalSection);
00082 }
00083
00084 ~MSScopedLock() { LeaveCriticalSection(_criticalSection); }
00085
00086 private:
00087 MSScopedLock(const MSScopedLock& other);
00088 LPCRITICAL_SECTION _criticalSection;
00089 };
00090
00095 typedef MSScopedLock ScopedLock;
00096
00103 template<typename T> class ThreadLocalDataHolder {
00104 public:
00105 inline ThreadLocalDataHolder() :
00106 _key(TlsAlloc()) {};
00107
00108 inline ~ThreadLocalDataHolder() { TlsFree(_key); };
00109
00115 inline T* get() const {
00116 return (T*)TlsGetValue(_key);
00117 };
00118
00125 inline T* operator->() const { return get(); };
00126
00132 inline T& operator*() const { return *get(); };
00133
00140 inline T* release() {
00141 T* result = (T*)TlsGetValue(_key);
00142 TlsSetValue(_key, NULL);
00143 return result;
00144 };
00145
00152 inline void reset(T* p = NULL) {
00153 T* thing = (T*)TlsGetValue(_key);
00154 delete thing;
00155 TlsSetValue(_key, p);
00156 };
00157
00158 private:
00159 DWORD _key;
00160 };
00161 }
00162 }
00163 #endif