OmniThreads.hh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * OmniThreads.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_OMNITHREADS_HH
  10. #define _LOG4CPP_THREADING_OMNITHREADS_HH
  11. #include <log4cpp/Portability.hh>
  12. #include <omnithread.h>
  13. #include <stdio.h>
  14. #include <string>
  15. LOG4CPP_NS_BEGIN
  16. namespace threading {
  17. /**
  18. * Return an identifier for the current thread. What these
  19. * identifiers look like is completely up to the underlying
  20. * thread library. OmniThreads returns the POSIX thread Id.
  21. **/
  22. std::string getThreadId();
  23. /**
  24. * A simple, non recursive Mutex.
  25. * Equivalent to Boost.Threads boost::mutex
  26. **/
  27. typedef omni_mutex Mutex;
  28. /**
  29. * A simple "resource acquisition is initialization" idiom type lock
  30. * for Mutex.
  31. * Equivalent to Boost.Threads boost::scoped_lock.
  32. **/
  33. typedef omni_mutex_lock ScopedLock;
  34. /**
  35. * This class holds Thread local data of type T, i.e. for each
  36. * thread a ThreadLocalDataHolder holds 0 or 1 instance of T.
  37. * The held object must be heap allocated and will be deleted
  38. * upon termination of the thread to wich it belongs.
  39. * This is an omni_threads based equivalent of Boost.Threads
  40. * thread_specific_ptr<T> class.
  41. **/
  42. template<typename T> class ThreadLocalDataHolder {
  43. public:
  44. typedef T data_type;
  45. inline ThreadLocalDataHolder() :
  46. _key(omni_thread::allocate_key()) {};
  47. inline ~ThreadLocalDataHolder() {};
  48. /**
  49. * Obtains the Object held for the current thread.
  50. * @return a pointer to the held Object or NULL if no
  51. * Object has been set for the current thread.
  52. **/
  53. inline T* get() const {
  54. Holder* holder = dynamic_cast<Holder*>(
  55. ::omni_thread::self()->get_value(_key));
  56. return (holder) ? holder->data : NULL;
  57. };
  58. /**
  59. * Obtains the Object held for the current thread.
  60. * Initially each thread holds NULL.
  61. * @return a pointer to the held Object or NULL if no
  62. * Object has been set for the current thread.
  63. **/
  64. inline T* operator->() const { return get(); };
  65. /**
  66. * Obtains the Object held for the current thread.
  67. * @pre get() != NULL
  68. * @return a reference to the held Object.
  69. **/
  70. inline T& operator*() const { return *get(); };
  71. /**
  72. * Releases the Object held for the current thread.
  73. * @post get() == NULL
  74. * @return a pointer to the Object thas was held for
  75. * the current thread or NULL if no Object was held.
  76. **/
  77. inline T* release() {
  78. T* result = NULL;
  79. Holder* holder = dynamic_cast<Holder*>(
  80. ::omni_thread::self()->get_value(_key));
  81. if (holder) {
  82. result = holder->data;
  83. holder->data = NULL;
  84. }
  85. return result;
  86. };
  87. /**
  88. * Sets a new Object to be held for the current thread. A
  89. * previously set Object will be deleted.
  90. * @param p the new object to hold.
  91. * @post get() == p
  92. **/
  93. inline void reset(T* p = NULL) {
  94. Holder* holder = dynamic_cast<Holder*>(
  95. ::omni_thread::self()->get_value(_key));
  96. if (holder) {
  97. if (holder->data)
  98. delete holder->data;
  99. holder->data = p;
  100. }
  101. else {
  102. holder = new Holder(p);
  103. ::omni_thread::self()->set_value(_key, holder);
  104. }
  105. };
  106. private:
  107. class Holder : public omni_thread::value_t {
  108. public:
  109. Holder(data_type* data) : data(data) {};
  110. virtual ~Holder() { if (data) delete (data); };
  111. data_type* data;
  112. private:
  113. Holder(const Holder& other);
  114. Holder& operator=(const Holder& other);
  115. };
  116. omni_thread::key_t _key;
  117. };
  118. }
  119. LOG4CPP_NS_END
  120. #endif