llworkerthread.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:10k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /** 
  2.  * @file llworkerthread.cpp
  3.  *
  4.  * $LicenseInfo:firstyear=2004&license=viewergpl$
  5.  * 
  6.  * Copyright (c) 2004-2010, 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 "llworkerthread.h"
  33. #include "llstl.h"
  34. #if USE_FRAME_CALLBACK_MANAGER
  35. #include "llframecallbackmanager.h"
  36. #endif
  37. //============================================================================
  38. // Run on MAIN thread
  39. LLWorkerThread::LLWorkerThread(const std::string& name, bool threaded) :
  40. LLQueuedThread(name, threaded)
  41. {
  42. mDeleteMutex = new LLMutex(NULL);
  43. if(!mLocalAPRFilePoolp)
  44. {
  45. mLocalAPRFilePoolp = new LLVolatileAPRPool() ;
  46. }
  47. }
  48. LLWorkerThread::~LLWorkerThread()
  49. {
  50. // Delete any workers in the delete queue (should be safe - had better be!)
  51. if (!mDeleteList.empty())
  52. {
  53. llwarns << "Worker Thread: " << mName << " destroyed with " << mDeleteList.size()
  54. << " entries in delete list." << llendl;
  55. }
  56. delete mDeleteMutex;
  57. // ~LLQueuedThread() will be called here
  58. }
  59. // virtual
  60. S32 LLWorkerThread::update(U32 max_time_ms)
  61. {
  62. S32 res = LLQueuedThread::update(max_time_ms);
  63. // Delete scheduled workers
  64. std::vector<LLWorkerClass*> delete_list;
  65. std::vector<LLWorkerClass*> abort_list;
  66. mDeleteMutex->lock();
  67. for (delete_list_t::iterator iter = mDeleteList.begin();
  68.  iter != mDeleteList.end(); )
  69. {
  70. delete_list_t::iterator curiter = iter++;
  71. LLWorkerClass* worker = *curiter;
  72. if (worker->deleteOK())
  73. {
  74. if (worker->getFlags(LLWorkerClass::WCF_WORK_FINISHED))
  75. {
  76. delete_list.push_back(worker);
  77. mDeleteList.erase(curiter);
  78. }
  79. else if (!worker->getFlags(LLWorkerClass::WCF_ABORT_REQUESTED))
  80. {
  81. abort_list.push_back(worker);
  82. }
  83. }
  84. }
  85. mDeleteMutex->unlock();
  86. // abort and delete after releasing mutex
  87. for (std::vector<LLWorkerClass*>::iterator iter = abort_list.begin();
  88.  iter != abort_list.end(); ++iter)
  89. {
  90. (*iter)->abortWork(false);
  91. }
  92. for (std::vector<LLWorkerClass*>::iterator iter = delete_list.begin();
  93.  iter != delete_list.end(); ++iter)
  94. {
  95. LLWorkerClass* worker = *iter;
  96. if (worker->mRequestHandle)
  97. {
  98. // Finished but not completed
  99. completeRequest(worker->mRequestHandle);
  100. worker->mRequestHandle = LLWorkerThread::nullHandle();
  101. worker->clearFlags(LLWorkerClass::WCF_HAVE_WORK);
  102. }
  103. delete *iter;
  104. }
  105. // delete and aborted entries mean there's still work to do
  106. res += delete_list.size() + abort_list.size();
  107. return res;
  108. }
  109. //----------------------------------------------------------------------------
  110. LLWorkerThread::handle_t LLWorkerThread::addWorkRequest(LLWorkerClass* workerclass, S32 param, U32 priority)
  111. {
  112. handle_t handle = generateHandle();
  113. WorkRequest* req = new WorkRequest(handle, priority, workerclass, param);
  114. bool res = addRequest(req);
  115. if (!res)
  116. {
  117. llerrs << "add called after LLWorkerThread::cleanupClass()" << llendl;
  118. req->deleteRequest();
  119. handle = nullHandle();
  120. }
  121. return handle;
  122. }
  123. void LLWorkerThread::deleteWorker(LLWorkerClass* workerclass)
  124. {
  125. mDeleteMutex->lock();
  126. mDeleteList.push_back(workerclass);
  127. mDeleteMutex->unlock();
  128. }
  129. //============================================================================
  130. // Runs on its OWN thread
  131. LLWorkerThread::WorkRequest::WorkRequest(handle_t handle, U32 priority, LLWorkerClass* workerclass, S32 param) :
  132. LLQueuedThread::QueuedRequest(handle, priority),
  133. mWorkerClass(workerclass),
  134. mParam(param)
  135. {
  136. }
  137. LLWorkerThread::WorkRequest::~WorkRequest()
  138. {
  139. }
  140. // virtual (required for access by LLWorkerThread)
  141. void LLWorkerThread::WorkRequest::deleteRequest()
  142. {
  143. LLQueuedThread::QueuedRequest::deleteRequest();
  144. }
  145. // virtual
  146. bool LLWorkerThread::WorkRequest::processRequest()
  147. {
  148. LLWorkerClass* workerclass = getWorkerClass();
  149. workerclass->setWorking(true);
  150. bool complete = workerclass->doWork(getParam());
  151. workerclass->setWorking(false);
  152. return complete;
  153. }
  154. // virtual
  155. void LLWorkerThread::WorkRequest::finishRequest(bool completed)
  156. {
  157. LLWorkerClass* workerclass = getWorkerClass();
  158. workerclass->finishWork(getParam(), completed);
  159. U32 flags = LLWorkerClass::WCF_WORK_FINISHED | (completed ? 0 : LLWorkerClass::WCF_WORK_ABORTED);
  160. workerclass->setFlags(flags);
  161. }
  162. //============================================================================
  163. // LLWorkerClass:: operates in main thread
  164. LLWorkerClass::LLWorkerClass(LLWorkerThread* workerthread, const std::string& name)
  165. : mWorkerThread(workerthread),
  166.   mWorkerClassName(name),
  167.   mRequestHandle(LLWorkerThread::nullHandle()),
  168.   mRequestPriority(LLWorkerThread::PRIORITY_NORMAL),
  169.   mMutex(NULL),
  170.   mWorkFlags(0)
  171. {
  172. if (!mWorkerThread)
  173. {
  174. llerrs << "LLWorkerClass() called with NULL workerthread: " << name << llendl;
  175. }
  176. }
  177. LLWorkerClass::~LLWorkerClass()
  178. {
  179. llassert_always(!(mWorkFlags & WCF_WORKING));
  180. llassert_always(mWorkFlags & WCF_DELETE_REQUESTED);
  181. llassert_always(!mMutex.isLocked());
  182. if (mRequestHandle != LLWorkerThread::nullHandle())
  183. {
  184. LLWorkerThread::WorkRequest* workreq = (LLWorkerThread::WorkRequest*)mWorkerThread->getRequest(mRequestHandle);
  185. if (!workreq)
  186. {
  187. llerrs << "LLWorkerClass destroyed with stale work handle" << llendl;
  188. }
  189. if (workreq->getStatus() != LLWorkerThread::STATUS_ABORTED &&
  190. workreq->getStatus() != LLWorkerThread::STATUS_COMPLETE)
  191. {
  192. llerrs << "LLWorkerClass destroyed with active worker! Worker Status: " << workreq->getStatus() << llendl;
  193. }
  194. }
  195. }
  196. void LLWorkerClass::setWorkerThread(LLWorkerThread* workerthread)
  197. {
  198. mMutex.lock();
  199. if (mRequestHandle != LLWorkerThread::nullHandle())
  200. {
  201. llerrs << "LLWorkerClass attempt to change WorkerThread with active worker!" << llendl;
  202. }
  203. mWorkerThread = workerthread;
  204. mMutex.unlock();
  205. }
  206. //----------------------------------------------------------------------------
  207. //virtual
  208. void LLWorkerClass::finishWork(S32 param, bool success)
  209. {
  210. }
  211. //virtual
  212. bool LLWorkerClass::deleteOK()
  213. {
  214. return true; // default always OK
  215. }
  216. //----------------------------------------------------------------------------
  217. // Called from worker thread
  218. void LLWorkerClass::setWorking(bool working)
  219. {
  220. mMutex.lock();
  221. if (working)
  222. {
  223. llassert_always(!(mWorkFlags & WCF_WORKING));
  224. setFlags(WCF_WORKING);
  225. }
  226. else
  227. {
  228. llassert_always((mWorkFlags & WCF_WORKING));
  229. clearFlags(WCF_WORKING);
  230. }
  231. mMutex.unlock();
  232. }
  233. //----------------------------------------------------------------------------
  234. bool LLWorkerClass::yield()
  235. {
  236. LLThread::yield();
  237. mWorkerThread->checkPause();
  238. bool res;
  239. mMutex.lock();
  240. res = (getFlags() & WCF_ABORT_REQUESTED) ? true : false;
  241. mMutex.unlock();
  242. return res;
  243. }
  244. //----------------------------------------------------------------------------
  245. // calls startWork, adds doWork() to queue
  246. void LLWorkerClass::addWork(S32 param, U32 priority)
  247. {
  248. mMutex.lock();
  249. llassert_always(!(mWorkFlags & (WCF_WORKING|WCF_HAVE_WORK)));
  250. if (mRequestHandle != LLWorkerThread::nullHandle())
  251. {
  252. llerrs << "LLWorkerClass attempt to add work with active worker!" << llendl;
  253. }
  254. #if _DEBUG
  255. //  llinfos << "addWork: " << mWorkerClassName << " Param: " << param << llendl;
  256. #endif
  257. startWork(param);
  258. clearFlags(WCF_WORK_FINISHED|WCF_WORK_ABORTED);
  259. setFlags(WCF_HAVE_WORK);
  260. mRequestHandle = mWorkerThread->addWorkRequest(this, param, priority);
  261. mMutex.unlock();
  262. }
  263. void LLWorkerClass::abortWork(bool autocomplete)
  264. {
  265. mMutex.lock();
  266. #if _DEBUG
  267. //  LLWorkerThread::WorkRequest* workreq = mWorkerThread->getRequest(mRequestHandle);
  268. //  if (workreq)
  269. //  llinfos << "abortWork: " << mWorkerClassName << " Param: " << workreq->getParam() << llendl;
  270. #endif
  271. if (mRequestHandle != LLWorkerThread::nullHandle())
  272. {
  273. mWorkerThread->abortRequest(mRequestHandle, autocomplete);
  274. mWorkerThread->setPriority(mRequestHandle, LLQueuedThread::PRIORITY_IMMEDIATE);
  275. setFlags(WCF_ABORT_REQUESTED);
  276. }
  277. mMutex.unlock();
  278. }
  279. // if doWork is complete or aborted, call endWork() and return true
  280. bool LLWorkerClass::checkWork(bool aborting)
  281. {
  282. LLMutexLock lock(&mMutex);
  283. bool complete = false, abort = false;
  284. if (mRequestHandle != LLWorkerThread::nullHandle())
  285. {
  286. LLWorkerThread::WorkRequest* workreq = (LLWorkerThread::WorkRequest*)mWorkerThread->getRequest(mRequestHandle);
  287. llassert_always(workreq);
  288. LLQueuedThread::status_t status = workreq->getStatus();
  289. if (status == LLWorkerThread::STATUS_ABORTED)
  290. {
  291. complete = true;
  292. abort = true;
  293. }
  294. else if (status == LLWorkerThread::STATUS_COMPLETE)
  295. {
  296. complete = true;
  297. }
  298. else
  299. {
  300. llassert_always(!aborting || (workreq->getFlags() & LLQueuedThread::FLAG_ABORT));
  301. }
  302. if (complete)
  303. {
  304. llassert_always(!(getFlags(WCF_WORKING)));
  305. endWork(workreq->getParam(), abort);
  306. mWorkerThread->completeRequest(mRequestHandle);
  307. mRequestHandle = LLWorkerThread::nullHandle();
  308. clearFlags(WCF_HAVE_WORK);
  309. }
  310. }
  311. else
  312. {
  313. complete = true;
  314. }
  315. return complete;
  316. }
  317. void LLWorkerClass::scheduleDelete()
  318. {
  319. bool do_delete = false;
  320. mMutex.lock();
  321. if (!(getFlags(WCF_DELETE_REQUESTED)))
  322. {
  323. setFlags(WCF_DELETE_REQUESTED);
  324. do_delete = true;
  325. }
  326. mMutex.unlock();
  327. if (do_delete)
  328. {
  329. mWorkerThread->deleteWorker(this);
  330. }
  331. }
  332. void LLWorkerClass::setPriority(U32 priority)
  333. {
  334. mMutex.lock();
  335. if (mRequestHandle != LLWorkerThread::nullHandle())
  336. {
  337. mRequestPriority = priority;
  338. mWorkerThread->setPriority(mRequestHandle, priority);
  339. }
  340. mMutex.unlock();
  341. }
  342. //============================================================================