diff --git a/platforms/common/uORB/uORBManager.cpp b/platforms/common/uORB/uORBManager.cpp index c523ba8baee8..e2846c096bbc 100644 --- a/platforms/common/uORB/uORBManager.cpp +++ b/platforms/common/uORB/uORBManager.cpp @@ -626,29 +626,26 @@ void uORB::Manager::GlobalSemPool::init(void) void uORB::Manager::GlobalSemPool::free(int8_t i) { + IndexedStackHandle sems(_global_sems); + lock(); - _global_sem[i].free(); - _global_sem[i].in_use = false; + sems.push_free(i); unlock(); } int8_t uORB::Manager::GlobalSemPool::reserve() { + IndexedStackHandle sems(_global_sems); + lock(); // Find the first free lock - int8_t i; - - for (i = 0; i < NUM_GLOBAL_SEMS; i++) { - if (!_global_sem[i].in_use) { - break; - } - } + int8_t i = sems.pop_free(); // Check that we got one - if (i == NUM_GLOBAL_SEMS) { + if (!sems.handle_valid(i)) { PX4_ERR("Out of global locks"); unlock(); return -1; @@ -657,9 +654,6 @@ int8_t uORB::Manager::GlobalSemPool::reserve() // Make sure the semaphore is initialized properly for the new user _global_sem[i].init(); - // Mark this one as in use - _global_sem[i].in_use = true; - unlock(); return i; diff --git a/platforms/common/uORB/uORBManager.hpp b/platforms/common/uORB/uORBManager.hpp index 07cbb5a31a39..96e95c7b04b5 100644 --- a/platforms/common/uORB/uORBManager.hpp +++ b/platforms/common/uORB/uORBManager.hpp @@ -51,6 +51,7 @@ #endif /* CONFIG_ORB_COMMUNICATOR */ #define NUM_GLOBAL_SEMS 40 +#define SEM_LIST_T GlobalLock, int8_t, NUM_GLOBAL_SEMS namespace uORB { @@ -645,15 +646,14 @@ class Manager #if defined(__PX4_NUTTX) sem_setprotocol(&_sem, SEM_PRIO_NONE); #endif - in_use = false; } void free() { px4_sem_destroy(&_sem); } int take() { return px4_sem_wait(&_sem); } int take_timedwait(struct timespec *abstime) { return px4_sem_timedwait(&_sem, abstime); } void release() { px4_sem_post(&_sem); } int value() { int value; px4_sem_getvalue(&_sem, &value); return value; } - bool in_use{false}; + int8_t next; /* For linkage */ private: px4_sem_t _sem; /* For signaling to the callback thread */ }; @@ -662,6 +662,7 @@ class Manager void lock() { do {} while (px4_sem_wait(&_semLock) != 0); } void unlock() { px4_sem_post(&_semLock); } + IndexedStack _global_sems; GlobalLock _global_sem[NUM_GLOBAL_SEMS]; px4_sem_t _semLock; } g_sem_pool;