From 61a0f9743e102ae97649e24844eead6f1a0adf8c Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Mon, 6 Dec 2021 14:44:00 +0000 Subject: [PATCH] Added a base synchronized class. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@15210 27425a3e-05d8-49a3-a47f-9c15f0e5edd8 --- .../{oop_object.h => oop_base_object.h} | 8 +- .../utils/oop/include/oop_referenced_object.h | 33 +++- .../oop/include/oop_synchronized_object.h | 161 ++++++++++++++++++ 3 files changed, 194 insertions(+), 8 deletions(-) rename os/common/utils/oop/include/{oop_object.h => oop_base_object.h} (95%) create mode 100644 os/common/utils/oop/include/oop_synchronized_object.h diff --git a/os/common/utils/oop/include/oop_object.h b/os/common/utils/oop/include/oop_base_object.h similarity index 95% rename from os/common/utils/oop/include/oop_object.h rename to os/common/utils/oop/include/oop_base_object.h index 89a6b41e8..cb45c61ff 100644 --- a/os/common/utils/oop/include/oop_object.h +++ b/os/common/utils/oop/include/oop_base_object.h @@ -15,7 +15,7 @@ */ /** - * @file oop_object.h + * @file oop_base_object.h * @brief Base object. * @details This header defines a base class that is the root class of * the ChibiOS Object Model. @@ -60,8 +60,8 @@ * @{ */ -#ifndef OOP_OBJECT_H -#define OOP_OBJECT_H +#ifndef OOP_BASE_OBJECT_H +#define OOP_BASE_OBJECT_H #include "ccportab.h" #include "osal.h" @@ -166,6 +166,6 @@ static inline void __base_object_dispose_impl(void *ip) { (c)(((size_t)(ip)) - (ip)->vmt->instance_offset) /** @} */ -#endif /* OOP_OBJECT_H */ +#endif /* OOP_BASE_OBJECT_H */ /** @} */ diff --git a/os/common/utils/oop/include/oop_referenced_object.h b/os/common/utils/oop/include/oop_referenced_object.h index 9e0e95134..a2af8f33e 100644 --- a/os/common/utils/oop/include/oop_referenced_object.h +++ b/os/common/utils/oop/include/oop_referenced_object.h @@ -29,7 +29,7 @@ #ifndef OOP_REFERENCED_OBJECT_H #define OOP_REFERENCED_OBJECT_H -#include "oop_object.h" +#include "oop_base_object.h" /** * @brief Type of a referenced object class. @@ -105,9 +105,9 @@ static inline void __referenced_object_dispose_impl(void *ip) { } /** - * @brief Reference creation implementation. + * @brief New reference creation implementation. * - * @param[in] ip An existing reference to the object. + * @param[in] ip A reference to the object. * @return A new reference to the object. */ CC_FORCE_INLINE @@ -121,6 +121,9 @@ static inline void *__referenced_object_addref_impl(void *ip) { /** * @brief References get implementation. + * + * @param[in] ip A reference to the object. + * @return Remaining references. */ CC_FORCE_INLINE static inline unsigned __referenced_object_getref_impl(void *ip) { @@ -133,7 +136,6 @@ static inline unsigned __referenced_object_getref_impl(void *ip) { * @brief Reference release implementation. * * @param[in] ip A reference to the object. - * @return Remaining references. */ CC_FORCE_INLINE static inline void __referenced_object_release_impl(void *ip) { @@ -147,6 +149,29 @@ static inline void __referenced_object_release_impl(void *ip) { } /** @} */ +/** + * @brief New reference creation. + * + * @param[in] rop A reference to the object. + * @return A new reference to the object. + */ +CC_FORCE_INLINE +static inline referenced_object_c *roAddRef(referenced_object_c *rop) { + + return (referenced_object_c *)__referenced_object_addref_impl(rop); +} + +/** + * @brief Reference release. + * + * @param[in] rop A reference to the object. + */ +CC_FORCE_INLINE +static inline void roRelease(referenced_object_c *rop) { + + return __referenced_object_release_impl(rop); +} + #endif /* OOP_REFERENCED_OBJECT_H */ /** @} */ diff --git a/os/common/utils/oop/include/oop_synchronized_object.h b/os/common/utils/oop/include/oop_synchronized_object.h new file mode 100644 index 000000000..3cd49a8b8 --- /dev/null +++ b/os/common/utils/oop/include/oop_synchronized_object.h @@ -0,0 +1,161 @@ +/* + ChibiOS - Copyright (C) 2006..2021 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file oop_synchronized_object.h + * @brief Base class for objects supporting synchronization. + * @details This header defines a base class for classes requiring a + * synchronization mechanism, + * + * @addtogroup OOP_SYNCHRONIZED_OBJECT + * @details Base class for objects that require a synchronization mechanism. + * This class extends @p synchronized_object_c class. + * @{ + */ + +#ifndef OOP_SYNCHRONIZED_OBJECT_H +#define OOP_SYNCHRONIZED_OBJECT_H + +#include "osal.h" +#include "oop_synchronized_object.h" + +/** + * @brief Type of a synchronized object class. + */ +typedef struct synchronized_object synchronized_object_c; + +/** + * @brief @p synchronized_object_c specific methods. + * @note This object defines no methods. + */ +#define __synchronized_object_methods \ + __referenced_object_methods + +/** + * @brief @p synchronized_object_c specific data. + */ +#define __synchronized_object_data \ + __referenced_object_data \ + mutex_t mutex; + + +/** + * @brief @p synchronized_object_c virtual methods table. + */ +struct __synchronized_object_vmt { \ + __synchronized_object_methods \ +}; + +/** + * @brief Structure representing a synchronized object class. + */ +typedef struct synchronized_object { + /** + * @brief Virtual Methods Table. + */ + const struct __synchronized_object_vmt *vmt; + __synchronized_object_data +} synchronized_object_c; + +/** + * @name Methods implementations + * @{ + */ +/** + * @brief Object creation implementation. + * + * @param[in] ip Pointer to a @p synchronized_object_c structure to be + * initialized. + * @return A new reference to the object. + */ +CC_FORCE_INLINE +static inline void *__synchronized_object_objinit_impl(void *ip, const void *vmt) { + synchronized_object_c *objp = (synchronized_object_c *)ip; + + __referenced_object_objinit_impl(ip, vmt); + osalMutexObjectInit(&objp->mutex); + + return ip; +} + +/** + * @brief Object finalization implementation. + * + * @param[in] ip Pointer to a @p synchronized_object_c structure to be + * disposed. + */ +CC_FORCE_INLINE +static inline void __synchronized_object_dispose_impl(void *ip) { + + __referenced_object_dispose_impl(ip); + /* Nothing.*/ + /* TODO add RT objects disposing when available.*/ +} + +/** + * @brief Object lock implementation. + * + * @param[in] ip Pointer to a @p synchronized_object_c structure to be + * locked. + */ +CC_FORCE_INLINE +static inline void __synchronized_object_lock_impl(void *ip) { + synchronized_object_c *objp = (synchronized_object_c *)ip; + + osalMutexLock(&objp->mutex); +} + +/** + * @brief Object unlock implementation. + * + * @param[in] ip Pointer to a @p synchronized_object_c structure to be + * unlocked. + */ +CC_FORCE_INLINE +static inline void __synchronized_object_unlock_impl(void *ip) { + synchronized_object_c *objp = (synchronized_object_c *)ip; + + osalMutexUnlock(&objp->mutex); +} +/** @} */ + +/** + * @brief Object lock. + * + * @param[in] ip Pointer to a @p synchronized_object_c structure to be + * locked. + */ +CC_FORCE_INLINE +static inline void soLock(synchronized_object_c *sop) { + + __synchronized_object_lock_impl(sop); +} + +/** + * @brief Object unlock. + * + * @param[in] ip Pointer to a @p synchronized_object_c structure to be + * unlocked. + */ +CC_FORCE_INLINE +static inline void soUnlock(synchronized_object_c *sop) { + + __synchronized_object_unlock_impl(sop); +} + +#endif /* OOP_SYNCHRONIZED_OBJECT_H */ + +/** @} */