llqueuedthread.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @file llqueuedthread.h
  3. * @brief
  4. *
  5. * $LicenseInfo:firstyear=2004&license=viewergpl$
  6. *
  7. * Copyright (c) 2004-2009, Linden Research, Inc. (c) 2022 Henri Beauchamp.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLQUEUEDTHREAD_H
  33. #define LL_LLQUEUEDTHREAD_H
  34. #include <map>
  35. #include <queue>
  36. #include <set>
  37. #include "llatomic.h"
  38. #include "hbfastmap.h"
  39. #include "llthread.h"
  40. // Note: ~LLQueuedThread is O(N) N=# of queued threads, assumed to be small
  41. // It is assumed that LLQueuedThreads are rarely created/destroyed.
  42. class LLQueuedThread : public LLThread
  43. {
  44. protected:
  45. LOG_CLASS(LLQueuedThread);
  46. public:
  47. enum priority_t
  48. {
  49. PRIORITY_IMMEDIATE = 0x7FFFFFFF,
  50. PRIORITY_URGENT = 0x40000000,
  51. PRIORITY_HIGH = 0x30000000,
  52. PRIORITY_NORMAL = 0x20000000,
  53. PRIORITY_LOW = 0x10000000,
  54. PRIORITY_LOWBITS = 0x0FFFFFFF,
  55. PRIORITY_HIGHBITS = 0x70000000
  56. };
  57. enum status_t
  58. {
  59. STATUS_EXPIRED = -1,
  60. STATUS_UNKNOWN = 0,
  61. STATUS_QUEUED = 1,
  62. STATUS_INPROGRESS = 2,
  63. STATUS_COMPLETE = 3,
  64. STATUS_ABORTED = 4,
  65. STATUS_DELETE = 5
  66. };
  67. enum flags_t
  68. {
  69. FLAG_AUTO_COMPLETE = 1,
  70. FLAG_AUTO_DELETE = 2, // Child-class dependent
  71. FLAG_ABORT = 4,
  72. };
  73. typedef U32 handle_t;
  74. class QueuedRequest
  75. {
  76. friend class LLQueuedThread;
  77. protected:
  78. LOG_CLASS(QueuedRequest);
  79. virtual ~QueuedRequest(); // Use deleteRequest()
  80. public:
  81. LL_INLINE QueuedRequest(handle_t handle, U32 priority, U32 flags = 0)
  82. : mStatus(STATUS_UNKNOWN),
  83. mHandle(handle),
  84. mPriority(priority),
  85. mFlags(flags)
  86. {
  87. }
  88. LL_INLINE status_t getStatus()
  89. {
  90. return mStatus;
  91. }
  92. LL_INLINE U32 getFlags() const
  93. {
  94. return mFlags;
  95. }
  96. LL_INLINE U32 getPriority() const
  97. {
  98. return mPriority;
  99. }
  100. LL_INLINE bool higherPriority(const QueuedRequest& second) const
  101. {
  102. if (mPriority == second.mPriority)
  103. {
  104. return mHandle < second.mHandle;
  105. }
  106. return mPriority > second.mPriority;
  107. }
  108. protected:
  109. LL_INLINE status_t setStatus(status_t newstatus)
  110. {
  111. status_t oldstatus = mStatus;
  112. mStatus = newstatus;
  113. return oldstatus;
  114. }
  115. LL_INLINE void setFlags(U32 flags)
  116. {
  117. // NOTE: flags are |'d
  118. mFlags |= flags;
  119. }
  120. // Returns true when request has completed
  121. virtual bool processRequest() = 0;
  122. // Always called from thread after request has completed or aborted
  123. LL_INLINE virtual void finishRequest(bool completed)
  124. {
  125. }
  126. // Only method to delete a request
  127. virtual void deleteRequest();
  128. LL_INLINE void setPriority(U32 pri)
  129. {
  130. // Only do this on a request that is not in a queued list !
  131. mPriority = pri;
  132. }
  133. protected:
  134. LLAtomic<status_t> mStatus;
  135. handle_t mHandle;
  136. U32 mFlags;
  137. U32 mPriority;
  138. };
  139. protected:
  140. struct queued_request_less
  141. {
  142. LL_INLINE bool operator()(const QueuedRequest* lhs,
  143. const QueuedRequest* rhs) const
  144. {
  145. // Higher priority in front of queue (set)
  146. return lhs->higherPriority(*rhs);
  147. }
  148. };
  149. public:
  150. static handle_t nullHandle() { return handle_t(0); }
  151. public:
  152. LLQueuedThread(const std::string& name);
  153. ~LLQueuedThread() override;
  154. void shutdown() override;
  155. private:
  156. // No copy constructor or copy assignment
  157. LLQueuedThread(const LLQueuedThread&) = delete;
  158. LLQueuedThread& operator=(const LLQueuedThread&) = delete;
  159. bool runCondition() override;
  160. void run() override;
  161. LL_INLINE virtual void startThread() {}
  162. LL_INLINE virtual void endThread() {}
  163. LL_INLINE virtual void threadedUpdate() {}
  164. protected:
  165. handle_t generateHandle();
  166. bool addRequest(QueuedRequest* req);
  167. size_t processNextRequest();
  168. void setRequestResult(QueuedRequest* req, bool result);
  169. public:
  170. virtual size_t update();
  171. void waitOnPending();
  172. void printQueueStats();
  173. virtual size_t getPending();
  174. // Request accessors
  175. status_t getRequestStatus(handle_t handle);
  176. void abortRequest(handle_t handle, bool autocomplete);
  177. void setFlags(handle_t handle, U32 flags);
  178. void setPriority(handle_t handle, U32 priority);
  179. bool completeRequest(handle_t handle);
  180. // This is public for support classes like LLWorkerThread, but generally
  181. // the methods above should be used.
  182. QueuedRequest* getRequest(handle_t handle);
  183. protected:
  184. typedef std::set<QueuedRequest*, queued_request_less> request_queue_t;
  185. request_queue_t mRequestQueue;
  186. handle_t mNextHandle;
  187. typedef fast_hmap<handle_t, QueuedRequest*> request_map_t;
  188. request_map_t mRequestMap;
  189. // Request queue is empty (or we are quitting) and the thread is idle
  190. LLAtomicBool mIdleThread;
  191. };
  192. #endif // LL_LLQUEUEDTHREAD_H