+NTSTATUS
+NTAPI
+IopAcquireFileObjectLock(
+ _In_ PFILE_OBJECT FileObject,
+ _In_ KPROCESSOR_MODE WaitMode,
+ _In_ BOOLEAN Alertable,
+ _Out_ PBOOLEAN LockFailed)
+{
+ NTSTATUS Status;
+
+ PAGED_CODE();
+
+ InterlockedIncrement((PLONG)&FileObject->Waiters);
+
+ Status = STATUS_SUCCESS;
+ do
+ {
+ if (!InterlockedExchange((PLONG)&FileObject->Busy, TRUE))
+ {
+ break;
+ }
+ Status = KeWaitForSingleObject(&FileObject->Lock,
+ Executive,
+ WaitMode,
+ Alertable,
+ NULL);
+ } while (Status == STATUS_SUCCESS);
+
+ InterlockedDecrement((PLONG)&FileObject->Waiters);
+ if (Status == STATUS_SUCCESS)
+ {
+ ObReferenceObject(FileObject);
+ *LockFailed = FALSE;
+ }
+ else
+ {
+ if (!FileObject->Busy && FileObject->Waiters)
+ {
+ KeSetEvent(&FileObject->Lock, IO_NO_INCREMENT, FALSE);
+ }
+ *LockFailed = TRUE;
+ }
+
+ return Status;
+}
+