log4shib 2.0.0

MSThreads.hh
Go to the documentation of this file.
1/*
2 * MSThreads.hh
3 *
4 * Copyright 2002, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
5 * Copyright 2002, Bastiaan Bakker. All rights reserved.
6 *
7 * See the COPYING file for the terms of usage and distribution.
8 */
9
10#ifndef _LOG4SHIB_THREADING_MSTHREADS_HH
11#define _LOG4SHIB_THREADING_MSTHREADS_HH
12
13#include <string>
14
15// deal with ERROR #define
16// N.B. This #includes windows.h with NOGDI and WIN32_LEAN_AND_MEAN #defined.
17// If this is not what the user wants, #include windows.h before this file.
18#ifndef _WINDOWS_
19# ifndef NOGDI
20# define NOGDI // this will circumvent the ERROR #define in windows.h
21# define LOG4SHIB_UNDEFINE_NOGDI
22# endif
23
24# ifndef WIN32_LEAN_AND_MEAN
25# define WIN32_LEAN_AND_MEAN
26# define LOG4SHIB_UNDEFINE_WIN32_LEAN_AND_MEAN
27# endif
28
29# include <windows.h>
30
31# ifdef LOG4SHIB_UNDEFINE_NOGDI
32# undef NOGDI
33# endif
34
35# ifdef LOG4SHIB_UNDEFINE_WIN32_LEAN_AND_MEAN
36# undef WIN32_LEAN_AND_MEAN
37# endif
38
39#endif // done dealing with ERROR #define
40
41 namespace log4shib {
42 namespace threading {
48 std::string getThreadId();
49
53 class LOG4SHIB_EXPORT MSMutex {
54 public:
55 MSMutex() { InitializeCriticalSection(&_criticalSection); }
56 ~MSMutex() { DeleteCriticalSection(&_criticalSection); }
57 inline LPCRITICAL_SECTION getCriticalSection() {
58 return &_criticalSection;
59 }
60
61 private:
62 MSMutex(const MSMutex& other);
63 CRITICAL_SECTION _criticalSection;
64 };
65
69 typedef MSMutex Mutex;
70
75 class MSScopedLock {
76 public:
77 MSScopedLock(MSMutex& mutex) {
78 _criticalSection = mutex.getCriticalSection();
79 EnterCriticalSection(_criticalSection);
80 }
81
82 ~MSScopedLock() { LeaveCriticalSection(_criticalSection); }
83
84 private:
85 MSScopedLock(const MSScopedLock& other);
86 LPCRITICAL_SECTION _criticalSection;
87 };
88
93 typedef MSScopedLock ScopedLock;
94
101 template<typename T> class ThreadLocalDataHolder {
102 public:
103 inline ThreadLocalDataHolder() :
104 _key(TlsAlloc()) {};
105
106 inline ~ThreadLocalDataHolder() { TlsFree(_key); };
107
113 inline T* get() const {
114 return (T*)TlsGetValue(_key);
115 };
116
123 inline T* operator->() const { return get(); };
124
130 inline T& operator*() const { return *get(); };
131
138 inline T* release() {
139 T* result = (T*)TlsGetValue(_key);
140 TlsSetValue(_key, NULL);
141 return result;
142 };
143
150 inline void reset(T* p = NULL) {
151 T* thing = (T*)TlsGetValue(_key);
152 delete thing;
153 TlsSetValue(_key, p);
154 };
155
156 private:
157 DWORD _key;
158 };
159 }
160}
161#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:27
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