ConditionVariable: add classical wait interface with lockable

Change-Id: Id18264e786dba818138caf3908c7a89b18e2a1dd
Reviewed-on: https://review.haiku-os.org/c/haiku/+/4921
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
This commit is contained in:
X512 2022-01-30 15:49:44 +09:00 committed by waddlesplash
parent 32fd04c959
commit d03e5994ee
2 changed files with 48 additions and 0 deletions

View File

@ -17,6 +17,8 @@
#include <util/OpenHashTable.h>
struct mutex;
struct recursive_lock;
struct ConditionVariable;
@ -72,6 +74,9 @@ public:
status_t Wait(uint32 flags = 0, bigtime_t timeout = 0);
// all-in one, i.e. doesn't need a
// ConditionVariableEntry
status_t Wait(mutex* lock, uint32 flags = 0, bigtime_t timeout = 0);
status_t Wait(recursive_lock* lock, uint32 flags = 0, bigtime_t timeout = 0);
status_t Wait(spinlock* lock, uint32 flags = 0, bigtime_t timeout = 0);
const void* Object() const { return fObject; }
const char* ObjectType() const { return fObjectType; }

View File

@ -333,6 +333,49 @@ ConditionVariable::Wait(uint32 flags, bigtime_t timeout)
}
status_t
ConditionVariable::Wait(mutex* lock, uint32 flags, bigtime_t timeout)
{
ConditionVariableEntry entry;
Add(&entry);
mutex_unlock(lock);
status_t res = entry.Wait(flags, timeout);
mutex_lock(lock);
return res;
}
status_t
ConditionVariable::Wait(recursive_lock* lock, uint32 flags, bigtime_t timeout)
{
ConditionVariableEntry entry;
Add(&entry);
int32 recursion = recursive_lock_get_recursion(lock);
for (int32 i = 0; i < recursion; i++)
recursive_lock_unlock(lock);
status_t res = entry.Wait(flags, timeout);
for (int32 i = 0; i < recursion; i++)
recursive_lock_lock(lock);
return res;
}
status_t
ConditionVariable::Wait(spinlock* lock, uint32 flags, bigtime_t timeout)
{
ConditionVariableEntry entry;
Add(&entry);
release_spinlock(lock);
status_t res = entry.Wait(flags, timeout);
acquire_spinlock(lock);
return res;
}
/*static*/ void
ConditionVariable::NotifyOne(const void* object, status_t result)
{