From 8b367be11b7f6f3868c0a58f3afcbbc73d63b219 Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sat, 28 Apr 2018 08:46:55 +0000 Subject: Added RAII helpers to the C++ wrapper. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11962 110e8d01-0319-4d1e-a829-52ad28d1bb01 --- os/various/cpp_wrappers/ch.hpp | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'os/various') diff --git a/os/various/cpp_wrappers/ch.hpp b/os/various/cpp_wrappers/ch.hpp index d52f80730..7bfaea5d2 100644 --- a/os/various/cpp_wrappers/ch.hpp +++ b/os/various/cpp_wrappers/ch.hpp @@ -380,11 +380,27 @@ namespace chibios_rt { #endif /* CH_CFG_NO_IDLE_THREAD == FALSE */ }; + /*------------------------------------------------------------------------* + * chibios_rt::CriticalSectionLocker * + *------------------------------------------------------------------------*/ + /** + * @brief RAII helper for reentrant critical sections. + */ + class CriticalSectionLocker { + volatile const ::syssts_t syssts = chSysGetStatusAndLockX(); + + public: + ~CriticalSectionLocker() { + + chSysRestoreStatusX(syssts); + } + }; + /*------------------------------------------------------------------------* * chibios_rt::Scheduler * *------------------------------------------------------------------------*/ /** - * @brief Class encapsulating thelow level scheduler functionalities. + * @brief Class encapsulating the low level scheduler functionalities. */ class Scheduler { public: @@ -2057,6 +2073,28 @@ namespace chibios_rt { } }; + /*------------------------------------------------------------------------* + * chibios_rt::MutexLocker * + *------------------------------------------------------------------------*/ + /** + * @brief RAII helper for mutexes. + */ + class MutexLocker + { + chibios_rt::Mutex& mutex; + + public: + MutexLocker(Mutex& m) : mutex(m) { + + mutex.lock(); + } + + ~MutexLocker() { + + mutex.unlock(); + } + }; + #if (CH_CFG_USE_CONDVARS == TRUE) || defined(__DOXYGEN__) /*------------------------------------------------------------------------* * chibios_rt::CondVar * @@ -2926,7 +2964,7 @@ namespace chibios_rt { * * @api */ - inline size_t status(size_t &frag, size_t *largestp=0) { + size_t status(size_t &frag, size_t *largestp=0) { return chHeapStatus(&heap, &frag, largestp); } -- cgit v1.2.3