llthreadpool.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * @file llthreadpool.cpp
  3. * @brief Configures a LLWorkQueue along with a pool of threads to service it.
  4. * @author Nat Goodspeed
  5. * @date 2021-10-21
  6. *
  7. * $LicenseInfo:firstyear=2021&license=viewergpl$
  8. *
  9. * Copyright (c) 2021, Linden Research, Inc. (c) 2022 Henri Beauchamp.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #include "linden_common.h"
  35. #include "llthreadpool.h"
  36. #include "llevents.h"
  37. #include "llsys.h"
  38. #include "lltimer.h" // For ms_sleep()
  39. #include "hbtracy.h"
  40. LLThreadPool::LLThreadPool(const std::string& name, U32 threads, U32 capacity)
  41. : mQueue(name, capacity),
  42. mName("ThreadPool:" + name),
  43. #if TRACY_ENABLE
  44. mThreadPoolName(NULL),
  45. #endif
  46. mThreadCount(threads),
  47. mStartedThreads(0)
  48. {
  49. mThreads.reserve(threads);
  50. }
  51. const std::string& LLThreadPool::getThreadName(U64 id_hash)
  52. {
  53. static const std::string invalid = "invalid";
  54. mThreadNamesMutex.lock();
  55. tnames_map_t::const_iterator it = mThreadNames.find(id_hash);
  56. tnames_map_t::const_iterator end = mThreadNames.end();
  57. mThreadNamesMutex.unlock();
  58. return it != end ? it->second : invalid;
  59. }
  60. void LLThreadPool::start(bool wait_for_start)
  61. {
  62. std::string tname;
  63. for (U32 i = 0; i < mThreadCount; ++i)
  64. {
  65. tname = llformat("%s:%d/%d", mName.c_str(), i + 1, mThreadCount);
  66. // Note 1: this fails to compile with gcc v5.5 which does not like
  67. // emplacing a lambda as a boost::thread (or std::thread)... HB
  68. // Note 2: since we queue a thread, this is also a threaded operation
  69. // and the newly created thread will start asynchronously to this
  70. // queueing operation. HB
  71. mThreads.emplace_back(tname,
  72. [this, tname]()
  73. {
  74. run(tname);
  75. });
  76. }
  77. LLEventPump& pump = gEventPumps.obtain("LLApp");
  78. pump.listen(mName,
  79. [this](const LLSD& stat)
  80. {
  81. std::string status = stat["status"];
  82. if (status != "running")
  83. {
  84. // Note: on crash, the app first goes to "error" status,
  85. // then to "stopped" status as soon as it ran its error
  86. // handler. If the status directly reaches "stopped",
  87. // then it is too late to join the threads anyway, so
  88. // we consider anything else than "quitting" to be the
  89. // result of a crash, in order to avoid never ending
  90. // loops waiting for thread shutdown. HB
  91. close(true, status != "quitting");
  92. }
  93. return false;
  94. });
  95. if (wait_for_start)
  96. {
  97. // We wait for all threads to start before we return to the caller
  98. // (which is normally itself running on the main thread); this ensures
  99. // no operation will be attempted before all threads are ready to
  100. // process the queue. HB
  101. do
  102. {
  103. ms_sleep(1);
  104. }
  105. while ((U32)mStartedThreads < mThreadCount);
  106. }
  107. // Yield and give a tiny bit of time for the threads to start; use true for
  108. // the wait_for_start boolean if you want to ensure all threads get started
  109. // before we return to the caller (in which case, this additional sleep
  110. // gives a chance for the last started thread to call its run() method
  111. // before we return). HB
  112. ms_sleep(1);
  113. }
  114. void LLThreadPool::close(bool on_shutdown, bool on_crash)
  115. {
  116. if (on_crash)
  117. {
  118. llinfos << mName << " was informed of viewer crash." << llendl;
  119. }
  120. else if (on_shutdown)
  121. {
  122. llinfos << mName << " was informed of viewer shutdown." << llendl;
  123. }
  124. // Un-register ourselves from the event pump to avoid a crash should we
  125. // re-register later under the same name (which happens for the GL image
  126. // worker thread on restoreGL() calls). HB
  127. LL_DEBUGS("ThreadPool") << mName << ": stop listening to LLApp events..."
  128. << LL_ENDL;
  129. gEventPumps.obtain("LLApp").stopListening(mName);
  130. if (mQueue.isClosed())
  131. {
  132. LL_DEBUGS("ThreadPool") << mName << " queue is already closed."
  133. << LL_ENDL;
  134. return; // Nothing to do...
  135. }
  136. llinfos << mName << ": closing queue..." << llendl;
  137. mQueue.close();
  138. llinfos << mName << ": shutting down threads..." << llendl;
  139. for (auto& pair : mThreads)
  140. {
  141. if (!on_crash && pair.second.joinable())
  142. {
  143. LL_DEBUGS("ThreadPool") << mName << " joining thread "
  144. << pair.first << LL_ENDL;
  145. pair.second.join();
  146. }
  147. // Do not join threads on crash (or whenever a particular thread cannot
  148. // be joined, for whatever reason), to avoid waiting indefinitely (the
  149. // threads might rely on conditions to exit, that might not be possible
  150. // to fulfil any more due to the crash). Instead, detach the threads
  151. // and request their termination. HB
  152. else
  153. {
  154. LL_DEBUGS("ThreadPool") << mName << " detaching thread "
  155. << pair.first << LL_ENDL;
  156. auto native_handle = pair.second.native_handle();
  157. pair.second.detach();
  158. LL_DEBUGS("ThreadPool") << mName << " cancelling thread "
  159. << pair.first << LL_ENDL;
  160. #if LL_WINDOWS
  161. TerminateThread(native_handle, 0);
  162. #else
  163. pthread_cancel(native_handle);
  164. #endif
  165. }
  166. }
  167. llinfos << mName << " shutdown complete with "
  168. << (mQueue.empty() ? "an " : "a non-") << "empty queue." << llendl;
  169. }
  170. void LLThreadPool::run(const std::string& name)
  171. {
  172. llinfos << "Starting thread: " << name << llendl;
  173. mThreadNamesMutex.lock();
  174. #if TRACY_ENABLE
  175. if (!mThreadPoolName)
  176. {
  177. // We must keep the thread name string till the program exits (i.e. it
  178. // must outlive the thread), and the string pointer must be unique...
  179. // See the chapters 3.1.1 and 3.1.2 of the Tracy manual. HB
  180. gTracyThreadNamesLock.lock();
  181. gTracyThreadNames.emplace_back(name.substr(0, name.rfind(':')));
  182. mThreadPoolName = gTracyThreadNames.back().c_str();
  183. gTracyThreadNamesLock.unlock();
  184. }
  185. // We give to all threads of a given pool, the same (pool) name for Tracy
  186. // since we do not care about per-thread timing, but only want to see stats
  187. // about the pool itself, as a whole. HB
  188. tracy::SetThreadName(mThreadPoolName);
  189. #endif
  190. // Give the thread a way to find its own name later on... HB
  191. mThreadNames.emplace(LLThread::thisThreadIdHash(), name);
  192. mThreadNamesMutex.unlock();
  193. // Set the CPU affinity for this child thread to the complementary of the
  194. // main thread affinity, so that they run on different cores. When the main
  195. // thread affinity is 0 (or under macOS) this call is a no-operation and no
  196. // affinity is set for any thread. HB
  197. S32 result = LLCPUInfo::setThreadCPUAffinity();
  198. if (!result)
  199. {
  200. llwarns << "Failed to set CPU affinity for thread: " << name << llendl;
  201. }
  202. else if (result == -1)
  203. {
  204. llinfos << "Could not set CPU affinity for thead: " << name
  205. << " (main thread affinity not yet set)." << llendl;
  206. }
  207. // One more thread has been started, but maybe not fully intialized and
  208. // ready, in which case you must override this call with your own, no-op
  209. // maybeIncStartedThreads() method, and call, when appropriate, the
  210. // doIncStartedThreads() method in your own overridden run() method. HB
  211. maybeIncStartedThreads();
  212. run();
  213. llinfos << "Thread " << name
  214. << " stopped. Number of operations performed: "
  215. << mQueue.getCalls() << llendl;
  216. }
  217. //virtual
  218. void LLThreadPool::run()
  219. {
  220. mQueue.runUntilClose();
  221. }