MSThreads.hh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. namespace log4cpp {
  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() { TlsFree(_key); };
  89. /**
  90. * Obtains the Object held for the current thread.
  91. * @return a pointer to the held Object or NULL if no
  92. * Object has been set for the current thread.
  93. **/
  94. inline T* get() const {
  95. return (T*)TlsGetValue(_key);
  96. };
  97. /**
  98. * Obtains the Object held for the current thread.
  99. * Initially each thread holds NULL.
  100. * @return a pointer to the held Object or NULL if no
  101. * Object has been set for the current thread.
  102. **/
  103. inline T* operator->() const { return get(); };
  104. /**
  105. * Obtains the Object held for the current thread.
  106. * @pre get() != NULL
  107. * @return a reference to the held Object.
  108. **/
  109. inline T& operator*() const { return *get(); };
  110. /**
  111. * Releases the Object held for the current thread.
  112. * @post get() == NULL
  113. * @return a pointer to the Object thas was held for
  114. * the current thread or NULL if no Object was held.
  115. **/
  116. inline T* release() {
  117. T* result = (T*)TlsGetValue(_key);
  118. TlsSetValue(_key, NULL);
  119. return result;
  120. };
  121. /**
  122. * Sets a new Object to be held for the current thread. A
  123. * previously set Object will be deleted.
  124. * @param p the new object to hold.
  125. * @post get() == p
  126. **/
  127. inline void reset(T* p = NULL) {
  128. T* thing = (T*)TlsGetValue(_key);
  129. delete thing;
  130. TlsSetValue(_key, p);
  131. };
  132. private:
  133. DWORD _key;
  134. };
  135. }
  136. }
  137. #endif