From 869de36dcd786709985fd15c6867cdcea593f914 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Mon, 9 Oct 2023 20:21:06 -0400 Subject: [PATCH] freebsd_network: Remove the need for . Use sizeof()+roundup() to build a char[] of the correct size, and add a static_assert to ensure it stays so. The header needs to have its dependency manually declared, and not all consumers of this file properly declared it as such. This then fixes a concurrent build problem. Fixes #17965 as this was the only consumer of this header. --- src/libs/compat/freebsd_network/Jamfile | 4 ---- .../freebsd_network/compat/sys/condvar.h | 17 ++++++++++---- src/libs/compat/freebsd_network/condvar.cpp | 22 ++++++++++++------- src/libs/compat/freebsd_network/synch.cpp | 8 +++++-- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/libs/compat/freebsd_network/Jamfile b/src/libs/compat/freebsd_network/Jamfile index af4b17df51..f3440aaac4 100644 --- a/src/libs/compat/freebsd_network/Jamfile +++ b/src/libs/compat/freebsd_network/Jamfile @@ -6,10 +6,6 @@ UsePrivateHeaders net ; UsePrivateKernelHeaders ; UseHeaders $(HAIKU_PRIVATE_KERNEL_HEADERS) : true ; -# Enabling C++ structures in C only code -Includes [ FGristFiles kernel_c++_structs.h ] - : kernel_c++_struct_sizes.h ; - SubDirCcFlags [ FDefines _KERNEL=1 ] ; SubDirC++Flags [ FDefines _KERNEL=1 ] ; diff --git a/src/libs/compat/freebsd_network/compat/sys/condvar.h b/src/libs/compat/freebsd_network/compat/sys/condvar.h index 896c1eaa24..128fc10a19 100644 --- a/src/libs/compat/freebsd_network/compat/sys/condvar.h +++ b/src/libs/compat/freebsd_network/compat/sys/condvar.h @@ -1,20 +1,28 @@ /* - * Copyright 2009, Colin Günther, coling@gmx.de. - * All rights reserved. Distributed under the terms of the MIT License. + * Copyright 2023, Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. */ #ifndef _FBSD_COMPAT_SYS_CONDVAR_H_ #define _FBSD_COMPAT_SYS_CONDVAR_H_ -#include +#include +#include + __BEGIN_DECLS struct cv { - struct ConditionVariable condition; + // We cannot include here as it is C++-only. + char condition[roundup((sizeof(void*) * 5) + sizeof(spinlock) + sizeof(int32), sizeof(void*))]; }; +#ifdef __cplusplus +# define __cv_ConditionVariable(CV) reinterpret_cast(&(CV)->condition) +#endif + + void cv_init(struct cv*, const char*); void cv_destroy(struct cv*); void cv_wait(struct cv*, struct mtx*); @@ -24,4 +32,5 @@ void cv_signal(struct cv*); __END_DECLS + #endif /* _FBSD_COMPAT_SYS_CONDVAR_H_ */ diff --git a/src/libs/compat/freebsd_network/condvar.cpp b/src/libs/compat/freebsd_network/condvar.cpp index 9a94f5f23b..082073d3de 100644 --- a/src/libs/compat/freebsd_network/condvar.cpp +++ b/src/libs/compat/freebsd_network/condvar.cpp @@ -1,27 +1,32 @@ /* - * Copyright 2022, Haiku, Inc. All rights reserved. + * Copyright 2022-2023, Haiku, Inc. All rights reserved. * Distributed under the terms of the MIT license. */ extern "C" { #include #include +#include } -#include +#include + + +static_assert(sizeof(cv::condition) == sizeof(ConditionVariable)); void cv_init(struct cv* variable, const char* description) { - variable->condition.Init(NULL, description); + __cv_ConditionVariable(variable)->Init(NULL, description); } void cv_destroy(struct cv* variable) { - variable->condition.NotifyAll(); + __cv_ConditionVariable(variable)->NotifyAll(); + // Nothing else to do. } @@ -29,27 +34,28 @@ cv_destroy(struct cv* variable) void cv_signal(struct cv* variable) { - variable->condition.NotifyOne(); + __cv_ConditionVariable(variable)->NotifyOne(); } int cv_timedwait(struct cv* variable, struct mtx* mutex, int timeout) { - int status; + ConditionVariable* condition = __cv_ConditionVariable(variable); const uint32 flags = timeout ? B_RELATIVE_TIMEOUT : 0; const bigtime_t bigtimeout = TICKS_2_USEC(timeout); + int status; if (mutex->type == MTX_RECURSE) { // Special case: let the ConditionVariable handle switching recursive locks. - status = variable->condition.Wait(&mutex->u.recursive, + status = condition->Wait(&mutex->u.recursive, flags, bigtimeout); return status; } ConditionVariableEntry entry; - variable->condition.Add(&entry); + condition->Add(&entry); mtx_unlock(mutex); diff --git a/src/libs/compat/freebsd_network/synch.cpp b/src/libs/compat/freebsd_network/synch.cpp index 94de255b63..ee585dba71 100644 --- a/src/libs/compat/freebsd_network/synch.cpp +++ b/src/libs/compat/freebsd_network/synch.cpp @@ -3,10 +3,14 @@ * Distributed under the terms of the MIT license. */ +extern "C" { #include #include #include #include +} + +#include int @@ -14,11 +18,11 @@ msleep(void* identifier, struct mtx* mutex, int priority, const char* description, int timeout) { struct cv channel; - channel.condition.Publish(identifier, description); + __cv_ConditionVariable(&channel)->Publish(identifier, description); int status = cv_timedwait(&channel, mutex, timeout); - channel.condition.Unpublish(); + __cv_ConditionVariable(&channel)->Unpublish(); return status; }