diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llvfs/lllfsthread.cpp | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llvfs/lllfsthread.cpp')
-rw-r--r-- | linden/indra/llvfs/lllfsthread.cpp | 327 |
1 files changed, 327 insertions, 0 deletions
diff --git a/linden/indra/llvfs/lllfsthread.cpp b/linden/indra/llvfs/lllfsthread.cpp new file mode 100644 index 0000000..b7335e2 --- /dev/null +++ b/linden/indra/llvfs/lllfsthread.cpp | |||
@@ -0,0 +1,327 @@ | |||
1 | /** | ||
2 | * @file lllfsthread.cpp | ||
3 | * @brief LLLFSThread base class | ||
4 | * | ||
5 | * Copyright (c) 2001-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #include "linden_common.h" | ||
29 | #include "llmath.h" | ||
30 | #include "lllfsthread.h" | ||
31 | #include "llstl.h" | ||
32 | #include "llapr.h" | ||
33 | |||
34 | //============================================================================ | ||
35 | |||
36 | /*static*/ LLLFSThread* LLLFSThread::sLocal = NULL; | ||
37 | |||
38 | //============================================================================ | ||
39 | // Run on MAIN thread | ||
40 | //static | ||
41 | void LLLFSThread::initClass(bool local_is_threaded, bool local_run_always) | ||
42 | { | ||
43 | llassert(sLocal == NULL); | ||
44 | sLocal = new LLLFSThread(local_is_threaded, local_run_always); | ||
45 | } | ||
46 | |||
47 | //static | ||
48 | S32 LLLFSThread::updateClass(U32 ms_elapsed) | ||
49 | { | ||
50 | sLocal->update(ms_elapsed); | ||
51 | return sLocal->getPending(); | ||
52 | } | ||
53 | |||
54 | //static | ||
55 | void LLLFSThread::cleanupClass() | ||
56 | { | ||
57 | sLocal->setQuitting(); | ||
58 | while (sLocal->getPending()) | ||
59 | { | ||
60 | sLocal->update(0); | ||
61 | } | ||
62 | delete sLocal; | ||
63 | sLocal = 0; | ||
64 | } | ||
65 | |||
66 | //---------------------------------------------------------------------------- | ||
67 | |||
68 | LLLFSThread::LLLFSThread(bool threaded, bool runalways) : | ||
69 | LLQueuedThread("LFS", threaded, runalways) | ||
70 | { | ||
71 | } | ||
72 | |||
73 | LLLFSThread::~LLLFSThread() | ||
74 | { | ||
75 | // ~LLQueuedThread() will be called here | ||
76 | } | ||
77 | |||
78 | //---------------------------------------------------------------------------- | ||
79 | |||
80 | LLLFSThread::handle_t LLLFSThread::read(const LLString& filename, | ||
81 | U8* buffer, S32 offset, S32 numbytes, U32 priority, U32 flags) | ||
82 | { | ||
83 | handle_t handle = generateHandle(); | ||
84 | |||
85 | priority = llmax(priority, (U32)PRIORITY_LOW); // All reads are at least PRIORITY_LOW | ||
86 | Request* req = new Request(handle, priority, flags, | ||
87 | FILE_READ, filename, | ||
88 | buffer, offset, numbytes); | ||
89 | |||
90 | bool res = addRequest(req); | ||
91 | if (!res) | ||
92 | { | ||
93 | llerrs << "LLLFSThread::read called after LLLFSThread::cleanupClass()" << llendl; | ||
94 | req->deleteRequest(); | ||
95 | handle = nullHandle(); | ||
96 | } | ||
97 | |||
98 | return handle; | ||
99 | } | ||
100 | |||
101 | S32 LLLFSThread::readImmediate(const LLString& filename, | ||
102 | U8* buffer, S32 offset, S32 numbytes) | ||
103 | { | ||
104 | handle_t handle = generateHandle(); | ||
105 | |||
106 | Request* req = new Request(handle, PRIORITY_IMMEDIATE, 0, | ||
107 | FILE_READ, filename, | ||
108 | buffer, offset, numbytes); | ||
109 | |||
110 | S32 res = addRequest(req) ? 1 : 0; | ||
111 | if (res == 0) | ||
112 | { | ||
113 | llerrs << "LLLFSThread::read called after LLLFSThread::cleanupClass()" << llendl; | ||
114 | req->deleteRequest(); | ||
115 | } | ||
116 | else | ||
117 | { | ||
118 | llverify(waitForResult(handle, false) == true); | ||
119 | res = req->getBytesRead(); | ||
120 | completeRequest(handle); | ||
121 | } | ||
122 | return res; | ||
123 | } | ||
124 | |||
125 | LLLFSThread::handle_t LLLFSThread::write(const LLString& filename, | ||
126 | U8* buffer, S32 offset, S32 numbytes, U32 flags) | ||
127 | { | ||
128 | handle_t handle = generateHandle(); | ||
129 | |||
130 | Request* req = new Request(handle, 0, flags, | ||
131 | FILE_WRITE, filename, | ||
132 | buffer, offset, numbytes); | ||
133 | |||
134 | bool res = addRequest(req); | ||
135 | if (!res) | ||
136 | { | ||
137 | llerrs << "LLLFSThread::read called after LLLFSThread::cleanupClass()" << llendl; | ||
138 | req->deleteRequest(); | ||
139 | handle = nullHandle(); | ||
140 | } | ||
141 | |||
142 | return handle; | ||
143 | } | ||
144 | |||
145 | S32 LLLFSThread::writeImmediate(const LLString& filename, | ||
146 | U8* buffer, S32 offset, S32 numbytes) | ||
147 | { | ||
148 | handle_t handle = generateHandle(); | ||
149 | |||
150 | Request* req = new Request(handle, PRIORITY_IMMEDIATE, 0, | ||
151 | FILE_WRITE, filename, | ||
152 | buffer, offset, numbytes); | ||
153 | |||
154 | S32 res = addRequest(req) ? 1 : 0; | ||
155 | if (res == 0) | ||
156 | { | ||
157 | llerrs << "LLLFSThread::write called after LLLFSThread::cleanupClass()" << llendl; | ||
158 | req->deleteRequest(); | ||
159 | } | ||
160 | else | ||
161 | { | ||
162 | llverify(waitForResult(handle, false) == true); | ||
163 | res = req->getBytesRead(); | ||
164 | completeRequest(handle); | ||
165 | } | ||
166 | return res; | ||
167 | } | ||
168 | |||
169 | |||
170 | LLLFSThread::handle_t LLLFSThread::rename(const LLString& filename, const LLString& newname, U32 flags) | ||
171 | { | ||
172 | handle_t handle = generateHandle(); | ||
173 | |||
174 | LLString* new_name_str = new LLString(newname); // deleted with Request | ||
175 | Request* req = new Request(handle, 0, flags, | ||
176 | FILE_RENAME, filename, | ||
177 | (U8*)new_name_str, 0, 0); | ||
178 | |||
179 | bool res = addRequest(req); | ||
180 | if (!res) | ||
181 | { | ||
182 | llerrs << "LLLFSThread::rename called after LLLFSThread::cleanupClass()" << llendl; | ||
183 | req->deleteRequest(); | ||
184 | handle = nullHandle(); | ||
185 | } | ||
186 | |||
187 | return handle; | ||
188 | } | ||
189 | |||
190 | LLLFSThread::handle_t LLLFSThread::remove(const LLString& filename, U32 flags) | ||
191 | { | ||
192 | handle_t handle = generateHandle(); | ||
193 | |||
194 | Request* req = new Request(handle, 0, flags, | ||
195 | FILE_RENAME, filename, | ||
196 | NULL, 0, 0); | ||
197 | |||
198 | bool res = addRequest(req); | ||
199 | if (!res) | ||
200 | { | ||
201 | llerrs << "LLLFSThread::remove called after LLLFSThread::cleanupClass()" << llendl; | ||
202 | req->deleteRequest(); | ||
203 | handle = nullHandle(); | ||
204 | } | ||
205 | |||
206 | return handle; | ||
207 | } | ||
208 | |||
209 | //============================================================================ | ||
210 | // Runs on its OWN thread | ||
211 | |||
212 | bool LLLFSThread::processRequest(QueuedRequest* qreq) | ||
213 | { | ||
214 | Request *req = (Request*)qreq; | ||
215 | |||
216 | bool complete = req->processIO(); | ||
217 | |||
218 | return complete; | ||
219 | } | ||
220 | |||
221 | //============================================================================ | ||
222 | |||
223 | LLLFSThread::Request::Request(handle_t handle, U32 priority, U32 flags, | ||
224 | operation_t op, const LLString& filename, | ||
225 | U8* buffer, S32 offset, S32 numbytes) : | ||
226 | QueuedRequest(handle, priority, flags), | ||
227 | mOperation(op), | ||
228 | mFileName(filename), | ||
229 | mBuffer(buffer), | ||
230 | mOffset(offset), | ||
231 | mBytes(numbytes), | ||
232 | mBytesRead(0) | ||
233 | { | ||
234 | llassert(mBuffer); | ||
235 | |||
236 | if (numbytes <= 0 && mOperation != FILE_RENAME && mOperation != FILE_REMOVE) | ||
237 | { | ||
238 | llwarns << "LLLFSThread: Request with numbytes = " << numbytes << llendl; | ||
239 | } | ||
240 | } | ||
241 | |||
242 | void LLLFSThread::Request::finishRequest() | ||
243 | { | ||
244 | } | ||
245 | |||
246 | void LLLFSThread::Request::deleteRequest() | ||
247 | { | ||
248 | if (getStatus() == STATUS_QUEUED || getStatus() == STATUS_ABORT) | ||
249 | { | ||
250 | llerrs << "Attempt to delete a queued LLLFSThread::Request!" << llendl; | ||
251 | } | ||
252 | if (mOperation == FILE_WRITE) | ||
253 | { | ||
254 | if (mFlags & AUTO_DELETE) | ||
255 | { | ||
256 | delete mBuffer; | ||
257 | } | ||
258 | } | ||
259 | else if (mOperation == FILE_RENAME) | ||
260 | { | ||
261 | LLString* new_name = (LLString*)mBuffer; | ||
262 | delete new_name; | ||
263 | } | ||
264 | LLQueuedThread::QueuedRequest::deleteRequest(); | ||
265 | } | ||
266 | |||
267 | bool LLLFSThread::Request::processIO() | ||
268 | { | ||
269 | bool complete = false; | ||
270 | if (mOperation == FILE_READ) | ||
271 | { | ||
272 | llassert(mOffset >= 0); | ||
273 | apr_file_t* filep = ll_apr_file_open(mFileName, LL_APR_RB); | ||
274 | if (!filep) | ||
275 | { | ||
276 | llwarns << "LLLFS: Unable to read file: " << mFileName << llendl; | ||
277 | mBytesRead = 0; // fail | ||
278 | return true; | ||
279 | } | ||
280 | if (mOffset < 0) | ||
281 | ll_apr_file_seek(filep, APR_END, 0); | ||
282 | else | ||
283 | ll_apr_file_seek(filep, APR_SET, mOffset); | ||
284 | mBytesRead = ll_apr_file_read(filep, mBuffer, mBytes ); | ||
285 | apr_file_close(filep); | ||
286 | complete = true; | ||
287 | //llinfos << llformat("LLLFSThread::READ '%s': %d bytes",mFileName.c_str(),mBytesRead) << llendl; | ||
288 | } | ||
289 | else if (mOperation == FILE_WRITE) | ||
290 | { | ||
291 | apr_file_t* filep = ll_apr_file_open(mFileName, LL_APR_WB); | ||
292 | if (!filep) | ||
293 | { | ||
294 | llwarns << "LLLFS: Unable to write file: " << mFileName << llendl; | ||
295 | mBytesRead = 0; // fail | ||
296 | return true; | ||
297 | } | ||
298 | if (mOffset < 0) | ||
299 | ll_apr_file_seek(filep, APR_END, 0); | ||
300 | else | ||
301 | ll_apr_file_seek(filep, APR_SET, mOffset); | ||
302 | mBytesRead = ll_apr_file_write(filep, mBuffer, mBytes ); | ||
303 | complete = true; | ||
304 | apr_file_close(filep); | ||
305 | //llinfos << llformat("LLLFSThread::WRITE '%s': %d bytes",mFileName.c_str(),mBytesRead) << llendl; | ||
306 | } | ||
307 | else if (mOperation == FILE_RENAME) | ||
308 | { | ||
309 | LLString* new_name = (LLString*)mBuffer; | ||
310 | ll_apr_file_rename(mFileName, *new_name); | ||
311 | complete = true; | ||
312 | //llinfos << llformat("LLLFSThread::RENAME '%s': '%s'",mFileName.c_str(),new_name->c_str()) << llendl; | ||
313 | } | ||
314 | else if (mOperation == FILE_REMOVE) | ||
315 | { | ||
316 | ll_apr_file_remove(mFileName); | ||
317 | complete = true; | ||
318 | //llinfos << llformat("LLLFSThread::REMOVE '%s'",mFileName.c_str()) << llendl; | ||
319 | } | ||
320 | else | ||
321 | { | ||
322 | llerrs << llformat("LLLFSThread::unknown operation: %d", mOperation) << llendl; | ||
323 | } | ||
324 | return complete; | ||
325 | } | ||
326 | |||
327 | //============================================================================ | ||