MSThreads.hh
Go to the documentation of this file.
00001 /*
00002  * MSThreads.hh
00003  *
00004  * Copyright 2002, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
00005  * Copyright 2002, Bastiaan Bakker. All rights reserved.
00006  *
00007  * See the COPYING file for the terms of usage and distribution.
00008  */
00009 
00010 #ifndef _LOG4CPP_THREADING_MSTHREADS_HH
00011 #define _LOG4CPP_THREADING_MSTHREADS_HH
00012 
00013 #include <string>
00014 
00015 // deal with ERROR #define
00016 // N.B. This #includes windows.h with NOGDI and WIN32_LEAN_AND_MEAN #defined.
00017 //      If this is not what the user wants, #include windows.h before this file.
00018 #ifndef _WINDOWS_
00019 #  ifndef NOGDI
00020 #    define NOGDI  // this will circumvent the ERROR #define in windows.h
00021 #    define LOG4CPP_UNDEFINE_NOGDI
00022 #  endif
00023 
00024 #  ifndef WIN32_LEAN_AND_MEAN
00025 #    define WIN32_LEAN_AND_MEAN
00026 #    define LOG4CPP_UNDEFINE_WIN32_LEAN_AND_MEAN
00027 #  endif
00028 
00029 #  include <windows.h>
00030 
00031 #  ifdef LOG4CPP_UNDEFINE_NOGDI
00032 #    undef NOGDI
00033 #  endif
00034 
00035 #  ifdef LOG4CPP_UNDEFINE_WIN32_LEAN_AND_MEAN
00036 #    undef WIN32_LEAN_AND_MEAN
00037 #  endif
00038 
00039 #endif // done dealing with ERROR #define
00040 
00041 namespace log4cpp {
00042     namespace threading {
00048         LOG4CPP_EXPORT std::string getThreadId();
00057         LOG4CPP_EXPORT char* getThreadId(char* buffer);
00058 
00062         class LOG4CPP_EXPORT MSMutex {
00063             public:
00064             MSMutex() { InitializeCriticalSection(&_criticalSection); }
00065             ~MSMutex() { DeleteCriticalSection(&_criticalSection); }
00066             inline LPCRITICAL_SECTION getCriticalSection() {
00067                 return &_criticalSection;
00068             }
00069 
00070             private:
00071             MSMutex(const MSMutex& other);
00072             CRITICAL_SECTION _criticalSection;
00073         };
00074 
00078         typedef MSMutex Mutex;
00079 
00084         class MSScopedLock {
00085             public:
00086             MSScopedLock(MSMutex& mutex) {
00087                 _criticalSection = mutex.getCriticalSection();
00088                 EnterCriticalSection(_criticalSection);
00089             }
00090 
00091             ~MSScopedLock() { LeaveCriticalSection(_criticalSection); }
00092 
00093             private:
00094             MSScopedLock(const MSScopedLock& other);
00095             LPCRITICAL_SECTION _criticalSection;
00096         };
00097 
00102         typedef MSScopedLock ScopedLock;
00103 
00110         template<typename T> class ThreadLocalDataHolder {
00111             public:
00112             inline ThreadLocalDataHolder() :
00113                 _key(TlsAlloc()) {};
00114 
00115             inline ~ThreadLocalDataHolder() { TlsFree(_key); };
00116             
00122             inline T* get() const {
00123                 return (T*)TlsGetValue(_key);
00124             };
00125 
00132             inline T* operator->() const { return get(); };
00133 
00139             inline T& operator*() const { return *get(); };
00140 
00147             inline T* release() {
00148                 T* result = (T*)TlsGetValue(_key);
00149                 TlsSetValue(_key, NULL);
00150                 return result;
00151             };
00152 
00159             inline void reset(T* p = NULL) {
00160                 T* thing = (T*)TlsGetValue(_key);
00161                 delete thing;
00162                 TlsSetValue(_key, p);
00163             };
00164 
00165             private:            
00166             DWORD _key;            
00167         };
00168     }
00169 }
00170 #endif


log4cpp
Author(s): Stephen Roderick
autogenerated on Mon Oct 6 2014 03:13:14