From d4b99ae3f8698df11c865c83ec65b316bb5bb64b Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Wed, 15 Nov 2017 13:21:01 +0100 Subject: libio: Add assert to rtems_libio_iop_drop() This assert helps to detect an invalid reference counting in RTEMS_DEBUG configurations. Update #3132. --- cpukit/libcsupport/include/rtems/libio_.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/cpukit/libcsupport/include/rtems/libio_.h b/cpukit/libcsupport/include/rtems/libio_.h index cf4ea78f7e..5cc31eed64 100644 --- a/cpukit/libcsupport/include/rtems/libio_.h +++ b/cpukit/libcsupport/include/rtems/libio_.h @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -181,11 +182,33 @@ static inline unsigned int rtems_libio_iop_hold( rtems_libio_t *iop ) */ static inline void rtems_libio_iop_drop( rtems_libio_t *iop ) { +#if defined(RTEMS_DEBUG) + unsigned int flags; + bool success; + + flags = _Atomic_Load_uint( &iop->flags, ATOMIC_ORDER_RELAXED ); + + do { + unsigned int desired; + + _Assert( flags >= LIBIO_FLAGS_REFERENCE_INC ); + + desired = flags - LIBIO_FLAGS_REFERENCE_INC; + success = _Atomic_Compare_exchange_uint( + &iop->flags, + &flags, + desired, + ATOMIC_ORDER_RELEASE, + ATOMIC_ORDER_RELAXED + ); + } while ( !success ); +#else _Atomic_Fetch_sub_uint( &iop->flags, LIBIO_FLAGS_REFERENCE_INC, ATOMIC_ORDER_RELEASE ); +#endif } /* -- cgit v1.2.3