Loading...
Searching...
No Matches
MSThreads.hh
Go to the documentation of this file.
/*
* MSThreads.hh
*
* Copyright 2002, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
* Copyright 2002, Bastiaan Bakker. All rights reserved.
*
* See the COPYING file for the terms of usage and distribution.
*/
#ifndef _LOG4SHIB_THREADING_MSTHREADS_HH
#define _LOG4SHIB_THREADING_MSTHREADS_HH
#include <string>
// deal with ERROR #define
// N.B. This #includes windows.h with NOGDI and WIN32_LEAN_AND_MEAN #defined.
// If this is not what the user wants, #include windows.h before this file.
#ifndef _WINDOWS_
# ifndef NOGDI
# define NOGDI // this will circumvent the ERROR #define in windows.h
# define LOG4SHIB_UNDEFINE_NOGDI
# endif
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# define LOG4SHIB_UNDEFINE_WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# ifdef LOG4SHIB_UNDEFINE_NOGDI
# undef NOGDI
# endif
# ifdef LOG4SHIB_UNDEFINE_WIN32_LEAN_AND_MEAN
# undef WIN32_LEAN_AND_MEAN
# endif
#endif // done dealing with ERROR #define
namespace log4shib {
namespace threading {
std::string getThreadId();
class LOG4SHIB_EXPORT MSMutex {
public:
MSMutex() { InitializeCriticalSection(&_criticalSection); }
~MSMutex() { DeleteCriticalSection(&_criticalSection); }
inline LPCRITICAL_SECTION getCriticalSection() {
return &_criticalSection;
}
private:
MSMutex(const MSMutex& other);
CRITICAL_SECTION _criticalSection;
};
typedef MSMutex Mutex;
class MSScopedLock {
public:
MSScopedLock(MSMutex& mutex) {
_criticalSection = mutex.getCriticalSection();
EnterCriticalSection(_criticalSection);
}
~MSScopedLock() { LeaveCriticalSection(_criticalSection); }
private:
MSScopedLock(const MSScopedLock& other);
LPCRITICAL_SECTION _criticalSection;
};
typedef MSScopedLock ScopedLock;
template<typename T> class ThreadLocalDataHolder {
public:
inline ThreadLocalDataHolder() :
_key(TlsAlloc()) {};
inline ~ThreadLocalDataHolder() { TlsFree(_key); };
inline T* get() const {
return (T*)TlsGetValue(_key);
};
inline T* operator->() const { return get(); };
inline T& operator*() const { return *get(); };
inline T* release() {
T* result = (T*)TlsGetValue(_key);
TlsSetValue(_key, NULL);
return result;
};
inline void reset(T* p = NULL) {
T* thing = (T*)TlsGetValue(_key);
delete thing;
TlsSetValue(_key, p);
};
private:
DWORD _key;
};
}
}
#endif
LOG4SHIB_EXPORT
#define LOG4SHIB_EXPORT
Definition Export.hh:11
log4shib::threading::MSMutex
A simple object wrapper around CreateMutex() and DeleteMutex().
Definition MSThreads.hh:53
log4shib::threading::MSMutex::MSMutex
MSMutex()
Definition MSThreads.hh:55
log4shib::threading::MSMutex::~MSMutex
~MSMutex()
Definition MSThreads.hh:56
log4shib::threading::MSMutex::getCriticalSection
LPCRITICAL_SECTION getCriticalSection()
Definition MSThreads.hh:57
log4shib::threading::MSScopedLock
A simple object wrapper around WaitForSingleObject() and ReleaseMutex().
Definition MSThreads.hh:75
log4shib::threading::MSScopedLock::MSScopedLock
MSScopedLock(MSMutex &mutex)
Definition MSThreads.hh:77
log4shib::threading::MSScopedLock::~MSScopedLock
~MSScopedLock()
Definition MSThreads.hh:82
log4shib::threading::ThreadLocalDataHolder::operator->
T * operator->() const
Obtains the Object held for the current thread.
Definition MSThreads.hh:123
log4shib::threading::ThreadLocalDataHolder::operator*
T & operator*() const
Obtains the Object held for the current thread.
Definition MSThreads.hh:130
log4shib::threading::ThreadLocalDataHolder::~ThreadLocalDataHolder
~ThreadLocalDataHolder()
Definition MSThreads.hh:106
log4shib::threading::ThreadLocalDataHolder::reset
void reset(T *p=NULL)
Sets a new Object to be held for the current thread.
Definition MSThreads.hh:150
log4shib::threading::ThreadLocalDataHolder::get
T * get() const
Obtains the Object held for the current thread.
Definition MSThreads.hh:113
log4shib::threading::ThreadLocalDataHolder::release
T * release()
Releases the Object held for the current thread.
Definition MSThreads.hh:138
log4shib::threading::ThreadLocalDataHolder::ThreadLocalDataHolder
ThreadLocalDataHolder()
Definition MSThreads.hh:103
log4shib::threading::getThreadId
static std::string getThreadId()
Return an identifier for the current thread.
Definition BoostThreads.hh:22
log4shib::threading::Mutex
boost::mutex Mutex
Dummy type 'int' for Mutex.
Definition BoostThreads.hh:29
log4shib::threading::ScopedLock
boost::mutex::scoped_lock ScopedLock
Dummy type 'int' defintion of ScopedLock;.
Definition BoostThreads.hh:30
log4shib
The top level namespace for all 'Log for C++' types and classes.
Definition AbortAppender.hh:16