1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/**
* @file llvfsthread.cpp
* @brief LLVFSThread implementation
*
* $LicenseInfo:firstyear=2001&license=viewergpl$
*
* Copyright (c) 2001-2009, Linden Research, Inc.
*
* Second Life Viewer Source Code
* The source code in this file ("Source Code") is provided by Linden Lab
* to you under the terms of the GNU General Public License, version 2.0
* ("GPL"), unless you have obtained a separate licensing agreement
* ("Other License"), formally executed by you and Linden Lab. Terms of
* the GPL can be found in doc/GPL-license.txt in this distribution, or
* online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution, or
* online at
* http://secondlifegrid.net/programs/open_source/licensing/flossexception
*
* By copying, modifying or distributing this software, you acknowledge
* that you have read and understood your obligations described above,
* and agree to abide by those obligations.
*
* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
* COMPLETENESS OR PERFORMANCE.
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llvfsthread.h"
#include "llstl.h"
//============================================================================
/*static*/ std::string LLVFSThread::sDataPath = "";
/*static*/ LLVFSThread* LLVFSThread::sLocal = NULL;
//============================================================================
// Run on MAIN thread
//static
void LLVFSThread::initClass(bool local_is_threaded)
{
llassert(sLocal == NULL);
sLocal = new LLVFSThread(local_is_threaded);
}
//static
S32 LLVFSThread::updateClass(U32 ms_elapsed)
{
sLocal->update(ms_elapsed);
return sLocal->getPending();
}
//static
void LLVFSThread::cleanupClass()
{
sLocal->setQuitting();
while (sLocal->getPending())
{
sLocal->update(0);
}
delete sLocal;
sLocal = 0;
}
//----------------------------------------------------------------------------
LLVFSThread::LLVFSThread(bool threaded) :
LLQueuedThread("VFS", threaded)
{
}
LLVFSThread::~LLVFSThread()
{
// ~LLQueuedThread() will be called here
}
//----------------------------------------------------------------------------
LLVFSThread::handle_t LLVFSThread::read(LLVFS* vfs, const LLUUID &file_id, const LLAssetType::EType file_type,
U8* buffer, S32 offset, S32 numbytes, U32 priority, U32 flags)
{
handle_t handle = generateHandle();
priority = llmax(priority, (U32)PRIORITY_LOW); // All reads are at least PRIORITY_LOW
Request* req = new Request(handle, priority, flags, FILE_READ, vfs, file_id, file_type,
buffer, offset, numbytes);
bool res = addRequest(req);
if (!res)
{
llerrs << "LLVFSThread::read called after LLVFSThread::cleanupClass()" << llendl;
req->deleteRequest();
handle = nullHandle();
}
return handle;
}
S32 LLVFSThread::readImmediate(LLVFS* vfs, const LLUUID &file_id, const LLAssetType::EType file_type,
U8* buffer, S32 offset, S32 numbytes)
{
handle_t handle = generateHandle();
Request* req = new Request(handle, PRIORITY_IMMEDIATE, 0, FILE_READ, vfs, file_id, file_type,
buffer, offset, numbytes);
S32 res = addRequest(req) ? 1 : 0;
if (res == 0)
{
llerrs << "LLVFSThread::read called after LLVFSThread::cleanupClass()" << llendl;
req->deleteRequest();
}
else
{
llverify(waitForResult(handle, false) == true);
res = req->getBytesRead();
completeRequest(handle);
}
return res;
}
LLVFSThread::handle_t LLVFSThread::write(LLVFS* vfs, const LLUUID &file_id, const LLAssetType::EType file_type,
U8* buffer, S32 offset, S32 numbytes, U32 flags)
{
handle_t handle = generateHandle();
Request* req = new Request(handle, 0, flags, FILE_WRITE, vfs, file_id, file_type,
buffer, offset, numbytes);
bool res = addRequest(req);
if (!res)
{
llerrs << "LLVFSThread::read called after LLVFSThread::cleanupClass()" << llendl;
req->deleteRequest();
handle = nullHandle();
}
return handle;
}
S32 LLVFSThread::writeImmediate(LLVFS* vfs, const LLUUID &file_id, const LLAssetType::EType file_type,
U8* buffer, S32 offset, S32 numbytes)
{
handle_t handle = generateHandle();
Request* req = new Request(handle, PRIORITY_IMMEDIATE, 0, FILE_WRITE, vfs, file_id, file_type,
buffer, offset, numbytes);
S32 res = addRequest(req) ? 1 : 0;
if (res == 0)
{
llerrs << "LLVFSThread::read called after LLVFSThread::cleanupClass()" << llendl;
req->deleteRequest();
}
else
{
llverify(waitForResult(handle, false) == true);
res = req->getBytesRead();
completeRequest(handle);
}
return res;
}
// LLVFSThread::handle_t LLVFSThread::rename(LLVFS* vfs, const LLUUID &file_id, const LLAssetType::EType file_type,
// const LLUUID &new_id, const LLAssetType::EType new_type, U32 flags)
// {
// handle_t handle = generateHandle();
// LLUUID* new_idp = new LLUUID(new_id); // deleted with Request
// // new_type is passed as "numbytes"
// Request* req = new Request(handle, 0, flags, FILE_RENAME, vfs, file_id, file_type,
// (U8*)new_idp, 0, (S32)new_type);
// bool res = addRequest(req);
// if (!res)
// {
// llerrs << "LLVFSThread::read called after LLVFSThread::cleanupClass()" << llendl;
// req->deleteRequest();
// handle = nullHandle();
// }
// return handle;
// }
//============================================================================
LLVFSThread::Request::Request(handle_t handle, U32 priority, U32 flags,
operation_t op, LLVFS* vfs,
const LLUUID &file_id, const LLAssetType::EType file_type,
U8* buffer, S32 offset, S32 numbytes) :
QueuedRequest(handle, priority, flags),
mOperation(op),
mVFS(vfs),
mFileID(file_id),
mFileType(file_type),
mBuffer(buffer),
mOffset(offset),
mBytes(numbytes),
mBytesRead(0)
{
llassert(mBuffer);
if (numbytes <= 0 && mOperation != FILE_RENAME)
{
llwarns << "LLVFSThread: Request with numbytes = " << numbytes
<< " operation = " << op
<< " offset " << offset
<< " file_type " << file_type << llendl;
}
if (mOperation == FILE_WRITE)
{
S32 blocksize = mVFS->getMaxSize(mFileID, mFileType);
if (blocksize < 0)
{
llwarns << "VFS write to temporary block (shouldn't happen)" << llendl;
}
mVFS->incLock(mFileID, mFileType, VFSLOCK_APPEND);
}
else if (mOperation == FILE_RENAME)
{
mVFS->incLock(mFileID, mFileType, VFSLOCK_APPEND);
}
else // if (mOperation == FILE_READ)
{
mVFS->incLock(mFileID, mFileType, VFSLOCK_READ);
}
}
// dec locks as soon as a request finishes
void LLVFSThread::Request::finishRequest(bool completed)
{
if (mOperation == FILE_WRITE)
{
mVFS->decLock(mFileID, mFileType, VFSLOCK_APPEND);
}
else if (mOperation == FILE_RENAME)
{
mVFS->decLock(mFileID, mFileType, VFSLOCK_APPEND);
}
else // if (mOperation == FILE_READ)
{
mVFS->decLock(mFileID, mFileType, VFSLOCK_READ);
}
}
void LLVFSThread::Request::deleteRequest()
{
if (getStatus() == STATUS_QUEUED)
{
llerrs << "Attempt to delete a queued LLVFSThread::Request!" << llendl;
}
if (mOperation == FILE_WRITE)
{
if (mFlags & FLAG_AUTO_DELETE)
{
delete [] mBuffer;
}
}
else if (mOperation == FILE_RENAME)
{
LLUUID* new_idp = (LLUUID*)mBuffer;
delete new_idp;
}
LLQueuedThread::QueuedRequest::deleteRequest();
}
bool LLVFSThread::Request::processRequest()
{
bool complete = false;
if (mOperation == FILE_READ)
{
llassert(mOffset >= 0);
mBytesRead = mVFS->getData(mFileID, mFileType, mBuffer, mOffset, mBytes);
complete = true;
//llinfos << llformat("LLVFSThread::READ '%s': %d bytes arg:%d",getFilename(),mBytesRead) << llendl;
}
else if (mOperation == FILE_WRITE)
{
mBytesRead = mVFS->storeData(mFileID, mFileType, mBuffer, mOffset, mBytes);
complete = true;
//llinfos << llformat("LLVFSThread::WRITE '%s': %d bytes arg:%d",getFilename(),mBytesRead) << llendl;
}
else if (mOperation == FILE_RENAME)
{
LLUUID* new_idp = (LLUUID*)mBuffer;
LLAssetType::EType new_type = (LLAssetType::EType)mBytes;
mVFS->renameFile(mFileID, mFileType, *new_idp, new_type);
mFileID = *new_idp;
complete = true;
//llinfos << llformat("LLVFSThread::RENAME '%s': %d bytes arg:%d",getFilename(),mBytesRead) << llendl;
}
else
{
llerrs << llformat("LLVFSThread::unknown operation: %d", mOperation) << llendl;
}
return complete;
}
//============================================================================
|