--- mstools/samples/pdc/pdc.c 2018/08/09 18:20:38 1.1 +++ mstools/samples/pdc/pdc.c 2018/08/09 18:21:29 1.1.1.2 @@ -109,6 +109,7 @@ Return Value: { SYSTEM_INFO SystemInformation; + PWORK_QUEUE WorkQueue; // // Query the number of processors from the system and @@ -174,7 +175,7 @@ Return Value: EnumerateDirectoryTree( DirectoryPath, QueueSearchFile, - NULL + WorkQueue ); // @@ -272,6 +273,7 @@ Return Value: --*/ { + PWORK_QUEUE WorkQueue = (PWORK_QUEUE)EnumerateParameter; PSEARCH_REQUEST SearchRequest; HANDLE File; HANDLE Mapping; @@ -463,7 +465,7 @@ Return Value: FileData, FileSize, &SearchRequest->OverlappedIO, - ProcessReadFileCompletion + (LPOVERLAPPED_COMPLETION_ROUTINE) ProcessReadFileCompletion ) ) { fprintf( stderr, "%s(0) : error %u: Unable to queue read of file.\n", @@ -544,6 +546,7 @@ Return Value: DWORD BytesWritten; PSEARCH_REQUEST_STATE State; PSEARCH_REQUEST SearchRequest; + CHAR MessageBuffer[ 2 * MAX_PATH ]; if (WorkItem->Reason == WORK_INITIALIZE_ITEM) { // @@ -649,12 +652,6 @@ Return Value: } // - // Free the storage used by the SearchRequest - // - - LocalFree( SearchRequest ); - - // // If any output was written to the virtual buffer, // flush the output to standard output. Trim the // virtual buffer back to zero committed pages. @@ -684,10 +681,24 @@ Return Value: // commit the memory. // - fprintf( stderr, "PDC: out of memory.\n" ); + WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), + MessageBuffer, + sprintf( MessageBuffer, + "%s(0) : error 0: too many matches for file\n", + SearchRequest->FullPathName + ), + &BytesWritten, + NULL + ); } // + // Free the storage used by the SearchRequest + // + + LocalFree( SearchRequest ); + + // // All done with this request. Return to the worker thread that // called us. // @@ -739,7 +750,7 @@ Return Value: // SearchRequest = CONTAINING_RECORD( lpOverlapped, SEARCH_REQUEST, OverlappedIO ); - QueueWorkItem( WorkQueue, &SearchRequest->WorkItem ); + QueueWorkItem( SearchRequest->WorkItem.WorkQueue, &SearchRequest->WorkItem ); } @@ -784,7 +795,6 @@ Return Value: DWORD MatchesFound; DWORD BytesRead; - // // Get a pointer to the beginning of the file data in memory // and calculate the address of the end of file point in // memory. @@ -888,7 +898,15 @@ Return Value: // if (MappedFileIO) { - UnmapViewOfFile( FileData ); + if (!UnmapViewOfFile( FileData )) { + State->CurrentOutput += sprintf( State->CurrentOutput, + "%s(%u) : UnmapViewOfFile( %08x ) failed, error == %u\n", + SearchRequest->FullPathName, + LineNumber, + FileData, + GetLastError() + ); + } } else { VirtualFree( FileData, 0, MEM_RELEASE ); @@ -955,7 +973,7 @@ Return Value: // an item from the queue. // - if (WorkQueue->Semaphore = CreateSemaphore( NULL, 0, 1000, NULL )) { + if (WorkQueue->Semaphore = CreateSemaphore( NULL, 0, 100000, NULL )) { // // Mutual exclusion between the worker threads accessing // the work queue is done with a critical section. @@ -987,8 +1005,8 @@ Return Value: for (i=0; iNumberOfWorkerThreads++; WorkQueue->WorkerThreads[ i ] = Thread; + SetThreadPriority( Thread, THREAD_PRIORITY_ABOVE_NORMAL ); } } @@ -1182,6 +1201,7 @@ Return Value: EnterCriticalSection( &WorkQueue->CriticalSection ); Result = TRUE; try { + WorkItem->WorkQueue = WorkQueue; InsertTailList( &WorkQueue->Queue, &WorkItem->List ); Result = ReleaseSemaphore( WorkQueue->Semaphore, 1, NULL ); } @@ -1199,6 +1219,7 @@ WorkerThread( LPVOID lpThreadParameter ) { + PWORK_QUEUE WorkQueue = (PWORK_QUEUE)lpThreadParameter; DWORD rc; WORK_QUEUE_ITEM InitWorkItem; PWORK_QUEUE_ITEM WorkItem; @@ -1213,39 +1234,41 @@ WorkerThread( InitWorkItem.Reason = WORK_INITIALIZE_ITEM; (WorkQueue->WorkerRoutine)( &InitWorkItem ); while( TRUE ) { + try { - // - // Wait until something is put in the queue (semaphore is - // released), remove the item from the queue, mark it not - // inserted, and execute the specified routine. - // - - rc = WaitForSingleObjectEx( WorkQueue->Semaphore, 0xFFFFFFFF, TRUE ); - if (rc == WAIT_IO_COMPLETION) { - continue; - } + // + // Wait until something is put in the queue (semaphore is + // released), remove the item from the queue, mark it not + // inserted, and execute the specified routine. + // - EnterCriticalSection( &WorkQueue->CriticalSection ); - try { - if (WorkQueue->Terminating && IsListEmpty( &WorkQueue->Queue )) { - break; + rc = WaitForSingleObjectEx( WorkQueue->Semaphore, 0xFFFFFFFF, TRUE ); + if (rc == WAIT_IO_COMPLETION) { + continue; } - WorkItem = (PWORK_QUEUE_ITEM)RemoveHeadList( &WorkQueue->Queue ); - } - finally { - LeaveCriticalSection( &WorkQueue->CriticalSection ); - } + EnterCriticalSection( &WorkQueue->CriticalSection ); + try { + if (WorkQueue->Terminating && IsListEmpty( &WorkQueue->Queue )) { + break; + } - // - // Execute the worker routine for this work item. - // + WorkItem = (PWORK_QUEUE_ITEM)RemoveHeadList( &WorkQueue->Queue ); + } + finally { + LeaveCriticalSection( &WorkQueue->CriticalSection ); + } + + // + // Execute the worker routine for this work item. + // - try { (WorkQueue->WorkerRoutine)( WorkItem ); } - except( EXCEPTION_EXECUTE_HANDLER ) { + // + // Ignore any exceptions from worker routine. + // } } @@ -1620,7 +1643,7 @@ Return Value: // faulting instruction. // - return EXCEPTION_CONTINUE_EXECUTION; + return EXCEPTION_CONTINUE_EXECUTION; } else { //