llthread.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * @file llthread.cpp
  3. *
  4. * $LicenseInfo:firstyear=2004&license=viewergpl$
  5. *
  6. * Copyright (c) 2004-2009, Linden Research, Inc.
  7. *
  8. * Second Life Viewer Source Code
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. */
  31. #include "linden_common.h"
  32. #include "boost/container_hash/hash.hpp"
  33. #include "llthread.h"
  34. #include "lltimer.h"
  35. #include "hbtracy.h"
  36. #include "llsys.h"
  37. #if TRACY_ENABLE
  38. std::list<std::string> gTracyThreadNames;
  39. LLMutex gTracyThreadNamesLock;
  40. #endif
  41. #ifdef LL_WINDOWS
  42. constexpr DWORD MS_VC_EXCEPTION = 0x406D1388;
  43. # pragma pack(push,8)
  44. typedef struct tagTHREADNAME_INFO
  45. {
  46. DWORD dwType; // Must be 0x1000.
  47. LPCSTR szName; // Pointer to name (in user addr space).
  48. DWORD dwThreadID; // Thread ID (-1=caller thread).
  49. DWORD dwFlags; // Reserved for future use, must be zero.
  50. } THREADNAME_INFO;
  51. # pragma pack(pop)
  52. void set_thread_name(DWORD thread_id, const char* thread_name)
  53. {
  54. THREADNAME_INFO info;
  55. info.dwType = 0x1000;
  56. info.szName = thread_name;
  57. info.dwThreadID = thread_id;
  58. info.dwFlags = 0;
  59. __try
  60. {
  61. ::RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD),
  62. (ULONG_PTR*)&info);
  63. }
  64. __except(EXCEPTION_CONTINUE_EXECUTION)
  65. {
  66. }
  67. }
  68. #endif
  69. // Caching the current thread Id in a thread_local variable for speed... HB
  70. static thread_local LLThread::id_t tThreadId = boost::this_thread::get_id();
  71. static LLThread::id_t get_main_thread_id()
  72. {
  73. // Using a function-static variable to identify the main thread requires
  74. // that control reaches here from the main thread before it reaches here
  75. // from any other thread. We simply trust that whichever thread gets here
  76. // first is the main thread.
  77. static LLThread::id_t main_thread_id = tThreadId;
  78. return main_thread_id;
  79. }
  80. bool is_main_thread()
  81. {
  82. return tThreadId == get_main_thread_id();
  83. }
  84. void assert_main_thread()
  85. {
  86. if (tThreadId != get_main_thread_id())
  87. {
  88. llerrs << "Illegal execution from thread id " << tThreadId
  89. << " outside main thread " << get_main_thread_id() << llendl;
  90. }
  91. }
  92. //static
  93. LLThread::id_t LLThread::currentID()
  94. {
  95. return tThreadId;
  96. }
  97. //static
  98. U64 LLThread::thisThreadIdHash()
  99. {
  100. // Caching the hash in a thread_local static variable for speed. HB
  101. thread_local U64 id_hash = boost::hash<id_t>()(tThreadId);
  102. return id_hash;
  103. }
  104. //static
  105. void LLThread::yield()
  106. {
  107. boost::this_thread::yield();
  108. }
  109. LLThread::LLThread(const std::string& name)
  110. : mName(name),
  111. #if TRACY_ENABLE
  112. mThreadName(NULL),
  113. #endif
  114. mThreadp(NULL),
  115. mStatus(STOPPED),
  116. mRetries(1),
  117. mPaused(false),
  118. mNeedsAffinity(false)
  119. {
  120. mRunCondition = new LLCondition();
  121. mDataLock = new LLMutex();
  122. }
  123. LLThread::~LLThread()
  124. {
  125. shutdown();
  126. }
  127. void LLThread::threadRun()
  128. {
  129. #ifdef LL_WINDOWS
  130. set_thread_name(-1, mName.c_str());
  131. #endif
  132. #if TRACY_ENABLE
  133. if (!mThreadName)
  134. {
  135. // We must keep the thread name string till the program exits (i.e. it
  136. // must outlive the thread), and the string pointer must be unique...
  137. // See the chapters 3.1.1 and 3.1.2 of the Tracy manual. HB
  138. gTracyThreadNamesLock.lock();
  139. gTracyThreadNames.emplace_back(mName);
  140. mThreadName = gTracyThreadNames.back().c_str();
  141. gTracyThreadNamesLock.unlock();
  142. }
  143. tracy::SetThreadName(mThreadName);
  144. #endif
  145. mID = tThreadId;
  146. llinfos << "Running thread " << mName << " with Id: " << mID << llendl;
  147. // Set the CPU affinity for this child thread to the complementary of the
  148. // main thread affinity, so that they run on different cores.
  149. // When the main thread affinity is 0 (or under macOS) this call is a no-
  150. // operation and no affinity is set for any thread. HB
  151. S32 result = LLCPUInfo::setThreadCPUAffinity();
  152. if (!result)
  153. {
  154. llwarns << "Failed to set CPU affinity for thread: " << mName
  155. << " - Id: " << mID << llendl;
  156. }
  157. else if (result == -1)
  158. {
  159. mNeedsAffinity = true;
  160. }
  161. while (mRetries)
  162. {
  163. --mRetries;
  164. LL_DEBUGS("Threads") << "Running: " << mName << " - Retries left: "
  165. << mRetries << LL_ENDL;
  166. try
  167. {
  168. // Run the user supplied function
  169. run();
  170. }
  171. catch (std::runtime_error& e)
  172. {
  173. llwarns << "Caught exception '" << e.what() << "' in thread: "
  174. << mName << " - Id: " << mID << llendl;
  175. continue;
  176. }
  177. catch (...)
  178. {
  179. llwarns << "An unknown exception occurred during thread"
  180. << mName << " - Id: " << mID << llendl;
  181. }
  182. break;
  183. }
  184. LL_DEBUGS("Threads") << "Exiting: " << mName << " - Id: "
  185. << mID << LL_ENDL;
  186. // We are done with the run function, this thread is done executing now.
  187. mStatus = STOPPED;
  188. }
  189. void LLThread::shutdown()
  190. {
  191. // WARNING: if you somehow call the thread destructor from itself, the
  192. // thread will die in an unclean fashion !
  193. if (mThreadp)
  194. {
  195. if (!isStopped())
  196. {
  197. // The thread is not already stopped. First, set the flag
  198. // indicating that we are ready to die
  199. setQuitting();
  200. LL_DEBUGS("Threads") << "Killing thread: " << mName << " Status: "
  201. << mStatus << LL_ENDL;
  202. // Now wait a bit for the thread to exit. It is unclear whether I
  203. // should even bother doing this; this destructor should never get
  204. // called unless we are already stopped, really...
  205. S32 counter = 0;
  206. constexpr S32 MAX_WAIT = 600;
  207. while (counter < MAX_WAIT)
  208. {
  209. if (isStopped())
  210. {
  211. break;
  212. }
  213. // Sleep for a tenth of a second
  214. ms_sleep(100);
  215. yield();
  216. ++counter;
  217. }
  218. }
  219. if (!isStopped())
  220. {
  221. // This thread just would not stop, even though we gave it time
  222. llwarns << "Exiting thread before clean exit !" << llendl;
  223. // Note: since the thread has been detached when started, we can
  224. // safely terminate it now, without any risk to cause a termination
  225. // of the main process.
  226. #if LL_WINDOWS
  227. TerminateThread(mNativeHandle, 0);
  228. #else
  229. pthread_cancel(mNativeHandle);
  230. #endif
  231. }
  232. delete mThreadp;
  233. mThreadp = NULL;
  234. }
  235. delete mRunCondition;
  236. mRunCondition = NULL;
  237. delete mDataLock;
  238. mDataLock = NULL;
  239. }
  240. void LLThread::start()
  241. {
  242. llassert(isStopped());
  243. // Set thread state to running
  244. mStatus = RUNNING;
  245. try
  246. {
  247. mThreadp = new boost::thread(boost::bind(&LLThread::threadRun, this));
  248. mNativeHandle = mThreadp->native_handle();
  249. // Detach immediately the thread from the main process, so that it will
  250. // run independently until its termination.
  251. mThreadp->detach();
  252. }
  253. catch ( ...)
  254. {
  255. mStatus = STOPPED;
  256. llwarns << "Failed to start thread: " << mName << " - Id: " << mID
  257. << llendl;
  258. }
  259. }
  260. // Called from MAIN THREAD. Requests that the thread pauses. The thread will
  261. // pause when (and if) it calls checkPause()
  262. void LLThread::pause()
  263. {
  264. if (!mPaused)
  265. {
  266. // This will cause the thread to stop execution as soon as checkPause()
  267. // is called. Does not need to be atomic since this is only set/unset
  268. // from the main thread
  269. mPaused = true;
  270. }
  271. }
  272. // Request that the thread pause/resume.
  273. // Called from MAIN THREAD. Requests that the thread resumes.
  274. void LLThread::unpause()
  275. {
  276. if (mPaused)
  277. {
  278. mPaused = false;
  279. }
  280. wake(); // Wake up the thread if necessary
  281. }
  282. // Virtual predicate function. Returns true if the thread should wake up, false
  283. // if it should sleep.
  284. bool LLThread::runCondition()
  285. {
  286. // By default, always run. Handling of pause/unpause is done regardless of
  287. // this function's result.
  288. return true;
  289. }
  290. // Called from run() (CHILD THREAD). Stops thread execution if requested until
  291. // unpaused.
  292. void LLThread::checkPause()
  293. {
  294. if (mNeedsAffinity)
  295. {
  296. S32 result = LLCPUInfo::setThreadCPUAffinity();
  297. if (result == 1)
  298. {
  299. mNeedsAffinity = false;
  300. }
  301. else if (!result)
  302. {
  303. llwarns << "Failed to set CPU affinity for thread: " << mName
  304. << " - Id: " << mID << llendl;
  305. }
  306. }
  307. mDataLock->lock();
  308. // This is in a while loop because the pthread API allows for spurious
  309. // wakeups.
  310. while (shouldSleep())
  311. {
  312. mDataLock->unlock();
  313. mRunCondition->wait(); // Locks mRunCondition
  314. mDataLock->lock();
  315. // mRunCondition is locked when the thread wakes up
  316. }
  317. mDataLock->unlock();
  318. }
  319. void LLThread::setQuitting()
  320. {
  321. mDataLock->lock();
  322. if (mStatus == RUNNING)
  323. {
  324. mStatus = QUITTING;
  325. }
  326. // It is only safe to remove mRunCondition if all locked threads were
  327. // notified
  328. mRunCondition->broadcast();
  329. mDataLock->unlock();
  330. }
  331. void LLThread::wake()
  332. {
  333. mDataLock->lock();
  334. if (!shouldSleep())
  335. {
  336. mRunCondition->signal();
  337. }
  338. mDataLock->unlock();
  339. }
  340. void LLThread::wakeLocked()
  341. {
  342. if (!shouldSleep())
  343. {
  344. mRunCondition->signal();
  345. }
  346. }