MSThreads.hh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef _LOG4CPP_THREADING_MSTHREADS_HH
  10. #define _LOG4CPP_THREADING_MSTHREADS_HH
  11. #include <string>
  12. // deal with ERROR #define
  13. // N.B. This #includes windows.h with NOGDI and WIN32_LEAN_AND_MEAN #defined.
  14. // If this is not what the user wants, #include windows.h before this file.
  15. #ifndef _WINDOWS_
  16. # ifndef NOGDI
  17. # define NOGDI // this will circumvent the ERROR #define in windows.h
  18. # define LOG4CPP_UNDEFINE_NOGDI
  19. # endif
  20. # ifndef WIN32_LEAN_AND_MEAN
  21. # define WIN32_LEAN_AND_MEAN
  22. # define LOG4CPP_UNDEFINE_WIN32_LEAN_AND_MEAN
  23. # endif
  24. # include <windows.h>
  25. # ifdef LOG4CPP_UNDEFINE_NOGDI
  26. # undef NOGDI
  27. # endif
  28. # ifdef LOG4CPP_UNDEFINE_WIN32_LEAN_AND_MEAN
  29. # undef WIN32_LEAN_AND_MEAN
  30. # endif
  31. #endif // done dealing with ERROR #define
  32. LOG4CPP_NS_BEGIN
  33. namespace threading {
  34. /**
  35. * Return an identifier for the current thread. What these
  36. * identifiers look like is completely up to the underlying
  37. * thread library.
  38. **/
  39. std::string getThreadId();
  40. /**
  41. * A simple object wrapper around CreateMutex() and DeleteMutex()
  42. */
  43. class LOG4CPP_EXPORT MSMutex {
  44. public:
  45. MSMutex() { InitializeCriticalSection(&_criticalSection); }
  46. ~MSMutex() { DeleteCriticalSection(&_criticalSection); }
  47. inline LPCRITICAL_SECTION getCriticalSection() {
  48. return &_criticalSection;
  49. }
  50. private:
  51. MSMutex(const MSMutex& other);
  52. CRITICAL_SECTION _criticalSection;
  53. };
  54. /**
  55. * A simple, non recursive Mutex.
  56. **/
  57. typedef MSMutex Mutex;
  58. /**
  59. * A simple object wrapper around WaitForSingleObject() and
  60. * ReleaseMutex()
  61. */
  62. class MSScopedLock {
  63. public:
  64. MSScopedLock(MSMutex& mutex) {
  65. _criticalSection = mutex.getCriticalSection();
  66. EnterCriticalSection(_criticalSection);
  67. }
  68. ~MSScopedLock() { LeaveCriticalSection(_criticalSection); }
  69. private:
  70. MSScopedLock(const MSScopedLock& other);
  71. LPCRITICAL_SECTION _criticalSection;
  72. };
  73. /**
  74. * A simple "resource acquisition is initialization" idiom type lock
  75. * for Mutex.
  76. **/
  77. typedef MSScopedLock ScopedLock;
  78. /**
  79. * This class holds Thread local data of type T, i.e. for each
  80. * thread a ThreadLocalDataHolder holds 0 or 1 instance of T.
  81. * The held object must be heap allocated and will be deleted
  82. * upon termination of the thread to which it belongs.
  83. **/
  84. template<typename T> class ThreadLocalDataHolder {
  85. public:
  86. inline ThreadLocalDataHolder() :
  87. _key(TlsAlloc()) {};
  88. inline ~ThreadLocalDataHolder() {
  89. TlsFree(_key);
  90. };
  91. /**
  92. * Obtains the Object held for the current thread.
  93. * @return a pointer to the held Object or NULL if no
  94. * Object has been set for the current thread.
  95. **/
  96. inline T* get() const {
  97. return (T*)TlsGetValue(_key);
  98. };
  99. /**
  100. * Obtains the Object held for the current thread.
  101. * Initially each thread holds NULL.
  102. * @return a pointer to the held Object or NULL if no
  103. * Object has been set for the current thread.
  104. **/
  105. inline T* operator->() const { return get(); };
  106. /**
  107. * Obtains the Object held for the current thread.
  108. * @pre get() != NULL
  109. * @return a reference to the held Object.
  110. **/
  111. inline T& operator*() const { return *get(); };
  112. /**
  113. * Releases the Object held for the current thread.
  114. * @post get() == NULL
  115. * @return a pointer to the Object thas was held for
  116. * the current thread or NULL if no Object was held.
  117. **/
  118. inline T* release() {
  119. T* result = (T*)TlsGetValue(_key);
  120. TlsSetValue(_key, NULL);
  121. return result;
  122. };
  123. /**
  124. * Sets a new Object to be held for the current thread. A
  125. * previously set Object will be deleted.
  126. * @param p the new object to hold.
  127. * @post get() == p
  128. **/
  129. inline void reset(T* p = NULL) {
  130. T* thing = (T*)TlsGetValue(_key);
  131. delete thing;
  132. TlsSetValue(_key, p);
  133. };
  134. private:
  135. DWORD _key;
  136. };
  137. }
  138. LOG4CPP_NS_END
  139. #endif