[rtems commit] libio: Add assert to rtems_libio_iop_drop()

Sebastian Huber sebh at rtems.org
Wed Nov 15 12:23:06 UTC 2017


Module:    rtems
Branch:    master
Commit:    d4b99ae3f8698df11c865c83ec65b316bb5bb64b
Changeset: http://git.rtems.org/rtems/commit/?id=d4b99ae3f8698df11c865c83ec65b316bb5bb64b

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Wed Nov 15 13:21:01 2017 +0100

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 cf4ea78..5cc31ee 100644
--- a/cpukit/libcsupport/include/rtems/libio_.h
+++ b/cpukit/libcsupport/include/rtems/libio_.h
@@ -29,6 +29,7 @@
 #include <rtems.h>
 #include <rtems/libio.h>
 #include <rtems/seterr.h>
+#include <rtems/score/assert.h>
 
 #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
 }
 
 /*




More information about the vc mailing list