sl@0
|
1 |
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
#include "scsiprot.h"
|
sl@0
|
17 |
#include "usbmsshared.h"
|
sl@0
|
18 |
#include "rwdrivethread.h"
|
sl@0
|
19 |
#include "massstoragedebug.h"
|
sl@0
|
20 |
|
sl@0
|
21 |
// ---
|
sl@0
|
22 |
|
sl@0
|
23 |
#ifdef PRINT_MSDC_MULTITHREADED_READ_INFO
|
sl@0
|
24 |
#define __MT_READ_PRINT(t) {RDebug::Print(t);}
|
sl@0
|
25 |
#define __MT_READ_PRINT1(t,a) {RDebug::Print(t,a);}
|
sl@0
|
26 |
#define __MT_READ_PRINT2(t,a,b) {RDebug::Print(t,a,b);}
|
sl@0
|
27 |
#else
|
sl@0
|
28 |
#define __MT_READ_PRINT(t)
|
sl@0
|
29 |
#define __MT_READ_PRINT1(t,a)
|
sl@0
|
30 |
#define __MT_READ_PRINT2(t,a,b)
|
sl@0
|
31 |
#endif // PRINT_MSDC_MULTITHREADED_READ_INFO
|
sl@0
|
32 |
|
sl@0
|
33 |
|
sl@0
|
34 |
#ifdef MSDC_MULTITHREADED
|
sl@0
|
35 |
|
sl@0
|
36 |
TBlockDesc::TBlockDesc()
|
sl@0
|
37 |
:iBuf((TUint8 *)NULL,0,0)
|
sl@0
|
38 |
{
|
sl@0
|
39 |
}
|
sl@0
|
40 |
|
sl@0
|
41 |
void TBlockDesc::SetPtr(TPtr8& aDes)
|
sl@0
|
42 |
{
|
sl@0
|
43 |
iBuf.Set(aDes);
|
sl@0
|
44 |
}
|
sl@0
|
45 |
|
sl@0
|
46 |
|
sl@0
|
47 |
TBlockDescBuffer::TBlockDescBuffer()
|
sl@0
|
48 |
{
|
sl@0
|
49 |
iDescReadPtr = &iDesc1;
|
sl@0
|
50 |
iDescWritePtr = &iDesc2;
|
sl@0
|
51 |
}
|
sl@0
|
52 |
|
sl@0
|
53 |
void TBlockDescBuffer::SetUpReadBuf(TPtr8& aDes1, TPtr8& aDes2)
|
sl@0
|
54 |
{
|
sl@0
|
55 |
iDesc1.SetPtr(aDes1);
|
sl@0
|
56 |
iDesc2.SetPtr(aDes2);
|
sl@0
|
57 |
iDescReadPtr = &iDesc1;
|
sl@0
|
58 |
iDescWritePtr = &iDesc2;
|
sl@0
|
59 |
}
|
sl@0
|
60 |
|
sl@0
|
61 |
|
sl@0
|
62 |
//-----------------------------------------------
|
sl@0
|
63 |
|
sl@0
|
64 |
/**
|
sl@0
|
65 |
Construct a CThreadContext object.
|
sl@0
|
66 |
|
sl@0
|
67 |
@param aName The name to be assigned to this thread.
|
sl@0
|
68 |
@param aThreadFunction Function to be called when thread is initially scheduled.
|
sl@0
|
69 |
@param aOwner Pointer to the object owning the thread. Used as the parameter to aThreadFunction.
|
sl@0
|
70 |
*/
|
sl@0
|
71 |
CThreadContext* CThreadContext::NewL(const TDesC& aName,
|
sl@0
|
72 |
TThreadFunction aThreadFunction,
|
sl@0
|
73 |
TAny* aOwner)
|
sl@0
|
74 |
{
|
sl@0
|
75 |
__FNLOG("CThreadContext::NewL");
|
sl@0
|
76 |
CThreadContext* self = new (ELeave) CThreadContext();
|
sl@0
|
77 |
CleanupStack::PushL(self);
|
sl@0
|
78 |
self->ConstructL(aName, aThreadFunction, aOwner);
|
sl@0
|
79 |
CleanupStack::Pop();
|
sl@0
|
80 |
return self;
|
sl@0
|
81 |
}
|
sl@0
|
82 |
|
sl@0
|
83 |
/**
|
sl@0
|
84 |
Construct a CThreadContext object
|
sl@0
|
85 |
|
sl@0
|
86 |
@param aName The name to be assigned to this thread.
|
sl@0
|
87 |
@param aThreadFunction Function to be called when thread is initially scheduled.
|
sl@0
|
88 |
@param aOwner Pointer to the object owning the thread. Used as the parameter to aThreadFunction.
|
sl@0
|
89 |
*/
|
sl@0
|
90 |
void CThreadContext::ConstructL(const TDesC& aName,
|
sl@0
|
91 |
TThreadFunction aThreadFunction,
|
sl@0
|
92 |
TAny* aOwner)
|
sl@0
|
93 |
{
|
sl@0
|
94 |
__FNLOG("CThreadContext::ConstructL");
|
sl@0
|
95 |
__PRINT(_L("Creating Critical Section"));
|
sl@0
|
96 |
User::LeaveIfError(iCritSect.CreateLocal());
|
sl@0
|
97 |
__PRINT(_L("Creating RThread"));
|
sl@0
|
98 |
|
sl@0
|
99 |
TUint serial(0); // Used to retry creation of a thread in case
|
sl@0
|
100 |
// one with the same name already exists
|
sl@0
|
101 |
|
sl@0
|
102 |
RBuf threadName;
|
sl@0
|
103 |
threadName.CreateMaxL(aName.Length() + 8);
|
sl@0
|
104 |
CleanupClosePushL(threadName);
|
sl@0
|
105 |
threadName = aName;
|
sl@0
|
106 |
|
sl@0
|
107 |
TInt err;
|
sl@0
|
108 |
for (;;)
|
sl@0
|
109 |
{
|
sl@0
|
110 |
err = iThread.Create(threadName, aThreadFunction, 0x1000, NULL, aOwner);
|
sl@0
|
111 |
__PRINT2(_L("CThreadContext::ConstructL Created thread %S err=%d"), &threadName, err);
|
sl@0
|
112 |
|
sl@0
|
113 |
// for a restart wait and retry until old thread is gone
|
sl@0
|
114 |
if (err == KErrAlreadyExists)
|
sl@0
|
115 |
{
|
sl@0
|
116 |
User::After(10 * 1000); // 10 mS
|
sl@0
|
117 |
threadName = aName;
|
sl@0
|
118 |
threadName.AppendNumFixedWidth(serial, EDecimal, 8);
|
sl@0
|
119 |
++serial;
|
sl@0
|
120 |
}
|
sl@0
|
121 |
else
|
sl@0
|
122 |
{
|
sl@0
|
123 |
break;
|
sl@0
|
124 |
}
|
sl@0
|
125 |
}
|
sl@0
|
126 |
|
sl@0
|
127 |
User::LeaveIfError(err);
|
sl@0
|
128 |
CleanupStack::Pop(); // threadName
|
sl@0
|
129 |
threadName.Close();
|
sl@0
|
130 |
|
sl@0
|
131 |
// set priority
|
sl@0
|
132 |
iThread.SetPriority(EPriorityMore);
|
sl@0
|
133 |
}
|
sl@0
|
134 |
|
sl@0
|
135 |
|
sl@0
|
136 |
/**
|
sl@0
|
137 |
Construct a CThreadContext object
|
sl@0
|
138 |
*/
|
sl@0
|
139 |
CThreadContext::CThreadContext()
|
sl@0
|
140 |
:
|
sl@0
|
141 |
iError(KErrNone)
|
sl@0
|
142 |
{
|
sl@0
|
143 |
__FNLOG("CThreadContext::CThreadContext");
|
sl@0
|
144 |
}
|
sl@0
|
145 |
|
sl@0
|
146 |
/**
|
sl@0
|
147 |
Destructor
|
sl@0
|
148 |
*/
|
sl@0
|
149 |
CThreadContext::~CThreadContext()
|
sl@0
|
150 |
{
|
sl@0
|
151 |
__FNLOG("CThreadContext::~CThreadContext");
|
sl@0
|
152 |
__PRINT(_L("Closing Critical Section"));
|
sl@0
|
153 |
iCritSect.Close();
|
sl@0
|
154 |
__PRINT(_L("Killing ThreadContext"));
|
sl@0
|
155 |
iThread.Kill(0);
|
sl@0
|
156 |
__PRINT(_L("Closing ThreadContext"));
|
sl@0
|
157 |
iThread.Close();
|
sl@0
|
158 |
}
|
sl@0
|
159 |
|
sl@0
|
160 |
//-----------------------------------------------
|
sl@0
|
161 |
|
sl@0
|
162 |
/**
|
sl@0
|
163 |
Construct a CWriteDriveThread object
|
sl@0
|
164 |
*/
|
sl@0
|
165 |
CWriteDriveThread* CWriteDriveThread::NewL()
|
sl@0
|
166 |
{
|
sl@0
|
167 |
__FNLOG("CWriteDriveThread::NewL");
|
sl@0
|
168 |
CWriteDriveThread* self = new (ELeave) CWriteDriveThread();
|
sl@0
|
169 |
CleanupStack::PushL(self);
|
sl@0
|
170 |
self->ConstructL();
|
sl@0
|
171 |
CleanupStack::Pop();
|
sl@0
|
172 |
return self;
|
sl@0
|
173 |
}
|
sl@0
|
174 |
|
sl@0
|
175 |
/**
|
sl@0
|
176 |
Construct a CWriteDriveThread object
|
sl@0
|
177 |
*/
|
sl@0
|
178 |
void CWriteDriveThread::ConstructL()
|
sl@0
|
179 |
{
|
sl@0
|
180 |
__FNLOG("CWriteDriveThread::ConstructL");
|
sl@0
|
181 |
TBuf<16> name = _L("MassStorageWrite");
|
sl@0
|
182 |
iThreadContext = CThreadContext::NewL(name, ThreadFunction, this);
|
sl@0
|
183 |
// There are two free pointers to start with so initialise the semaphore with 1
|
sl@0
|
184 |
User::LeaveIfError(iProducerSem.CreateLocal(1));
|
sl@0
|
185 |
User::LeaveIfError(iConsumerSem.CreateLocal(0));
|
sl@0
|
186 |
|
sl@0
|
187 |
iThreadContext->Resume();
|
sl@0
|
188 |
}
|
sl@0
|
189 |
|
sl@0
|
190 |
/**
|
sl@0
|
191 |
Construct a CWriteDriveThread object
|
sl@0
|
192 |
*/
|
sl@0
|
193 |
CWriteDriveThread::CWriteDriveThread()
|
sl@0
|
194 |
: iIsCommandWrite10(EFalse)
|
sl@0
|
195 |
{
|
sl@0
|
196 |
__FNLOG("CWriteDriveThread::CWriteDriveThread");
|
sl@0
|
197 |
}
|
sl@0
|
198 |
|
sl@0
|
199 |
/**
|
sl@0
|
200 |
Destructor
|
sl@0
|
201 |
*/
|
sl@0
|
202 |
CWriteDriveThread::~CWriteDriveThread()
|
sl@0
|
203 |
{
|
sl@0
|
204 |
__FNLOG("CWriteDriveThread::~CWriteDriveThread");
|
sl@0
|
205 |
delete iThreadContext;
|
sl@0
|
206 |
}
|
sl@0
|
207 |
|
sl@0
|
208 |
/**
|
sl@0
|
209 |
This function is called when the thread is initially scheduled.
|
sl@0
|
210 |
|
sl@0
|
211 |
@param aSelf Pointer to self to facilitate call to member method.
|
sl@0
|
212 |
*/
|
sl@0
|
213 |
TInt CWriteDriveThread::ThreadFunction(TAny* aSelf)
|
sl@0
|
214 |
{
|
sl@0
|
215 |
__FNLOG("CWriteDriveThread::ThreadFunction");
|
sl@0
|
216 |
CWriteDriveThread* self = static_cast<CWriteDriveThread*>(aSelf);
|
sl@0
|
217 |
return self->WriteToDrive();
|
sl@0
|
218 |
}
|
sl@0
|
219 |
|
sl@0
|
220 |
/**
|
sl@0
|
221 |
Writes the data pointed to by iDescWritePtr to the drive.
|
sl@0
|
222 |
*/
|
sl@0
|
223 |
TInt CWriteDriveThread::WriteToDrive()
|
sl@0
|
224 |
{
|
sl@0
|
225 |
__FNLOG("\tCWriteDriveThread::WriteToDrive");
|
sl@0
|
226 |
|
sl@0
|
227 |
// One-off convenience variable assignment
|
sl@0
|
228 |
TBlockDesc* &desc = iThreadContext->iBuffer.iDescWritePtr;
|
sl@0
|
229 |
|
sl@0
|
230 |
for(;;)
|
sl@0
|
231 |
{
|
sl@0
|
232 |
iConsumerSem.Wait();
|
sl@0
|
233 |
__PRINT(_L("\tWaiting on Write CS..."));
|
sl@0
|
234 |
iThreadContext->iCritSect.Wait();
|
sl@0
|
235 |
// +++ WRITE CS STARTS HERE +++
|
sl@0
|
236 |
__PRINT1(_L("\tNow using as write buffer: iBuf%d"), iThreadContext->iBuffer.GetBufferNumber(&desc->iBuf));
|
sl@0
|
237 |
#ifdef MEASURE_AND_DISPLAY_WRITE_TIME
|
sl@0
|
238 |
RDebug::Print(_L("\tSCSI: writing %d bytes\n"), desc->iBuf.Length());
|
sl@0
|
239 |
TTime t0, t1;
|
sl@0
|
240 |
t0.HomeTime();
|
sl@0
|
241 |
#else
|
sl@0
|
242 |
__PRINT1(_L("\tSCSI: writing %d bytes\n"), desc->iBuf.Length());
|
sl@0
|
243 |
#endif
|
sl@0
|
244 |
// Write buffer to disk
|
sl@0
|
245 |
|
sl@0
|
246 |
#ifdef INJECT_ERROR
|
sl@0
|
247 |
if (desc->iBuf[0] == '2')
|
sl@0
|
248 |
{
|
sl@0
|
249 |
desc->iBuf[0] = 'x';
|
sl@0
|
250 |
RDebug::Printf("Injecting error");
|
sl@0
|
251 |
}
|
sl@0
|
252 |
|
sl@0
|
253 |
|
sl@0
|
254 |
RDebug::Printf("%08lx %x [%x] [%x]", desc->iByteOffset, desc->iBuf.Length(),
|
sl@0
|
255 |
desc->iBuf[0],
|
sl@0
|
256 |
desc->iBuf[desc->iBuf.Length()-1]);
|
sl@0
|
257 |
#endif
|
sl@0
|
258 |
|
sl@0
|
259 |
iThreadContext->iError = iThreadContext->iDrive->Write(desc->iByteOffset, desc->iBuf,iThreadContext->iDrive->IsWholeMediaAccess());
|
sl@0
|
260 |
#ifdef INJECT_ERROR
|
sl@0
|
261 |
if (desc->iBuf[0] == 'x')
|
sl@0
|
262 |
{
|
sl@0
|
263 |
iThreadContext->iError = KErrUnknown;
|
sl@0
|
264 |
}
|
sl@0
|
265 |
#endif
|
sl@0
|
266 |
|
sl@0
|
267 |
#ifdef MEASURE_AND_DISPLAY_WRITE_TIME
|
sl@0
|
268 |
t1.HomeTime();
|
sl@0
|
269 |
const TTimeIntervalMicroSeconds time = t1.MicroSecondsFrom(t0);
|
sl@0
|
270 |
const TUint time_ms = I64LOW(time.Int64() / 1000);
|
sl@0
|
271 |
RDebug::Print(_L("SCSI: write took %d ms\n"), time_ms);
|
sl@0
|
272 |
#endif
|
sl@0
|
273 |
iCallback((TUint8*) (desc->iBuf.Ptr()), iCallbackParameter);
|
sl@0
|
274 |
iWriteCounter--;
|
sl@0
|
275 |
ASSERT(iWriteCounter >= 0);
|
sl@0
|
276 |
|
sl@0
|
277 |
__PRINT(_L("\tSignalling Write CS"));
|
sl@0
|
278 |
iThreadContext->iCritSect.Signal();
|
sl@0
|
279 |
// +++ WRITE CS ENDS HERE +++
|
sl@0
|
280 |
iProducerSem.Signal();
|
sl@0
|
281 |
}
|
sl@0
|
282 |
}
|
sl@0
|
283 |
|
sl@0
|
284 |
/**
|
sl@0
|
285 |
Initiates writing data pointed to by iReadBuf to the drive and resumes the thread. Writing
|
sl@0
|
286 |
is completed by the ThreadFunction when the thread is resumed.
|
sl@0
|
287 |
|
sl@0
|
288 |
@param aDrive Drive to write to.
|
sl@0
|
289 |
@param aOffset Write offset.
|
sl@0
|
290 |
*/
|
sl@0
|
291 |
TInt CWriteDriveThread::WriteDriveData(CMassStorageDrive* aDrive, const TInt64& aOffset, TPtrC8& aDes, ProcessWriteCompleteFunc aFunc, TAny* aPtr)
|
sl@0
|
292 |
{
|
sl@0
|
293 |
// Check error code from previous write
|
sl@0
|
294 |
const TInt r = iThreadContext->iError;
|
sl@0
|
295 |
if (r != KErrNone)
|
sl@0
|
296 |
{
|
sl@0
|
297 |
__PRINT1(_L("Error after previous write = 0x%x \n"), r);
|
sl@0
|
298 |
return KErrAbort;
|
sl@0
|
299 |
}
|
sl@0
|
300 |
|
sl@0
|
301 |
// Swap the two buffer pointers
|
sl@0
|
302 |
iProducerSem.Wait();
|
sl@0
|
303 |
__PRINT(_L("Waiting on Write CS..."));
|
sl@0
|
304 |
// +++ WRITE CS STARTS HERE +++
|
sl@0
|
305 |
iThreadContext->iCritSect.Wait();
|
sl@0
|
306 |
|
sl@0
|
307 |
// New DB First read into the iDescReadPtr pointer,
|
sl@0
|
308 |
// then swap,so that write pointer points to correct location, as the ptr pointed to by iDescWritePtr is what is written from in WriteToDrive
|
sl@0
|
309 |
iThreadContext->iBuffer.iDescReadPtr->iBuf.Set((TUint8*)aDes.Ptr(), aDes.Length(), KMaxBufSize );
|
sl@0
|
310 |
|
sl@0
|
311 |
iCallback = aFunc;
|
sl@0
|
312 |
iCallbackParameter = aPtr;
|
sl@0
|
313 |
|
sl@0
|
314 |
iWriteCounter++;
|
sl@0
|
315 |
iThreadContext->iBuffer.SwapDesc();
|
sl@0
|
316 |
// Prepare variables for next write
|
sl@0
|
317 |
iThreadContext->iDrive = aDrive;
|
sl@0
|
318 |
iThreadContext->iBuffer.iDescWritePtr->iByteOffset = aOffset;
|
sl@0
|
319 |
// +++ WRITE CS ENDS HERE +++
|
sl@0
|
320 |
__PRINT(_L("Signalling Write CS..."));
|
sl@0
|
321 |
iThreadContext->iCritSect.Signal();
|
sl@0
|
322 |
|
sl@0
|
323 |
iConsumerSem.Signal();
|
sl@0
|
324 |
return KErrNone;
|
sl@0
|
325 |
}
|
sl@0
|
326 |
|
sl@0
|
327 |
|
sl@0
|
328 |
void CWriteDriveThread::WaitForWriteEmpty()
|
sl@0
|
329 |
{
|
sl@0
|
330 |
while(iWriteCounter > 0)
|
sl@0
|
331 |
{
|
sl@0
|
332 |
User::After(100);
|
sl@0
|
333 |
}
|
sl@0
|
334 |
}
|
sl@0
|
335 |
|
sl@0
|
336 |
// Check if the target address range was recently written to, this is to force a
|
sl@0
|
337 |
// cache miss when reading from the same sectors that were just written.
|
sl@0
|
338 |
// Optimisation note: this is only needed if the read precache was started
|
sl@0
|
339 |
// before the write was completed.
|
sl@0
|
340 |
TBool CWriteDriveThread::IsRecentlyWritten(TInt64 aOffset, TInt aLength)
|
sl@0
|
341 |
{
|
sl@0
|
342 |
ASSERT(iWriteCounter == 0);
|
sl@0
|
343 |
if (iIsCommandWrite10) //If the previous command is Write10, then discard pre-read as the same buffers are used and will be over written by Write10
|
sl@0
|
344 |
return ETrue;
|
sl@0
|
345 |
if(aOffset <= iThreadContext->iBuffer.iDescReadPtr->iByteOffset &&
|
sl@0
|
346 |
aOffset + aLength >= iThreadContext->iBuffer.iDescReadPtr->iByteOffset)
|
sl@0
|
347 |
return ETrue;
|
sl@0
|
348 |
if(aOffset >= iThreadContext->iBuffer.iDescReadPtr->iByteOffset &&
|
sl@0
|
349 |
aOffset <= iThreadContext->iBuffer.iDescReadPtr->iByteOffset + iThreadContext->iBuffer.iDescReadPtr->iLength)
|
sl@0
|
350 |
return ETrue;
|
sl@0
|
351 |
if(aOffset <= iThreadContext->iBuffer.iDescWritePtr->iByteOffset &&
|
sl@0
|
352 |
aOffset + aLength >= iThreadContext->iBuffer.iDescReadPtr->iByteOffset)
|
sl@0
|
353 |
return ETrue;
|
sl@0
|
354 |
if(aOffset >= iThreadContext->iBuffer.iDescWritePtr->iByteOffset &&
|
sl@0
|
355 |
aOffset <= iThreadContext->iBuffer.iDescReadPtr->iByteOffset + iThreadContext->iBuffer.iDescReadPtr->iLength)
|
sl@0
|
356 |
return ETrue;
|
sl@0
|
357 |
return EFalse;
|
sl@0
|
358 |
}
|
sl@0
|
359 |
|
sl@0
|
360 |
//-----------------------------------------------
|
sl@0
|
361 |
|
sl@0
|
362 |
/**
|
sl@0
|
363 |
Construct a CReadDriveThread object
|
sl@0
|
364 |
*/
|
sl@0
|
365 |
CReadDriveThread* CReadDriveThread::NewL()
|
sl@0
|
366 |
{
|
sl@0
|
367 |
__FNLOG("CReadDriveThread::NewL");
|
sl@0
|
368 |
CReadDriveThread* self = new (ELeave) CReadDriveThread();
|
sl@0
|
369 |
CleanupStack::PushL(self);
|
sl@0
|
370 |
self->ConstructL();
|
sl@0
|
371 |
CleanupStack::Pop();
|
sl@0
|
372 |
return self;
|
sl@0
|
373 |
}
|
sl@0
|
374 |
|
sl@0
|
375 |
/**
|
sl@0
|
376 |
Construct a CReadDriveThread object
|
sl@0
|
377 |
|
sl@0
|
378 |
@param aName The name to be assigned to this thread.
|
sl@0
|
379 |
@pram aThreadFunction Function to be called when thread is initially scheduled.
|
sl@0
|
380 |
*/
|
sl@0
|
381 |
void CReadDriveThread::ConstructL()
|
sl@0
|
382 |
{
|
sl@0
|
383 |
__FNLOG("CReadDriveThread::ConstructL");
|
sl@0
|
384 |
TBuf<15> name = _L("MassStorageRead");
|
sl@0
|
385 |
iThreadContext = CThreadContext::NewL(name, ThreadFunction, this);
|
sl@0
|
386 |
}
|
sl@0
|
387 |
|
sl@0
|
388 |
/**
|
sl@0
|
389 |
Construct a CReadDriveThread object
|
sl@0
|
390 |
*/
|
sl@0
|
391 |
CReadDriveThread::CReadDriveThread()
|
sl@0
|
392 |
:
|
sl@0
|
393 |
iThreadRunning(EFalse)
|
sl@0
|
394 |
{
|
sl@0
|
395 |
__FNLOG("CReadDriveThread::CReadDriveThread");
|
sl@0
|
396 |
}
|
sl@0
|
397 |
|
sl@0
|
398 |
/**
|
sl@0
|
399 |
Destructor
|
sl@0
|
400 |
*/
|
sl@0
|
401 |
CReadDriveThread::~CReadDriveThread()
|
sl@0
|
402 |
{
|
sl@0
|
403 |
__FNLOG("CReadDriveThread::~CReadDriveThread");
|
sl@0
|
404 |
delete iThreadContext;
|
sl@0
|
405 |
}
|
sl@0
|
406 |
|
sl@0
|
407 |
/**
|
sl@0
|
408 |
This function is called when the thread is initially scheduled.
|
sl@0
|
409 |
|
sl@0
|
410 |
@param aSelf Pointer to self to facilitate call to member method.
|
sl@0
|
411 |
*/
|
sl@0
|
412 |
TInt CReadDriveThread::ThreadFunction(TAny* aSelf)
|
sl@0
|
413 |
{
|
sl@0
|
414 |
__FNLOG("CReadDriveThread::ThreadFunction");
|
sl@0
|
415 |
CReadDriveThread* self = static_cast<CReadDriveThread*>(aSelf);
|
sl@0
|
416 |
return self->ReadFromDrive();
|
sl@0
|
417 |
}
|
sl@0
|
418 |
|
sl@0
|
419 |
/**
|
sl@0
|
420 |
Reads data from the drive with iOffset and iReadLength into memory pointer iReadBuffer
|
sl@0
|
421 |
and suspends the thread.
|
sl@0
|
422 |
*/
|
sl@0
|
423 |
TInt CReadDriveThread::ReadFromDrive()
|
sl@0
|
424 |
{
|
sl@0
|
425 |
__FNLOG("\tCReadDriveThread::ReadFromDrive");
|
sl@0
|
426 |
|
sl@0
|
427 |
// One-off convenience variable assignment
|
sl@0
|
428 |
TBlockDesc* &desc = iThreadContext->iBuffer.iDescWritePtr;
|
sl@0
|
429 |
|
sl@0
|
430 |
for (;;)
|
sl@0
|
431 |
{
|
sl@0
|
432 |
__PRINT(_L("\tWaiting on Read CS..."));
|
sl@0
|
433 |
iThreadContext->iCritSect.Wait();
|
sl@0
|
434 |
// +++ READ CS STARTS HERE +++
|
sl@0
|
435 |
iThreadRunning = ETrue;
|
sl@0
|
436 |
iCompleted = EFalse;
|
sl@0
|
437 |
|
sl@0
|
438 |
__PRINT1(_L("\tNow using as read buffer: iBuf%d"), iThreadContext->iBuffer.GetBufferNumber(&desc->iBuf));
|
sl@0
|
439 |
|
sl@0
|
440 |
#ifdef MEASURE_AND_DISPLAY_READ_TIME
|
sl@0
|
441 |
RDebug::Print(_L("\tSCSI: reading %d bytes\n"), desc->iBuf.Length());
|
sl@0
|
442 |
TTime t0, t1;
|
sl@0
|
443 |
t0.HomeTime();
|
sl@0
|
444 |
#else
|
sl@0
|
445 |
__PRINT1(_L("\tSCSI: reading %d bytes\n"), desc->iBuf.Length());
|
sl@0
|
446 |
#endif
|
sl@0
|
447 |
// Fill read buffer from disk
|
sl@0
|
448 |
iThreadContext->iError = iThreadContext->iDrive->Read(desc->iByteOffset,
|
sl@0
|
449 |
desc->iLength,
|
sl@0
|
450 |
desc->iBuf,
|
sl@0
|
451 |
iThreadContext->iDrive->IsWholeMediaAccess());
|
sl@0
|
452 |
|
sl@0
|
453 |
#ifdef MEASURE_AND_DISPLAY_READ_TIME
|
sl@0
|
454 |
t1.HomeTime();
|
sl@0
|
455 |
const TTimeIntervalMicroSeconds time = t1.MicroSecondsFrom(t0);
|
sl@0
|
456 |
const TUint time_ms = I64LOW(time.Int64() / 1000);
|
sl@0
|
457 |
RDebug::Print(_L("SCSI: read took %d ms\n"), time_ms);
|
sl@0
|
458 |
#endif
|
sl@0
|
459 |
|
sl@0
|
460 |
iCompleted = ETrue;
|
sl@0
|
461 |
iThreadRunning = EFalse;
|
sl@0
|
462 |
__PRINT(_L("\tSignalling Read CS"));
|
sl@0
|
463 |
// +++ READ CS ENDS HERE +++
|
sl@0
|
464 |
iThreadContext->iCritSect.Signal();
|
sl@0
|
465 |
// Suspend self
|
sl@0
|
466 |
__PRINT(_L("\tSuspending Read Thread"));
|
sl@0
|
467 |
RThread().Suspend();
|
sl@0
|
468 |
}
|
sl@0
|
469 |
}
|
sl@0
|
470 |
|
sl@0
|
471 |
/**
|
sl@0
|
472 |
Client read request of a data block from the specified drive.
|
sl@0
|
473 |
If there is no pre-read data that matches the requested Offset and Length then the drive
|
sl@0
|
474 |
is read and the next pre-read is setup. If there is matching pre-read data available then
|
sl@0
|
475 |
the next pre-read is setup. Finishes by resuming the thread and the ThreadFunciton runs.
|
sl@0
|
476 |
|
sl@0
|
477 |
@param aDrive Drive to read from.
|
sl@0
|
478 |
@param aOffset Read offset
|
sl@0
|
479 |
@param aLength Length
|
sl@0
|
480 |
*/
|
sl@0
|
481 |
TBool CReadDriveThread::ReadDriveData(CMassStorageDrive* aDrive,
|
sl@0
|
482 |
const TInt64& aOffset,
|
sl@0
|
483 |
TUint32 aLength,
|
sl@0
|
484 |
TBool aIgnoreCache)
|
sl@0
|
485 |
{
|
sl@0
|
486 |
__MT_READ_PRINT2(_L("\nRead10: offs %ld len %d"), aOffset, aLength);
|
sl@0
|
487 |
|
sl@0
|
488 |
__PRINT(_L("Waiting on Read CS..."));
|
sl@0
|
489 |
iThreadContext->iCritSect.Wait();
|
sl@0
|
490 |
// +++ READ CS STARTS HERE +++
|
sl@0
|
491 |
__ASSERT_DEBUG(!iThreadRunning, User::Panic(_L("MSDC-THREAD"), 666));
|
sl@0
|
492 |
|
sl@0
|
493 |
TBlockDesc* &desc = iThreadContext->iBuffer.iDescReadPtr;
|
sl@0
|
494 |
TBlockDesc* &bgDesc = iThreadContext->iBuffer.iDescWritePtr;
|
sl@0
|
495 |
|
sl@0
|
496 |
if ((!aIgnoreCache) &&
|
sl@0
|
497 |
(iCompleted) &&
|
sl@0
|
498 |
(iThreadContext->iError == KErrNone) &&
|
sl@0
|
499 |
(iThreadContext->iDrive == aDrive) &&
|
sl@0
|
500 |
(bgDesc->iByteOffset == aOffset) &&
|
sl@0
|
501 |
(bgDesc->iLength == aLength))
|
sl@0
|
502 |
{
|
sl@0
|
503 |
// Good: We pre-read the correct data :-)
|
sl@0
|
504 |
__MT_READ_PRINT(_L("Match: Using pre-read data :-) :-) :-) :-)"));
|
sl@0
|
505 |
}
|
sl@0
|
506 |
else
|
sl@0
|
507 |
{
|
sl@0
|
508 |
__MT_READ_PRINT(_L("Not using pre-read data"));
|
sl@0
|
509 |
if (iThreadContext->iError != KErrNone)
|
sl@0
|
510 |
{
|
sl@0
|
511 |
__MT_READ_PRINT1(_L("Pre-read failed: %d"), iThreadContext->iError);
|
sl@0
|
512 |
}
|
sl@0
|
513 |
if (iThreadContext->iDrive != aDrive)
|
sl@0
|
514 |
{
|
sl@0
|
515 |
__MT_READ_PRINT2(_L("Pre-read drive mismatch: pre 0x%08x / act 0x%08x"),
|
sl@0
|
516 |
iThreadContext->iDrive, aDrive);
|
sl@0
|
517 |
}
|
sl@0
|
518 |
if (desc->iByteOffset != aOffset)
|
sl@0
|
519 |
{
|
sl@0
|
520 |
__MT_READ_PRINT2(_L("Pre-read offset mismatch: pre %ld / act %ld"),
|
sl@0
|
521 |
desc->iByteOffset, aOffset);
|
sl@0
|
522 |
}
|
sl@0
|
523 |
if (desc->iLength != aLength)
|
sl@0
|
524 |
{
|
sl@0
|
525 |
__MT_READ_PRINT2(_L("Pre-read length mismatch: pre %d / act %d"),
|
sl@0
|
526 |
desc->iLength, aLength);
|
sl@0
|
527 |
// Potential optimization: If the pre-read was OK but for more data
|
sl@0
|
528 |
// than the host is now asking for, we could still satisfy that
|
sl@0
|
529 |
// request from the pre-read data by shortening the buffer.
|
sl@0
|
530 |
}
|
sl@0
|
531 |
// No valid pre-read data was available - so we have to read it now
|
sl@0
|
532 |
bgDesc->iByteOffset = aOffset;
|
sl@0
|
533 |
bgDesc->iLength = aLength;
|
sl@0
|
534 |
TInt err = aDrive->Read(aOffset,
|
sl@0
|
535 |
aLength,
|
sl@0
|
536 |
bgDesc->iBuf,
|
sl@0
|
537 |
aDrive->IsWholeMediaAccess());
|
sl@0
|
538 |
if (err != KErrNone)
|
sl@0
|
539 |
{
|
sl@0
|
540 |
__PRINT1(_L("Read failed, err=%d\n"), err);
|
sl@0
|
541 |
// +++ READ CS ENDS HERE +++
|
sl@0
|
542 |
__PRINT(_L("Signalling Read CS..."));
|
sl@0
|
543 |
iThreadContext->iCritSect.Signal();
|
sl@0
|
544 |
return EFalse;
|
sl@0
|
545 |
}
|
sl@0
|
546 |
}
|
sl@0
|
547 |
|
sl@0
|
548 |
// Prepare thread variables for next pre-read attempt by the ReadThread
|
sl@0
|
549 |
const TInt64 offs_new = aOffset + aLength;
|
sl@0
|
550 |
iThreadContext->iDrive = aDrive; // same drive
|
sl@0
|
551 |
desc->iByteOffset = offs_new; // next block
|
sl@0
|
552 |
desc->iLength = aLength; // same length
|
sl@0
|
553 |
iCompleted = EFalse;
|
sl@0
|
554 |
iThreadContext->iBuffer.SwapDesc();
|
sl@0
|
555 |
|
sl@0
|
556 |
// +++ READ CS ENDS HERE +++
|
sl@0
|
557 |
__PRINT(_L("Signalling Read CS..."));
|
sl@0
|
558 |
iThreadContext->iCritSect.Signal();
|
sl@0
|
559 |
// Start background read
|
sl@0
|
560 |
__PRINT(_L("Resuming Read Thread"));
|
sl@0
|
561 |
iThreadContext->Resume();
|
sl@0
|
562 |
return ETrue;
|
sl@0
|
563 |
}
|
sl@0
|
564 |
|
sl@0
|
565 |
/**
|
sl@0
|
566 |
Discard the read buffer. This is used to force a cache miss when reading from
|
sl@0
|
567 |
the same sectors that were just written.
|
sl@0
|
568 |
*/
|
sl@0
|
569 |
void CReadDriveThread::DiscardRead()
|
sl@0
|
570 |
{
|
sl@0
|
571 |
__PRINT(_L("Waiting on Read CS in DiscardRead..."));
|
sl@0
|
572 |
iThreadContext->iCritSect.Wait();
|
sl@0
|
573 |
// +++ READ CS STARTS HERE +++
|
sl@0
|
574 |
__PRINT(_L("Discarding pre-read buffer"));
|
sl@0
|
575 |
iCompleted = EFalse;
|
sl@0
|
576 |
iThreadContext->iBuffer.iDescReadPtr->iLength = 0;
|
sl@0
|
577 |
|
sl@0
|
578 |
// +++ READ CS ENDS HERE +++
|
sl@0
|
579 |
__PRINT(_L("Signalling Read CS in DiscardRead..."));
|
sl@0
|
580 |
iThreadContext->iCritSect.Signal();
|
sl@0
|
581 |
}
|
sl@0
|
582 |
#endif // MSDC_MULTITHREADED
|
sl@0
|
583 |
|