/indra/llvfs/llvfsthread.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 300 lines · 206 code · 37 blank · 57 comment · 42 complexity · 7ce7081a49ab9b791d136466089b5a4b MD5 · raw file

  1. /**
  2. * @file llvfsthread.cpp
  3. * @brief LLVFSThread implementation
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llvfsthread.h"
  28. #include "llstl.h"
  29. //============================================================================
  30. /*static*/ std::string LLVFSThread::sDataPath = "";
  31. /*static*/ LLVFSThread* LLVFSThread::sLocal = NULL;
  32. //============================================================================
  33. // Run on MAIN thread
  34. //static
  35. void LLVFSThread::initClass(bool local_is_threaded)
  36. {
  37. llassert(sLocal == NULL);
  38. sLocal = new LLVFSThread(local_is_threaded);
  39. }
  40. //static
  41. S32 LLVFSThread::updateClass(U32 ms_elapsed)
  42. {
  43. sLocal->update(ms_elapsed);
  44. return sLocal->getPending();
  45. }
  46. //static
  47. void LLVFSThread::cleanupClass()
  48. {
  49. sLocal->setQuitting();
  50. while (sLocal->getPending())
  51. {
  52. sLocal->update(0);
  53. }
  54. delete sLocal;
  55. sLocal = 0;
  56. }
  57. //----------------------------------------------------------------------------
  58. LLVFSThread::LLVFSThread(bool threaded) :
  59. LLQueuedThread("VFS", threaded)
  60. {
  61. }
  62. LLVFSThread::~LLVFSThread()
  63. {
  64. // ~LLQueuedThread() will be called here
  65. }
  66. //----------------------------------------------------------------------------
  67. LLVFSThread::handle_t LLVFSThread::read(LLVFS* vfs, const LLUUID &file_id, const LLAssetType::EType file_type,
  68. U8* buffer, S32 offset, S32 numbytes, U32 priority, U32 flags)
  69. {
  70. handle_t handle = generateHandle();
  71. priority = llmax(priority, (U32)PRIORITY_LOW); // All reads are at least PRIORITY_LOW
  72. Request* req = new Request(handle, priority, flags, FILE_READ, vfs, file_id, file_type,
  73. buffer, offset, numbytes);
  74. bool res = addRequest(req);
  75. if (!res)
  76. {
  77. llerrs << "LLVFSThread::read called after LLVFSThread::cleanupClass()" << llendl;
  78. req->deleteRequest();
  79. handle = nullHandle();
  80. }
  81. return handle;
  82. }
  83. S32 LLVFSThread::readImmediate(LLVFS* vfs, const LLUUID &file_id, const LLAssetType::EType file_type,
  84. U8* buffer, S32 offset, S32 numbytes)
  85. {
  86. handle_t handle = generateHandle();
  87. Request* req = new Request(handle, PRIORITY_IMMEDIATE, 0, FILE_READ, vfs, file_id, file_type,
  88. buffer, offset, numbytes);
  89. S32 res = addRequest(req) ? 1 : 0;
  90. if (res == 0)
  91. {
  92. llerrs << "LLVFSThread::read called after LLVFSThread::cleanupClass()" << llendl;
  93. req->deleteRequest();
  94. }
  95. else
  96. {
  97. llverify(waitForResult(handle, false) == true);
  98. res = req->getBytesRead();
  99. completeRequest(handle);
  100. }
  101. return res;
  102. }
  103. LLVFSThread::handle_t LLVFSThread::write(LLVFS* vfs, const LLUUID &file_id, const LLAssetType::EType file_type,
  104. U8* buffer, S32 offset, S32 numbytes, U32 flags)
  105. {
  106. handle_t handle = generateHandle();
  107. Request* req = new Request(handle, 0, flags, FILE_WRITE, vfs, file_id, file_type,
  108. buffer, offset, numbytes);
  109. bool res = addRequest(req);
  110. if (!res)
  111. {
  112. llerrs << "LLVFSThread::read called after LLVFSThread::cleanupClass()" << llendl;
  113. req->deleteRequest();
  114. handle = nullHandle();
  115. }
  116. return handle;
  117. }
  118. S32 LLVFSThread::writeImmediate(LLVFS* vfs, const LLUUID &file_id, const LLAssetType::EType file_type,
  119. U8* buffer, S32 offset, S32 numbytes)
  120. {
  121. handle_t handle = generateHandle();
  122. Request* req = new Request(handle, PRIORITY_IMMEDIATE, 0, FILE_WRITE, vfs, file_id, file_type,
  123. buffer, offset, numbytes);
  124. S32 res = addRequest(req) ? 1 : 0;
  125. if (res == 0)
  126. {
  127. llerrs << "LLVFSThread::read called after LLVFSThread::cleanupClass()" << llendl;
  128. req->deleteRequest();
  129. }
  130. else
  131. {
  132. llverify(waitForResult(handle, false) == true);
  133. res = req->getBytesRead();
  134. completeRequest(handle);
  135. }
  136. return res;
  137. }
  138. // LLVFSThread::handle_t LLVFSThread::rename(LLVFS* vfs, const LLUUID &file_id, const LLAssetType::EType file_type,
  139. // const LLUUID &new_id, const LLAssetType::EType new_type, U32 flags)
  140. // {
  141. // handle_t handle = generateHandle();
  142. // LLUUID* new_idp = new LLUUID(new_id); // deleted with Request
  143. // // new_type is passed as "numbytes"
  144. // Request* req = new Request(handle, 0, flags, FILE_RENAME, vfs, file_id, file_type,
  145. // (U8*)new_idp, 0, (S32)new_type);
  146. // bool res = addRequest(req);
  147. // if (!res)
  148. // {
  149. // llerrs << "LLVFSThread::read called after LLVFSThread::cleanupClass()" << llendl;
  150. // req->deleteRequest();
  151. // handle = nullHandle();
  152. // }
  153. // return handle;
  154. // }
  155. //============================================================================
  156. LLVFSThread::Request::Request(handle_t handle, U32 priority, U32 flags,
  157. operation_t op, LLVFS* vfs,
  158. const LLUUID &file_id, const LLAssetType::EType file_type,
  159. U8* buffer, S32 offset, S32 numbytes) :
  160. QueuedRequest(handle, priority, flags),
  161. mOperation(op),
  162. mVFS(vfs),
  163. mFileID(file_id),
  164. mFileType(file_type),
  165. mBuffer(buffer),
  166. mOffset(offset),
  167. mBytes(numbytes),
  168. mBytesRead(0)
  169. {
  170. llassert(mBuffer);
  171. if (numbytes <= 0 && mOperation != FILE_RENAME)
  172. {
  173. llwarns << "LLVFSThread: Request with numbytes = " << numbytes
  174. << " operation = " << op
  175. << " offset " << offset
  176. << " file_type " << file_type << llendl;
  177. }
  178. if (mOperation == FILE_WRITE)
  179. {
  180. S32 blocksize = mVFS->getMaxSize(mFileID, mFileType);
  181. if (blocksize < 0)
  182. {
  183. llwarns << "VFS write to temporary block (shouldn't happen)" << llendl;
  184. }
  185. mVFS->incLock(mFileID, mFileType, VFSLOCK_APPEND);
  186. }
  187. else if (mOperation == FILE_RENAME)
  188. {
  189. mVFS->incLock(mFileID, mFileType, VFSLOCK_APPEND);
  190. }
  191. else // if (mOperation == FILE_READ)
  192. {
  193. mVFS->incLock(mFileID, mFileType, VFSLOCK_READ);
  194. }
  195. }
  196. // dec locks as soon as a request finishes
  197. void LLVFSThread::Request::finishRequest(bool completed)
  198. {
  199. if (mOperation == FILE_WRITE)
  200. {
  201. mVFS->decLock(mFileID, mFileType, VFSLOCK_APPEND);
  202. }
  203. else if (mOperation == FILE_RENAME)
  204. {
  205. mVFS->decLock(mFileID, mFileType, VFSLOCK_APPEND);
  206. }
  207. else // if (mOperation == FILE_READ)
  208. {
  209. mVFS->decLock(mFileID, mFileType, VFSLOCK_READ);
  210. }
  211. }
  212. void LLVFSThread::Request::deleteRequest()
  213. {
  214. if (getStatus() == STATUS_QUEUED)
  215. {
  216. llerrs << "Attempt to delete a queued LLVFSThread::Request!" << llendl;
  217. }
  218. if (mOperation == FILE_WRITE)
  219. {
  220. if (mFlags & FLAG_AUTO_DELETE)
  221. {
  222. delete [] mBuffer;
  223. }
  224. }
  225. else if (mOperation == FILE_RENAME)
  226. {
  227. LLUUID* new_idp = (LLUUID*)mBuffer;
  228. delete new_idp;
  229. }
  230. LLQueuedThread::QueuedRequest::deleteRequest();
  231. }
  232. bool LLVFSThread::Request::processRequest()
  233. {
  234. bool complete = false;
  235. if (mOperation == FILE_READ)
  236. {
  237. llassert(mOffset >= 0);
  238. mBytesRead = mVFS->getData(mFileID, mFileType, mBuffer, mOffset, mBytes);
  239. complete = true;
  240. //llinfos << llformat("LLVFSThread::READ '%s': %d bytes arg:%d",getFilename(),mBytesRead) << llendl;
  241. }
  242. else if (mOperation == FILE_WRITE)
  243. {
  244. mBytesRead = mVFS->storeData(mFileID, mFileType, mBuffer, mOffset, mBytes);
  245. complete = true;
  246. //llinfos << llformat("LLVFSThread::WRITE '%s': %d bytes arg:%d",getFilename(),mBytesRead) << llendl;
  247. }
  248. else if (mOperation == FILE_RENAME)
  249. {
  250. LLUUID* new_idp = (LLUUID*)mBuffer;
  251. LLAssetType::EType new_type = (LLAssetType::EType)mBytes;
  252. mVFS->renameFile(mFileID, mFileType, *new_idp, new_type);
  253. mFileID = *new_idp;
  254. complete = true;
  255. //llinfos << llformat("LLVFSThread::RENAME '%s': %d bytes arg:%d",getFilename(),mBytesRead) << llendl;
  256. }
  257. else
  258. {
  259. llerrs << llformat("LLVFSThread::unknown operation: %d", mOperation) << llendl;
  260. }
  261. return complete;
  262. }
  263. //============================================================================