Skip to content

Commit

Permalink
replace calc_threads spin lock with ERESOURCE
Browse files Browse the repository at this point in the history
  • Loading branch information
maharmstone committed Dec 29, 2016
1 parent 705b4b3 commit cce99f2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/btrfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,7 @@ void STDCALL uninit(device_extension* Vcb, BOOL flush) {
ZwClose(Vcb->calcthreads.threads[i].handle);
}

ExDeleteResourceLite(&Vcb->calcthreads.lock);
ExFreePool(Vcb->calcthreads.threads);

time.QuadPart = 0;
Expand Down Expand Up @@ -3633,7 +3634,7 @@ static NTSTATUS create_calc_threads(PDEVICE_OBJECT DeviceObject) {
}

InitializeListHead(&Vcb->calcthreads.job_list);
KeInitializeSpinLock(&Vcb->calcthreads.spin_lock);
ExInitializeResourceLite(&Vcb->calcthreads.lock);
KeInitializeEvent(&Vcb->calcthreads.event, NotificationEvent, FALSE);

RtlZeroMemory(Vcb->calcthreads.threads, sizeof(drv_calc_thread) * Vcb->calcthreads.num_threads);
Expand Down
2 changes: 1 addition & 1 deletion src/btrfs_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ typedef struct {
typedef struct {
ULONG num_threads;
LIST_ENTRY job_list;
KSPIN_LOCK spin_lock;
ERESOURCE lock;
drv_calc_thread* threads;
KEVENT event;
} drv_calc_threads;
Expand Down
20 changes: 8 additions & 12 deletions src/calcthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

NTSTATUS add_calc_job(device_extension* Vcb, UINT8* data, UINT32 sectors, UINT32* csum, calc_job** pcj) {
calc_job* cj;
KIRQL irql;

cj = ExAllocatePoolWithTag(NonPagedPool, sizeof(calc_job), ALLOC_TAG);
if (!cj) {
Expand All @@ -36,10 +35,10 @@ NTSTATUS add_calc_job(device_extension* Vcb, UINT8* data, UINT32 sectors, UINT32
cj->done = 0;
cj->refcount = 1;
KeInitializeEvent(&cj->event, NotificationEvent, FALSE);
KeAcquireSpinLock(&Vcb->calcthreads.spin_lock, &irql);

ExAcquireResourceExclusiveLite(&Vcb->calcthreads.lock, TRUE);
InsertTailList(&Vcb->calcthreads.job_list, &cj->list_entry);
KeReleaseSpinLock(&Vcb->calcthreads.spin_lock, irql);
ExReleaseResourceLite(&Vcb->calcthreads.lock);

KeSetEvent(&Vcb->calcthreads.event, 0, FALSE);
KeClearEvent(&Vcb->calcthreads.event);
Expand Down Expand Up @@ -80,11 +79,9 @@ static BOOL do_calc(device_extension* Vcb, calc_job* cj) {
done = InterlockedIncrement(&cj->done);

if (done * SECTOR_BLOCK >= cj->sectors) {
KIRQL irql;

KeAcquireSpinLock(&Vcb->calcthreads.spin_lock, &irql);
ExAcquireResourceExclusiveLite(&Vcb->calcthreads.lock, TRUE);
RemoveEntryList(&cj->list_entry);
KeReleaseSpinLock(&Vcb->calcthreads.spin_lock, irql);
ExReleaseResourceLite(&Vcb->calcthreads.lock);

KeSetEvent(&cj->event, 0, FALSE);
}
Expand All @@ -104,21 +101,20 @@ void calc_thread(void* context) {
FsRtlEnterFileSystem();

while (TRUE) {
KIRQL irql;
calc_job* cj;
BOOL b;

KeAcquireSpinLock(&Vcb->calcthreads.spin_lock, &irql);
ExAcquireResourceExclusiveLite(&Vcb->calcthreads.lock, TRUE);

if (IsListEmpty(&Vcb->calcthreads.job_list)) {
KeReleaseSpinLock(&Vcb->calcthreads.spin_lock, irql);
ExReleaseResourceLite(&Vcb->calcthreads.lock);
break;
}

cj = CONTAINING_RECORD(Vcb->calcthreads.job_list.Flink, calc_job, list_entry);
cj->refcount++;

KeReleaseSpinLock(&Vcb->calcthreads.spin_lock, irql);
ExReleaseResourceLite(&Vcb->calcthreads.lock);

b = do_calc(Vcb, cj);

Expand Down

0 comments on commit cce99f2

Please sign in to comment.