PThreads.hh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * PThreads.hh
  3. *
  4. * Copyright 2002, Emiliano Martin emilianomc@terra.es All rights reserved.
  5. *
  6. * See the COPYING file for the terms of usage and distribution.
  7. */
  8. #ifndef _LOG4CPP_THREADING_PTHREADS_HH
  9. #define _LOG4CPP_THREADING_PTHREADS_HH
  10. #include <log4cpp/Portability.hh>
  11. #include <stdio.h>
  12. #include <pthread.h>
  13. #include <string>
  14. #include <assert.h>
  15. LOG4CPP_NS_BEGIN
  16. namespace threading {
  17. /**
  18. * returns the thread ID
  19. **/
  20. std::string getThreadId();
  21. /**
  22. **/
  23. #if defined(VXWORKS)
  24. class Mutex {
  25. private:
  26. SEM_ID m_sem;
  27. public:
  28. inline Mutex() {
  29. m_sem = semMCreate( SEM_Q_PRIORITY | SEM_INVERSION_SAFE );
  30. }
  31. inline void lock() {
  32. STATUS result = semTake( m_sem, WAIT_FOREVER );
  33. if (result != OK) {
  34. // throw here?
  35. }
  36. }
  37. inline void unlock() {
  38. semGive( m_sem );
  39. }
  40. inline ~Mutex() {
  41. semDelete( m_sem );
  42. }
  43. private:
  44. Mutex(const Mutex& m);
  45. Mutex& operator=(const Mutex &m);
  46. };
  47. #else
  48. class Mutex {
  49. private:
  50. pthread_mutexattr_t mutexattr;
  51. pthread_mutex_t mutex;
  52. public:
  53. inline Mutex() {
  54. ::pthread_mutexattr_init(&mutexattr);
  55. ::pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
  56. ::pthread_mutex_init(&mutex, &mutexattr);
  57. }
  58. inline void lock() {
  59. ::pthread_mutex_lock(&mutex);
  60. }
  61. inline void unlock() {
  62. ::pthread_mutex_unlock(&mutex);
  63. }
  64. inline ~Mutex() {
  65. ::pthread_mutex_destroy(&mutex);
  66. ::pthread_mutexattr_destroy(&mutexattr);
  67. }
  68. private:
  69. Mutex(const Mutex& m);
  70. Mutex& operator=(const Mutex &m);
  71. };
  72. #endif
  73. /**
  74. * definition of ScopedLock;
  75. **/
  76. class ScopedLock {
  77. private:
  78. Mutex& _mutex;
  79. public:
  80. inline ScopedLock(Mutex& mutex) :
  81. _mutex(mutex) {
  82. _mutex.lock();
  83. }
  84. inline ~ScopedLock() {
  85. _mutex.unlock();
  86. }
  87. };
  88. /**
  89. *
  90. **/
  91. template<typename T> class ThreadLocalDataHolder {
  92. private:
  93. pthread_key_t _key;
  94. public:
  95. typedef T data_type;
  96. inline ThreadLocalDataHolder() {
  97. ::pthread_key_create(&_key, freeHolder);
  98. }
  99. inline static void freeHolder(void *p) {
  100. assert(p != NULL);
  101. delete reinterpret_cast<T *>(p);
  102. }
  103. inline ~ThreadLocalDataHolder() {
  104. T *data = get();
  105. if (data != NULL) {
  106. delete data;
  107. }
  108. ::pthread_key_delete(_key);
  109. }
  110. inline T* get() const {
  111. return reinterpret_cast<T *>(::pthread_getspecific(_key));
  112. }
  113. inline T* operator->() const { return get(); }
  114. inline T& operator*() const { return *get(); }
  115. inline T* release() {
  116. T* result = get();
  117. ::pthread_setspecific(_key, NULL);
  118. return result;
  119. }
  120. inline void reset(T* p = NULL) {
  121. T *data = get();
  122. if (data != NULL) {
  123. delete data;
  124. }
  125. ::pthread_setspecific(_key, p);
  126. }
  127. };
  128. }
  129. LOG4CPP_NS_END
  130. #endif