First public contribution.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
17 #include "usbmsshared.h"
18 #include "rwdrivethread.h"
19 #include "massstoragedebug.h"
23 #ifdef PRINT_MSDC_MULTITHREADED_READ_INFO
24 #define __MT_READ_PRINT(t) {RDebug::Print(t);}
25 #define __MT_READ_PRINT1(t,a) {RDebug::Print(t,a);}
26 #define __MT_READ_PRINT2(t,a,b) {RDebug::Print(t,a,b);}
28 #define __MT_READ_PRINT(t)
29 #define __MT_READ_PRINT1(t,a)
30 #define __MT_READ_PRINT2(t,a,b)
31 #endif // PRINT_MSDC_MULTITHREADED_READ_INFO
34 #ifdef MSDC_MULTITHREADED
36 TBlockDesc::TBlockDesc()
37 :iBuf((TUint8 *)NULL,0,0)
41 void TBlockDesc::SetPtr(TPtr8& aDes)
47 TBlockDescBuffer::TBlockDescBuffer()
49 iDescReadPtr = &iDesc1;
50 iDescWritePtr = &iDesc2;
53 void TBlockDescBuffer::SetUpReadBuf(TPtr8& aDes1, TPtr8& aDes2)
57 iDescReadPtr = &iDesc1;
58 iDescWritePtr = &iDesc2;
62 //-----------------------------------------------
65 Construct a CThreadContext object.
67 @param aName The name to be assigned to this thread.
68 @param aThreadFunction Function to be called when thread is initially scheduled.
69 @param aOwner Pointer to the object owning the thread. Used as the parameter to aThreadFunction.
71 CThreadContext* CThreadContext::NewL(const TDesC& aName,
72 TThreadFunction aThreadFunction,
75 __FNLOG("CThreadContext::NewL");
76 CThreadContext* self = new (ELeave) CThreadContext();
77 CleanupStack::PushL(self);
78 self->ConstructL(aName, aThreadFunction, aOwner);
84 Construct a CThreadContext object
86 @param aName The name to be assigned to this thread.
87 @param aThreadFunction Function to be called when thread is initially scheduled.
88 @param aOwner Pointer to the object owning the thread. Used as the parameter to aThreadFunction.
90 void CThreadContext::ConstructL(const TDesC& aName,
91 TThreadFunction aThreadFunction,
94 __FNLOG("CThreadContext::ConstructL");
95 __PRINT(_L("Creating Critical Section"));
96 User::LeaveIfError(iCritSect.CreateLocal());
97 __PRINT(_L("Creating RThread"));
99 TUint serial(0); // Used to retry creation of a thread in case
100 // one with the same name already exists
103 threadName.CreateMaxL(aName.Length() + 8);
104 CleanupClosePushL(threadName);
110 err = iThread.Create(threadName, aThreadFunction, 0x1000, NULL, aOwner);
111 __PRINT2(_L("CThreadContext::ConstructL Created thread %S err=%d"), &threadName, err);
113 // for a restart wait and retry until old thread is gone
114 if (err == KErrAlreadyExists)
116 User::After(10 * 1000); // 10 mS
118 threadName.AppendNumFixedWidth(serial, EDecimal, 8);
127 User::LeaveIfError(err);
128 CleanupStack::Pop(); // threadName
132 iThread.SetPriority(EPriorityMore);
137 Construct a CThreadContext object
139 CThreadContext::CThreadContext()
143 __FNLOG("CThreadContext::CThreadContext");
149 CThreadContext::~CThreadContext()
151 __FNLOG("CThreadContext::~CThreadContext");
152 __PRINT(_L("Closing Critical Section"));
154 __PRINT(_L("Killing ThreadContext"));
156 __PRINT(_L("Closing ThreadContext"));
160 //-----------------------------------------------
163 Construct a CWriteDriveThread object
165 CWriteDriveThread* CWriteDriveThread::NewL()
167 __FNLOG("CWriteDriveThread::NewL");
168 CWriteDriveThread* self = new (ELeave) CWriteDriveThread();
169 CleanupStack::PushL(self);
176 Construct a CWriteDriveThread object
178 void CWriteDriveThread::ConstructL()
180 __FNLOG("CWriteDriveThread::ConstructL");
181 TBuf<16> name = _L("MassStorageWrite");
182 iThreadContext = CThreadContext::NewL(name, ThreadFunction, this);
183 // There are two free pointers to start with so initialise the semaphore with 1
184 User::LeaveIfError(iProducerSem.CreateLocal(1));
185 User::LeaveIfError(iConsumerSem.CreateLocal(0));
187 iThreadContext->Resume();
191 Construct a CWriteDriveThread object
193 CWriteDriveThread::CWriteDriveThread()
194 : iIsCommandWrite10(EFalse)
196 __FNLOG("CWriteDriveThread::CWriteDriveThread");
202 CWriteDriveThread::~CWriteDriveThread()
204 __FNLOG("CWriteDriveThread::~CWriteDriveThread");
205 delete iThreadContext;
209 This function is called when the thread is initially scheduled.
211 @param aSelf Pointer to self to facilitate call to member method.
213 TInt CWriteDriveThread::ThreadFunction(TAny* aSelf)
215 __FNLOG("CWriteDriveThread::ThreadFunction");
216 CWriteDriveThread* self = static_cast<CWriteDriveThread*>(aSelf);
217 return self->WriteToDrive();
221 Writes the data pointed to by iDescWritePtr to the drive.
223 TInt CWriteDriveThread::WriteToDrive()
225 __FNLOG("\tCWriteDriveThread::WriteToDrive");
227 // One-off convenience variable assignment
228 TBlockDesc* &desc = iThreadContext->iBuffer.iDescWritePtr;
233 __PRINT(_L("\tWaiting on Write CS..."));
234 iThreadContext->iCritSect.Wait();
235 // +++ WRITE CS STARTS HERE +++
236 __PRINT1(_L("\tNow using as write buffer: iBuf%d"), iThreadContext->iBuffer.GetBufferNumber(&desc->iBuf));
237 #ifdef MEASURE_AND_DISPLAY_WRITE_TIME
238 RDebug::Print(_L("\tSCSI: writing %d bytes\n"), desc->iBuf.Length());
242 __PRINT1(_L("\tSCSI: writing %d bytes\n"), desc->iBuf.Length());
244 // Write buffer to disk
247 if (desc->iBuf[0] == '2')
250 RDebug::Printf("Injecting error");
254 RDebug::Printf("%08lx %x [%x] [%x]", desc->iByteOffset, desc->iBuf.Length(),
256 desc->iBuf[desc->iBuf.Length()-1]);
259 iThreadContext->iError = iThreadContext->iDrive->Write(desc->iByteOffset, desc->iBuf,iThreadContext->iDrive->IsWholeMediaAccess());
261 if (desc->iBuf[0] == 'x')
263 iThreadContext->iError = KErrUnknown;
267 #ifdef MEASURE_AND_DISPLAY_WRITE_TIME
269 const TTimeIntervalMicroSeconds time = t1.MicroSecondsFrom(t0);
270 const TUint time_ms = I64LOW(time.Int64() / 1000);
271 RDebug::Print(_L("SCSI: write took %d ms\n"), time_ms);
273 iCallback((TUint8*) (desc->iBuf.Ptr()), iCallbackParameter);
275 ASSERT(iWriteCounter >= 0);
277 __PRINT(_L("\tSignalling Write CS"));
278 iThreadContext->iCritSect.Signal();
279 // +++ WRITE CS ENDS HERE +++
280 iProducerSem.Signal();
285 Initiates writing data pointed to by iReadBuf to the drive and resumes the thread. Writing
286 is completed by the ThreadFunction when the thread is resumed.
288 @param aDrive Drive to write to.
289 @param aOffset Write offset.
291 TInt CWriteDriveThread::WriteDriveData(CMassStorageDrive* aDrive, const TInt64& aOffset, TPtrC8& aDes, ProcessWriteCompleteFunc aFunc, TAny* aPtr)
293 // Check error code from previous write
294 const TInt r = iThreadContext->iError;
297 __PRINT1(_L("Error after previous write = 0x%x \n"), r);
301 // Swap the two buffer pointers
303 __PRINT(_L("Waiting on Write CS..."));
304 // +++ WRITE CS STARTS HERE +++
305 iThreadContext->iCritSect.Wait();
307 // New DB First read into the iDescReadPtr pointer,
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
309 iThreadContext->iBuffer.iDescReadPtr->iBuf.Set((TUint8*)aDes.Ptr(), aDes.Length(), KMaxBufSize );
312 iCallbackParameter = aPtr;
315 iThreadContext->iBuffer.SwapDesc();
316 // Prepare variables for next write
317 iThreadContext->iDrive = aDrive;
318 iThreadContext->iBuffer.iDescWritePtr->iByteOffset = aOffset;
319 // +++ WRITE CS ENDS HERE +++
320 __PRINT(_L("Signalling Write CS..."));
321 iThreadContext->iCritSect.Signal();
323 iConsumerSem.Signal();
328 void CWriteDriveThread::WaitForWriteEmpty()
330 while(iWriteCounter > 0)
336 // Check if the target address range was recently written to, this is to force a
337 // cache miss when reading from the same sectors that were just written.
338 // Optimisation note: this is only needed if the read precache was started
339 // before the write was completed.
340 TBool CWriteDriveThread::IsRecentlyWritten(TInt64 aOffset, TInt aLength)
342 ASSERT(iWriteCounter == 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
345 if(aOffset <= iThreadContext->iBuffer.iDescReadPtr->iByteOffset &&
346 aOffset + aLength >= iThreadContext->iBuffer.iDescReadPtr->iByteOffset)
348 if(aOffset >= iThreadContext->iBuffer.iDescReadPtr->iByteOffset &&
349 aOffset <= iThreadContext->iBuffer.iDescReadPtr->iByteOffset + iThreadContext->iBuffer.iDescReadPtr->iLength)
351 if(aOffset <= iThreadContext->iBuffer.iDescWritePtr->iByteOffset &&
352 aOffset + aLength >= iThreadContext->iBuffer.iDescReadPtr->iByteOffset)
354 if(aOffset >= iThreadContext->iBuffer.iDescWritePtr->iByteOffset &&
355 aOffset <= iThreadContext->iBuffer.iDescReadPtr->iByteOffset + iThreadContext->iBuffer.iDescReadPtr->iLength)
360 //-----------------------------------------------
363 Construct a CReadDriveThread object
365 CReadDriveThread* CReadDriveThread::NewL()
367 __FNLOG("CReadDriveThread::NewL");
368 CReadDriveThread* self = new (ELeave) CReadDriveThread();
369 CleanupStack::PushL(self);
376 Construct a CReadDriveThread object
378 @param aName The name to be assigned to this thread.
379 @pram aThreadFunction Function to be called when thread is initially scheduled.
381 void CReadDriveThread::ConstructL()
383 __FNLOG("CReadDriveThread::ConstructL");
384 TBuf<15> name = _L("MassStorageRead");
385 iThreadContext = CThreadContext::NewL(name, ThreadFunction, this);
389 Construct a CReadDriveThread object
391 CReadDriveThread::CReadDriveThread()
393 iThreadRunning(EFalse)
395 __FNLOG("CReadDriveThread::CReadDriveThread");
401 CReadDriveThread::~CReadDriveThread()
403 __FNLOG("CReadDriveThread::~CReadDriveThread");
404 delete iThreadContext;
408 This function is called when the thread is initially scheduled.
410 @param aSelf Pointer to self to facilitate call to member method.
412 TInt CReadDriveThread::ThreadFunction(TAny* aSelf)
414 __FNLOG("CReadDriveThread::ThreadFunction");
415 CReadDriveThread* self = static_cast<CReadDriveThread*>(aSelf);
416 return self->ReadFromDrive();
420 Reads data from the drive with iOffset and iReadLength into memory pointer iReadBuffer
421 and suspends the thread.
423 TInt CReadDriveThread::ReadFromDrive()
425 __FNLOG("\tCReadDriveThread::ReadFromDrive");
427 // One-off convenience variable assignment
428 TBlockDesc* &desc = iThreadContext->iBuffer.iDescWritePtr;
432 __PRINT(_L("\tWaiting on Read CS..."));
433 iThreadContext->iCritSect.Wait();
434 // +++ READ CS STARTS HERE +++
435 iThreadRunning = ETrue;
438 __PRINT1(_L("\tNow using as read buffer: iBuf%d"), iThreadContext->iBuffer.GetBufferNumber(&desc->iBuf));
440 #ifdef MEASURE_AND_DISPLAY_READ_TIME
441 RDebug::Print(_L("\tSCSI: reading %d bytes\n"), desc->iBuf.Length());
445 __PRINT1(_L("\tSCSI: reading %d bytes\n"), desc->iBuf.Length());
447 // Fill read buffer from disk
448 iThreadContext->iError = iThreadContext->iDrive->Read(desc->iByteOffset,
451 iThreadContext->iDrive->IsWholeMediaAccess());
453 #ifdef MEASURE_AND_DISPLAY_READ_TIME
455 const TTimeIntervalMicroSeconds time = t1.MicroSecondsFrom(t0);
456 const TUint time_ms = I64LOW(time.Int64() / 1000);
457 RDebug::Print(_L("SCSI: read took %d ms\n"), time_ms);
461 iThreadRunning = EFalse;
462 __PRINT(_L("\tSignalling Read CS"));
463 // +++ READ CS ENDS HERE +++
464 iThreadContext->iCritSect.Signal();
466 __PRINT(_L("\tSuspending Read Thread"));
472 Client read request of a data block from the specified drive.
473 If there is no pre-read data that matches the requested Offset and Length then the drive
474 is read and the next pre-read is setup. If there is matching pre-read data available then
475 the next pre-read is setup. Finishes by resuming the thread and the ThreadFunciton runs.
477 @param aDrive Drive to read from.
478 @param aOffset Read offset
479 @param aLength Length
481 TBool CReadDriveThread::ReadDriveData(CMassStorageDrive* aDrive,
482 const TInt64& aOffset,
486 __MT_READ_PRINT2(_L("\nRead10: offs %ld len %d"), aOffset, aLength);
488 __PRINT(_L("Waiting on Read CS..."));
489 iThreadContext->iCritSect.Wait();
490 // +++ READ CS STARTS HERE +++
491 __ASSERT_DEBUG(!iThreadRunning, User::Panic(_L("MSDC-THREAD"), 666));
493 TBlockDesc* &desc = iThreadContext->iBuffer.iDescReadPtr;
494 TBlockDesc* &bgDesc = iThreadContext->iBuffer.iDescWritePtr;
496 if ((!aIgnoreCache) &&
498 (iThreadContext->iError == KErrNone) &&
499 (iThreadContext->iDrive == aDrive) &&
500 (bgDesc->iByteOffset == aOffset) &&
501 (bgDesc->iLength == aLength))
503 // Good: We pre-read the correct data :-)
504 __MT_READ_PRINT(_L("Match: Using pre-read data :-) :-) :-) :-)"));
508 __MT_READ_PRINT(_L("Not using pre-read data"));
509 if (iThreadContext->iError != KErrNone)
511 __MT_READ_PRINT1(_L("Pre-read failed: %d"), iThreadContext->iError);
513 if (iThreadContext->iDrive != aDrive)
515 __MT_READ_PRINT2(_L("Pre-read drive mismatch: pre 0x%08x / act 0x%08x"),
516 iThreadContext->iDrive, aDrive);
518 if (desc->iByteOffset != aOffset)
520 __MT_READ_PRINT2(_L("Pre-read offset mismatch: pre %ld / act %ld"),
521 desc->iByteOffset, aOffset);
523 if (desc->iLength != aLength)
525 __MT_READ_PRINT2(_L("Pre-read length mismatch: pre %d / act %d"),
526 desc->iLength, aLength);
527 // Potential optimization: If the pre-read was OK but for more data
528 // than the host is now asking for, we could still satisfy that
529 // request from the pre-read data by shortening the buffer.
531 // No valid pre-read data was available - so we have to read it now
532 bgDesc->iByteOffset = aOffset;
533 bgDesc->iLength = aLength;
534 TInt err = aDrive->Read(aOffset,
537 aDrive->IsWholeMediaAccess());
540 __PRINT1(_L("Read failed, err=%d\n"), err);
541 // +++ READ CS ENDS HERE +++
542 __PRINT(_L("Signalling Read CS..."));
543 iThreadContext->iCritSect.Signal();
548 // Prepare thread variables for next pre-read attempt by the ReadThread
549 const TInt64 offs_new = aOffset + aLength;
550 iThreadContext->iDrive = aDrive; // same drive
551 desc->iByteOffset = offs_new; // next block
552 desc->iLength = aLength; // same length
554 iThreadContext->iBuffer.SwapDesc();
556 // +++ READ CS ENDS HERE +++
557 __PRINT(_L("Signalling Read CS..."));
558 iThreadContext->iCritSect.Signal();
559 // Start background read
560 __PRINT(_L("Resuming Read Thread"));
561 iThreadContext->Resume();
566 Discard the read buffer. This is used to force a cache miss when reading from
567 the same sectors that were just written.
569 void CReadDriveThread::DiscardRead()
571 __PRINT(_L("Waiting on Read CS in DiscardRead..."));
572 iThreadContext->iCritSect.Wait();
573 // +++ READ CS STARTS HERE +++
574 __PRINT(_L("Discarding pre-read buffer"));
576 iThreadContext->iBuffer.iDescReadPtr->iLength = 0;
578 // +++ READ CS ENDS HERE +++
579 __PRINT(_L("Signalling Read CS in DiscardRead..."));
580 iThreadContext->iCritSect.Signal();
582 #endif // MSDC_MULTITHREADED