NTSTATUS EncryptedIoQueueStart (EncryptedIoQueue *queue) { NTSTATUS status; EncryptedIoQueueBuffer *buffer; int i; queue->StartPending = TRUE; queue->ThreadExitRequested = FALSE; queue->OutstandingIoCount = 0; queue->IoThreadPendingRequestCount = 0; queue->FirstPoolBuffer = NULL; KeInitializeMutex (&queue->BufferPoolMutex, 0); KeInitializeEvent (&queue->NoOutstandingIoEvent, SynchronizationEvent, FALSE); KeInitializeEvent (&queue->PoolBufferFreeEvent, SynchronizationEvent, FALSE); KeInitializeEvent (&queue->QueueResumedEvent, SynchronizationEvent, FALSE); queue->FragmentBufferA = TCalloc (TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE); if (!queue->FragmentBufferA) goto noMemory; queue->FragmentBufferB = TCalloc (TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE); if (!queue->FragmentBufferB) goto noMemory; KeInitializeEvent (&queue->FragmentBufferAFreeEvent, SynchronizationEvent, TRUE); KeInitializeEvent (&queue->FragmentBufferBFreeEvent, SynchronizationEvent, TRUE); queue->ReadAheadBufferValid = FALSE; queue->ReadAheadBuffer = TCalloc (TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE); if (!queue->ReadAheadBuffer) goto noMemory; // Preallocate buffers for (i = 0; i < TC_ENC_IO_QUEUE_PREALLOCATED_IO_REQUEST_COUNT; ++i) { if (i < TC_ENC_IO_QUEUE_PREALLOCATED_ITEM_COUNT && !GetPoolBuffer (queue, sizeof (EncryptedIoQueueItem))) goto noMemory; if (!GetPoolBuffer (queue, sizeof (EncryptedIoRequest))) goto noMemory; } for (buffer = queue->FirstPoolBuffer; buffer != NULL; buffer = buffer->NextBuffer) { buffer->InUse = FALSE; } // Main thread InitializeListHead (&queue->MainThreadQueue); KeInitializeSpinLock (&queue->MainThreadQueueLock); KeInitializeEvent (&queue->MainThreadQueueNotEmptyEvent, SynchronizationEvent, FALSE); status = TCStartThread (MainThreadProc, queue, &queue->MainThread); if (!NT_SUCCESS (status)) goto err; // IO thread InitializeListHead (&queue->IoThreadQueue); KeInitializeSpinLock (&queue->IoThreadQueueLock); KeInitializeEvent (&queue->IoThreadQueueNotEmptyEvent, SynchronizationEvent, FALSE); status = TCStartThread (IoThreadProc, queue, &queue->IoThread); if (!NT_SUCCESS (status)) { queue->ThreadExitRequested = TRUE; TCStopThread (queue->MainThread, &queue->MainThreadQueueNotEmptyEvent); goto err; } // Completion thread InitializeListHead (&queue->CompletionThreadQueue); KeInitializeSpinLock (&queue->CompletionThreadQueueLock); KeInitializeEvent (&queue->CompletionThreadQueueNotEmptyEvent, SynchronizationEvent, FALSE); status = TCStartThread (CompletionThreadProc, queue, &queue->CompletionThread); if (!NT_SUCCESS (status)) { queue->ThreadExitRequested = TRUE; TCStopThread (queue->MainThread, &queue->MainThreadQueueNotEmptyEvent); TCStopThread (queue->IoThread, &queue->IoThreadQueueNotEmptyEvent); goto err; } #ifdef TC_TRACE_IO_QUEUE GetElapsedTimeInit (&queue->LastPerformanceCounter); #endif queue->StopPending = FALSE; queue->StartPending = FALSE; Dump ("Queue started\n"); return STATUS_SUCCESS; noMemory: status = STATUS_INSUFFICIENT_RESOURCES; err: if (queue->FragmentBufferA) TCfree (queue->FragmentBufferA); if (queue->FragmentBufferB) TCfree (queue->FragmentBufferB); if (queue->ReadAheadBuffer) TCfree (queue->ReadAheadBuffer); FreePoolBuffers (queue); queue->StartPending = FALSE; return status; }
BOOL EncryptionThreadPoolStart () { size_t cpuCount, i; if (ThreadPoolRunning) return TRUE; #ifdef DEVICE_DRIVER cpuCount = GetCpuCount(); #else { SYSTEM_INFO sysInfo; GetSystemInfo (&sysInfo); cpuCount = sysInfo.dwNumberOfProcessors; } #endif if (cpuCount < 2) return TRUE; if (cpuCount > TC_ENC_THREAD_POOL_MAX_THREAD_COUNT) cpuCount = TC_ENC_THREAD_POOL_MAX_THREAD_COUNT; StopPending = FALSE; DequeuePosition = 0; EnqueuePosition = 0; #ifdef DEVICE_DRIVER KeInitializeEvent (&WorkItemReadyEvent, SynchronizationEvent, FALSE); KeInitializeEvent (&WorkItemCompletedEvent, SynchronizationEvent, FALSE); #else WorkItemReadyEvent = CreateEvent (NULL, FALSE, FALSE, NULL); if (!WorkItemReadyEvent) return FALSE; WorkItemCompletedEvent = CreateEvent (NULL, FALSE, FALSE, NULL); if (!WorkItemCompletedEvent) { CloseHandle (WorkItemReadyEvent); return FALSE; } #endif TC_INIT_MUTEX (&DequeueMutex); TC_INIT_MUTEX (&EnqueueMutex); memset (WorkItemQueue, 0, sizeof (WorkItemQueue)); for (i = 0; i < sizeof (WorkItemQueue) / sizeof (WorkItemQueue[0]); ++i) { WorkItemQueue[i].State = WorkItemFree; #ifdef DEVICE_DRIVER KeInitializeEvent (&WorkItemQueue[i].ItemCompletedEvent, SynchronizationEvent, FALSE); #else WorkItemQueue[i].ItemCompletedEvent = CreateEvent (NULL, FALSE, FALSE, NULL); if (!WorkItemQueue[i].ItemCompletedEvent) { EncryptionThreadPoolStop(); return FALSE; } #endif } for (ThreadCount = 0; ThreadCount < cpuCount; ++ThreadCount) { #ifdef DEVICE_DRIVER if (!NT_SUCCESS (TCStartThread (EncryptionThreadProc, NULL, &ThreadHandles[ThreadCount]))) #else if (!(ThreadHandles[ThreadCount] = (HANDLE) _beginthreadex (NULL, 0, EncryptionThreadProc, NULL, 0, NULL))) #endif { EncryptionThreadPoolStop(); return FALSE; } } ThreadPoolRunning = TRUE; return TRUE; }