ThreadPool.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_THREAD_POOL_HPP
  26. #define CAT_THREAD_POOL_HPP
  27. #include <cat/Singleton.hpp>
  28. #include <cat/threads/Mutex.hpp>
  29. #include <cat/crypt/tunnel/KeyAgreement.hpp>
  30. #include <cat/threads/RegionAllocator.hpp>
  31. #include <cat/io/AsyncBuffer.hpp>
  32. #include <cat/threads/Thread.hpp>
  33. #include <cat/threads/WaitableFlag.hpp>
  34. #if defined(CAT_OS_WINDOWS)
  35. # include <cat/port/WindowsInclude.hpp>
  36. #endif
  37. namespace cat {
  38. // Reference Object priorities
  39. enum RefObjectPriorities
  40. {
  41. REFOBJ_PRIO_0,
  42. REFOBJ_PRIO_COUNT = 32,
  43. };
  44. /*
  45. class ThreadRefObject
  46. Base class for any thread-safe reference-counted thread pool object
  47. Designed this way so that all of these objects can be automatically deleted
  48. */
  49. class ThreadRefObject
  50. {
  51. friend class ThreadPool;
  52. ThreadRefObject *last, *next;
  53. int _priorityLevel;
  54. volatile u32 _refCount;
  55. public:
  56. ThreadRefObject(int priorityLevel);
  57. CAT_INLINE virtual ~ThreadRefObject() {}
  58. public:
  59. void AddRef();
  60. void ReleaseRef();
  61. // Safe release -- If not null, then releases and sets to null
  62. template<class T>
  63. static CAT_INLINE void SafeRelease(T * &object)
  64. {
  65. if (object)
  66. {
  67. object->ReleaseRef();
  68. object = 0;
  69. }
  70. }
  71. };
  72. // Auto release for ThreadRefObject references
  73. template<class T>
  74. class AutoRef
  75. {
  76. T *_ref;
  77. public:
  78. CAT_INLINE AutoRef(T *ref = 0) throw() { _ref = ref; }
  79. CAT_INLINE ~AutoRef() throw() { ThreadRefObject::SafeRelease(_ref); }
  80. CAT_INLINE AutoRef &operator=(T *ref) throw() { Reset(ref); return *this; }
  81. CAT_INLINE T *Get() throw() { return _ref; }
  82. CAT_INLINE T *operator->() throw() { return _ref; }
  83. CAT_INLINE T &operator*() throw() { return *_ref; }
  84. CAT_INLINE operator T*() { return _ref; }
  85. CAT_INLINE void Forget() throw() { _ref = 0; }
  86. CAT_INLINE void Reset(T *ref = 0) throw() { ThreadRefObject::SafeRelease(_ref); _ref = ref; }
  87. };
  88. //// TLS
  89. class ThreadPoolLocalStorage
  90. {
  91. public:
  92. BigTwistedEdwards *math;
  93. FortunaOutput *csprng;
  94. ThreadPoolLocalStorage();
  95. ~ThreadPoolLocalStorage();
  96. bool Valid();
  97. };
  98. //// Shutdown
  99. class ShutdownWait;
  100. class ShutdownObserver;
  101. class ShutdownWait
  102. {
  103. friend class ShutdownObserver;
  104. WaitableFlag _kill_flag;
  105. ShutdownObserver *_observer;
  106. void OnShutdownDone();
  107. public:
  108. // Priority number must be higher than users'
  109. ShutdownWait(int priorityLevel);
  110. /*virtual*/ ~ShutdownWait();
  111. CAT_INLINE ShutdownObserver *GetObserver() { return _observer; }
  112. bool WaitForShutdown(u32 milliseconds);
  113. };
  114. class ShutdownObserver : public ThreadRefObject
  115. {
  116. friend class ShutdownWait;
  117. ShutdownWait *_wait;
  118. private:
  119. ShutdownObserver(int priorityLevel, ShutdownWait *wait);
  120. ~ShutdownObserver();
  121. };
  122. //// ThreadPoolWorker
  123. class ThreadPoolWorker : public Thread
  124. {
  125. public:
  126. virtual bool ThreadFunction(void *port);
  127. };
  128. #if defined(CAT_OS_WINDOWS)
  129. typedef HANDLE ThreadPoolHandle;
  130. #else
  131. typedef int ThreadPoolHandle;
  132. #endif
  133. /*
  134. class ThreadPool
  135. Startup() : Call to start up the thread pool
  136. Shutdown() : Call to destroy the thread pool and objects
  137. */
  138. class ThreadPool : public Singleton<ThreadPool>
  139. {
  140. friend class ThreadRefObject;
  141. CAT_SINGLETON(ThreadPool);
  142. #if defined(CAT_OS_WINDOWS)
  143. HANDLE _port;
  144. #endif
  145. int _processor_count, _active_thread_count;
  146. static const int MAX_THREADS = 256;
  147. ThreadPoolWorker _threads[MAX_THREADS];
  148. // Track sockets for graceful termination
  149. Mutex _objectRefLock[REFOBJ_PRIO_COUNT];
  150. ThreadRefObject *_objectRefHead[REFOBJ_PRIO_COUNT];
  151. protected:
  152. void TrackObject(ThreadRefObject *object);
  153. void UntrackObject(ThreadRefObject *object);
  154. bool SpawnThread();
  155. bool SpawnThreads();
  156. public:
  157. bool Startup();
  158. void Shutdown();
  159. bool Associate(ThreadPoolHandle h, ThreadRefObject *key);
  160. int GetProcessorCount() { return _processor_count; }
  161. int GetThreadCount() { return _active_thread_count; }
  162. };
  163. } // namespace cat
  164. #endif // CAT_THREAD_POOL_HPP
粤ICP备19079148号