log4cpp  1.1.3
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() { 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:
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:
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
#define LOG4CPP_EXPORT
Definition: Export.hh:26
A simple object wrapper around CreateMutex() and DeleteMutex()
Definition: MSThreads.hh:53
LPCRITICAL_SECTION getCriticalSection()
Definition: MSThreads.hh:57
MSMutex()
Definition: MSThreads.hh:55
~MSMutex()
Definition: MSThreads.hh:56
A simple object wrapper around WaitForSingleObject() and ReleaseMutex()
Definition: MSThreads.hh:75
MSScopedLock(MSMutex &mutex)
Definition: MSThreads.hh:77
~MSScopedLock()
Definition: MSThreads.hh:82
T * get() const
Obtains the Object held for the current thread.
Definition: MSThreads.hh:113
T * release()
Releases the Object held for the current thread.
Definition: MSThreads.hh:138
~ThreadLocalDataHolder()
Definition: MSThreads.hh:106
T & operator*() const
Obtains the Object held for the current thread.
Definition: MSThreads.hh:130
ThreadLocalDataHolder()
Definition: MSThreads.hh:103
void reset(T *p=NULL)
Sets a new Object to be held for the current thread.
Definition: MSThreads.hh:150
T * operator->() const
Obtains the Object held for the current thread.
Definition: MSThreads.hh:123
boost::mutex Mutex
Dummy type 'int' for Mutex.
Definition: BoostThreads.hh:27
boost::mutex::scoped_lock ScopedLock
Dummy type 'int' defintion of ScopedLock;.
Definition: BoostThreads.hh:30
static std::string getThreadId()
Return an identifier for the current thread.
Definition: BoostThreads.hh:22
The top level namespace for all 'Log for C++' types and classes.
Definition: AbortAppender.hh:16