From 62b8be7e2276d22faeb9972d9c7bf4259e58bf01 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Mon, 6 Apr 2026 19:11:59 -0400 Subject: [PATCH] kernel/vm: DaemonCondition is really just a basic binary semaphore. So split it off into its own file. This will make it easier to split the page writer off into a separate file. --- headers/private/kernel/util/BinarySemaphore.h | 71 +++++++++++++++++++ src/system/kernel/vm/vm_page.cpp | 62 +--------------- 2 files changed, 74 insertions(+), 59 deletions(-) create mode 100644 headers/private/kernel/util/BinarySemaphore.h diff --git a/headers/private/kernel/util/BinarySemaphore.h b/headers/private/kernel/util/BinarySemaphore.h new file mode 100644 index 0000000000..fb91b2fd50 --- /dev/null +++ b/headers/private/kernel/util/BinarySemaphore.h @@ -0,0 +1,71 @@ +/* + * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de. + * Copyright 2026, Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef KERNEL_UTIL_BINARY_SEMAPHORE_H +#define KERNEL_UTIL_BINARY_SEMAPHORE_H + + +#include +#include + + +struct BinarySemaphore { + void Init(const char* name) + { + mutex_init(&fLock, "binary semaphore"); + fCondition.Init(this, name); + fActivated = false; + } + + bool Lock() + { + return mutex_lock(&fLock) == B_OK; + } + + void Unlock() + { + mutex_unlock(&fLock); + } + + bool Wait(bigtime_t timeout, bool clearActivated) + { + MutexLocker locker(fLock); + if (clearActivated) + fActivated = false; + else if (fActivated) + return true; + + ConditionVariableEntry entry; + fCondition.Add(&entry); + + locker.Unlock(); + + return entry.Wait(B_RELATIVE_TIMEOUT, timeout) == B_OK; + } + + void WakeUp() + { + if (fActivated) + return; + + MutexLocker locker(fLock); + fActivated = true; + fCondition.NotifyOne(); + } + + void ClearActivated() + { + MutexLocker locker(fLock); + fActivated = false; + } + +private: + mutex fLock; + ConditionVariable fCondition; + bool fActivated; +}; + + +#endif // KERNEL_UTIL_BINARY_SEMAPHORE_H diff --git a/src/system/kernel/vm/vm_page.cpp b/src/system/kernel/vm/vm_page.cpp index 91c9b744a4..799cd89d98 100644 --- a/src/system/kernel/vm/vm_page.cpp +++ b/src/system/kernel/vm/vm_page.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -196,65 +197,8 @@ typedef DoublyLinkedList PageReservationWaiterList; static PageReservationWaiterList sPageReservationWaiters; -struct DaemonCondition { - void Init(const char* name) - { - mutex_init(&fLock, "daemon condition"); - fCondition.Init(this, name); - fActivated = false; - } - - bool Lock() - { - return mutex_lock(&fLock) == B_OK; - } - - void Unlock() - { - mutex_unlock(&fLock); - } - - bool Wait(bigtime_t timeout, bool clearActivated) - { - MutexLocker locker(fLock); - if (clearActivated) - fActivated = false; - else if (fActivated) - return true; - - ConditionVariableEntry entry; - fCondition.Add(&entry); - - locker.Unlock(); - - return entry.Wait(B_RELATIVE_TIMEOUT, timeout) == B_OK; - } - - void WakeUp() - { - if (fActivated) - return; - - MutexLocker locker(fLock); - fActivated = true; - fCondition.NotifyOne(); - } - - void ClearActivated() - { - MutexLocker locker(fLock); - fActivated = false; - } - -private: - mutex fLock; - ConditionVariable fCondition; - bool fActivated; -}; - - -static DaemonCondition sPageWriterCondition; -static DaemonCondition sPageDaemonCondition; +static BinarySemaphore sPageWriterCondition; +static BinarySemaphore sPageDaemonCondition; #if PAGE_ALLOCATION_TRACING