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  LOG4CPP_EXPORT std::string getThreadId();
57  LOG4CPP_EXPORT char* getThreadId(char* buffer);
58 
63  public:
64  MSMutex() { InitializeCriticalSection(&_criticalSection); }
65  ~MSMutex() { DeleteCriticalSection(&_criticalSection); }
66  inline LPCRITICAL_SECTION getCriticalSection() {
67  return &_criticalSection;
68  }
69 
70  private:
71  MSMutex(const MSMutex& other);
72  CRITICAL_SECTION _criticalSection;
73  };
74 
78  typedef MSMutex Mutex;
79 
84  class MSScopedLock {
85  public:
87  _criticalSection = mutex.getCriticalSection();
88  EnterCriticalSection(_criticalSection);
89  }
90 
91  ~MSScopedLock() { LeaveCriticalSection(_criticalSection); }
92 
93  private:
94  MSScopedLock(const MSScopedLock& other);
95  LPCRITICAL_SECTION _criticalSection;
96  };
97 
102  typedef MSScopedLock ScopedLock;
103 
110  template<typename T> class ThreadLocalDataHolder {
111  public:
113  _key(TlsAlloc()) {};
114 
115  inline ~ThreadLocalDataHolder() { TlsFree(_key); };
116 
122  inline T* get() const {
123  return (T*)TlsGetValue(_key);
124  };
125 
132  inline T* operator->() const { return get(); };
133 
139  inline T& operator*() const { return *get(); };
140 
147  inline T* release() {
148  T* result = (T*)TlsGetValue(_key);
149  TlsSetValue(_key, NULL);
150  return result;
151  };
152 
159  inline void reset(T* p = NULL) {
160  T* thing = (T*)TlsGetValue(_key);
161  delete thing;
162  TlsSetValue(_key, p);
163  };
164 
165  private:
166  DWORD _key;
167  };
168  }
169 }
170 #endif
static std::string getThreadId()
Definition: BoostThreads.hh:22
LPCRITICAL_SECTION _criticalSection
Definition: MSThreads.hh:95
CRITICAL_SECTION _criticalSection
Definition: MSThreads.hh:72
#define LOG4CPP_EXPORT
Definition: Export.hh:26
boost::mutex Mutex
Definition: BoostThreads.hh:37
boost::mutex::scoped_lock ScopedLock
Definition: BoostThreads.hh:40
LPCRITICAL_SECTION getCriticalSection()
Definition: MSThreads.hh:66


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:14:17