log4cpp  1.1.6
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 _LOG4CPP_THREADING_MSTHREADS_HH
11 #define _LOG4CPP_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 LOG4CPP_UNDEFINE_NOGDI
22 #endif
23 
24 #ifndef WIN32_LEAN_AND_MEAN
25 #define WIN32_LEAN_AND_MEAN
26 #define LOG4CPP_UNDEFINE_WIN32_LEAN_AND_MEAN
27 #endif
28 
29 #include <windows.h>
30 
31 #ifdef LOG4CPP_UNDEFINE_NOGDI
32 #undef NOGDI
33 #endif
34 
35 #ifdef LOG4CPP_UNDEFINE_WIN32_LEAN_AND_MEAN
36 #undef WIN32_LEAN_AND_MEAN
37 #endif
38 
39 #endif // done dealing with ERROR #define
40 
41 namespace log4cpp {
42  namespace threading {
48  std::string getThreadId();
49 
54  public:
55  MSMutex() {
56  InitializeCriticalSection(&_criticalSection);
57  }
59  DeleteCriticalSection(&_criticalSection);
60  }
61  inline LPCRITICAL_SECTION getCriticalSection() {
62  return &_criticalSection;
63  }
64 
65  private:
66  MSMutex(const MSMutex& other);
67  CRITICAL_SECTION _criticalSection;
68  };
69 
73  typedef MSMutex Mutex;
74 
79  class MSScopedLock {
80  public:
82  _criticalSection = mutex.getCriticalSection();
83  EnterCriticalSection(_criticalSection);
84  }
85 
87  LeaveCriticalSection(_criticalSection);
88  }
89 
90  private:
91  MSScopedLock(const MSScopedLock& other);
92  LPCRITICAL_SECTION _criticalSection;
93  };
94 
99  typedef MSScopedLock ScopedLock;
100 
107  template <typename T> class ThreadLocalDataHolder {
108  public:
109  inline ThreadLocalDataHolder() : _key(TlsAlloc()) {};
110 
112  TlsFree(_key);
113  };
114 
120  inline T* get() const {
121  return (T*)TlsGetValue(_key);
122  };
123 
130  inline T* operator->() const {
131  return get();
132  };
133 
139  inline T& operator*() const {
140  return *get();
141  };
142 
149  inline T* release() {
150  T* result = (T*)TlsGetValue(_key);
151  TlsSetValue(_key, NULL);
152  return result;
153  };
154 
161  inline void reset(T* p = NULL) {
162  T* thing = (T*)TlsGetValue(_key);
163  delete thing;
164  TlsSetValue(_key, p);
165  };
166 
167  private:
168  DWORD _key;
169  };
170  } // namespace threading
171 } // namespace log4cpp
172 #endif
~ThreadLocalDataHolder()
Definition: MSThreads.hh:111
MSScopedLock(MSMutex &mutex)
Definition: MSThreads.hh:81
A simple object wrapper around WaitForSingleObject() and ReleaseMutex()
Definition: MSThreads.hh:79
ThreadLocalDataHolder()
Definition: MSThreads.hh:109
T * release()
Releases the Object held for the current thread.
Definition: MSThreads.hh:149
#define LOG4CPP_EXPORT
Definition: Export.hh:26
The top level namespace for all &#39;Log for C++&#39; types and classes.
Definition: AbortAppender.hh:16
boost::mutex Mutex
Dummy type &#39;int&#39; for Mutex.
Definition: BoostThreads.hh:27
void reset(T *p=NULL)
Sets a new Object to be held for the current thread.
Definition: MSThreads.hh:161
MSMutex()
Definition: MSThreads.hh:55
T * operator->() const
Obtains the Object held for the current thread.
Definition: MSThreads.hh:130
static std::string getThreadId()
Return an identifier for the current thread.
Definition: BoostThreads.hh:22
A simple object wrapper around CreateMutex() and DeleteMutex()
Definition: MSThreads.hh:53
boost::mutex::scoped_lock ScopedLock
Dummy type &#39;int&#39; defintion of ScopedLock;.
Definition: BoostThreads.hh:30
~MSScopedLock()
Definition: MSThreads.hh:86
LPCRITICAL_SECTION getCriticalSection()
Definition: MSThreads.hh:61
~MSMutex()
Definition: MSThreads.hh:58
T & operator*() const
Obtains the Object held for the current thread.
Definition: MSThreads.hh:139