diff --git a/platforms/common/uORB/uORBDeviceNode.cpp b/platforms/common/uORB/uORBDeviceNode.cpp index 2ac3b230ff4..7545d545495 100644 --- a/platforms/common/uORB/uORBDeviceNode.cpp +++ b/platforms/common/uORB/uORBDeviceNode.cpp @@ -204,10 +204,14 @@ uORB::DeviceNode::write(cdev::file_t *filp, const char *buffer, size_t buflen) /* Mark at least one data has been published */ _data_valid = true; + // Snapshot under the lock: poll() setup registers and self-checks under it too. + const bool notify_pollers = has_poll_waiters(); + ATOMIC_LEAVE; - /* notify any poll waiters */ - poll_notify(POLLIN); + if (notify_pollers) { + poll_notify(POLLIN); + } return _meta->o_size; } diff --git a/src/lib/cdev/CDev.hpp b/src/lib/cdev/CDev.hpp index e500ad91529..2ca81439f54 100644 --- a/src/lib/cdev/CDev.hpp +++ b/src/lib/cdev/CDev.hpp @@ -269,6 +269,14 @@ protected: */ void unlock() { px4_sem_post(&_lock); } + /** + * True once any poll waiter has been registered. Read it under + * ATOMIC_ENTER: poll() setup stores the waiter and then checks + * poll_state() under the same section, so a waiter that registers + * after a false reading sees the state change itself. + */ + bool has_poll_waiters() const { return _max_pollwaiters > 0; } + px4_sem_t _lock; /**< lock to protect access to all class members (also for derived classes) */ private: