From c9986738a34f1fc1607734a34d41ad9930e9c86f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Colin=20G=C3=BCnther?= Date: Thu, 3 Dec 2009 17:47:20 +0000 Subject: [PATCH] Refactoring condition variable subsystem of FreeBSD compat layer. Basically it separates the usage of published and unpublished ConditionalVariables into different functions. This allows to stick to the semantics of FreeBSD's condition variable subsystem where it isn't needed to call cv_destroy. With the refactoring now there aren't orphaned published ConditionalVariable left over, when shutting down the compat layer. Though, allocated unpublished struct cv's aren't cleaned up yet. This will be addressed in a next commit. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34468 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/libs/compat/freebsd_network/Condvar.cpp | 48 ++++++++++++------- .../freebsd_network/{condvar.h => Condvar.h} | 6 ++- .../freebsd_network/compat/sys/condvar.h | 2 - src/libs/compat/freebsd_network/condvar.c | 10 +--- src/libs/compat/freebsd_network/synch.c | 6 +-- 5 files changed, 41 insertions(+), 31 deletions(-) rename src/libs/compat/freebsd_network/{condvar.h => Condvar.h} (70%) diff --git a/src/libs/compat/freebsd_network/Condvar.cpp b/src/libs/compat/freebsd_network/Condvar.cpp index 5c9a2b2ecc..51112fd4bc 100644 --- a/src/libs/compat/freebsd_network/Condvar.cpp +++ b/src/libs/compat/freebsd_network/Condvar.cpp @@ -13,19 +13,25 @@ extern "C" { #include -#include "condvar.h" +#include "Condvar.h" #include "device.h" #define ticks_to_usecs(t) (1000000*((bigtime_t)t) / hz) +void +conditionInit(struct cv* variable, const char* description) +{ + variable->condition = new(std::nothrow) ConditionVariable(); + variable->condition->Init(variable, description); +} + + void conditionPublish(struct cv* variable, const void* waitChannel, const char* description) { - variable->waitChannel = waitChannel; - variable->description = description; variable->condition = new(std::nothrow) ConditionVariable(); variable->condition->Publish(waitChannel, description); } @@ -42,10 +48,8 @@ conditionUnpublish(const struct cv* variable) int conditionTimedWait(const struct cv* variable, const int timeout) { - ConditionVariableEntry variableEntry; - - status_t status = variableEntry.Wait(variable->waitChannel, - B_RELATIVE_TIMEOUT, ticks_to_usecs(timeout)); + status_t status = variable->condition->Wait(B_RELATIVE_TIMEOUT, + ticks_to_usecs(timeout)); if (status != B_OK) status = EWOULDBLOCK; @@ -55,22 +59,34 @@ conditionTimedWait(const struct cv* variable, const int timeout) void conditionWait(const struct cv* variable) +{ + variable->condition->Wait(); +} + + +void +conditionNotifyOne(const struct cv* variable) +{ + variable->condition->NotifyOne(); +} + + +int +publishedConditionTimedWait(const void* waitChannel, const int timeout) { ConditionVariableEntry variableEntry; - variableEntry.Wait(variable->waitChannel); + status_t status = variableEntry.Wait(waitChannel, B_RELATIVE_TIMEOUT, + ticks_to_usecs(timeout)); + + if (status != B_OK) + status = EWOULDBLOCK; + return status; } void -conditionNotifyOne(const void* waitChannel) -{ - ConditionVariable::NotifyOne(waitChannel); -} - - -void -conditionNotifyAll(const void* waitChannel) +publishedConditionNotifyAll(const void* waitChannel) { ConditionVariable::NotifyAll(waitChannel); } diff --git a/src/libs/compat/freebsd_network/condvar.h b/src/libs/compat/freebsd_network/Condvar.h similarity index 70% rename from src/libs/compat/freebsd_network/condvar.h rename to src/libs/compat/freebsd_network/Condvar.h index 2a0dba0b5c..617f324829 100644 --- a/src/libs/compat/freebsd_network/condvar.h +++ b/src/libs/compat/freebsd_network/Condvar.h @@ -10,12 +10,14 @@ extern "C" { #endif +void conditionInit(struct cv*, const char*); void conditionPublish(struct cv*, const void*, const char*); void conditionUnpublish(const struct cv*); -void conditionNotifyOne(const void*); -void conditionNotifyAll(const void*); int conditionTimedWait(const struct cv*, const int); void conditionWait(const struct cv*); +void conditionNotifyOne(const struct cv*); +int publishedConditionTimedWait(const void*, const int); +void publishedConditionNotifyAll(const void*); #ifdef __cplusplus } diff --git a/src/libs/compat/freebsd_network/compat/sys/condvar.h b/src/libs/compat/freebsd_network/compat/sys/condvar.h index 862a8968d4..676967f639 100644 --- a/src/libs/compat/freebsd_network/compat/sys/condvar.h +++ b/src/libs/compat/freebsd_network/compat/sys/condvar.h @@ -11,8 +11,6 @@ struct cv { struct ConditionVariable* condition; - const char* description; - const void* waitChannel; }; diff --git a/src/libs/compat/freebsd_network/condvar.c b/src/libs/compat/freebsd_network/condvar.c index bf5eea0f98..79f1062d3a 100644 --- a/src/libs/compat/freebsd_network/condvar.c +++ b/src/libs/compat/freebsd_network/condvar.c @@ -7,18 +7,12 @@ #include #include -#include "condvar.h" +#include "Condvar.h" void cv_init(struct cv* variable, const char* description) { - conditionPublish(variable, variable, description); -} - - -void cv_destroy(struct cv* variable) -{ - conditionUnpublish(variable); + conditionInit(variable, description); } diff --git a/src/libs/compat/freebsd_network/synch.c b/src/libs/compat/freebsd_network/synch.c index 1557ff7647..8f872636ba 100644 --- a/src/libs/compat/freebsd_network/synch.c +++ b/src/libs/compat/freebsd_network/synch.c @@ -9,7 +9,7 @@ #include #include -#include "condvar.h" +#include "Condvar.h" int @@ -22,7 +22,7 @@ msleep(void* identifier, struct mtx* mutex, int priority, conditionPublish(&sleep, identifier, description); mtx_unlock(mutex); - status = conditionTimedWait(&sleep, timeout); + status = publishedConditionTimedWait(identifier, timeout); mtx_lock(mutex); conditionUnpublish(&sleep); @@ -34,7 +34,7 @@ msleep(void* identifier, struct mtx* mutex, int priority, void wakeup(void* identifier) { - conditionNotifyAll(identifier); + publishedConditionNotifyAll(identifier); }