Skip to content

Commit

Permalink
uORBManager.cpp: Use InxedexStack for global semaphore list
Browse files Browse the repository at this point in the history
This optimizes one loop when reserving new signaling semaphore.
  • Loading branch information
pussuw committed May 8, 2024
1 parent 981b7ea commit e74f7c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
20 changes: 7 additions & 13 deletions platforms/common/uORB/uORBManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,29 +626,26 @@ void uORB::Manager::GlobalSemPool::init(void)

void uORB::Manager::GlobalSemPool::free(int8_t i)
{
IndexedStackHandle<SEM_LIST_T> 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<SEM_LIST_T> 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;
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions platforms/common/uORB/uORBManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 */
};
Expand All @@ -662,6 +662,7 @@ class Manager
void lock() { do {} while (px4_sem_wait(&_semLock) != 0); }
void unlock() { px4_sem_post(&_semLock); }

IndexedStack<SEM_LIST_T> _global_sems;
GlobalLock _global_sem[NUM_GLOBAL_SEMS];
px4_sem_t _semLock;
} g_sem_pool;
Expand Down

0 comments on commit e74f7c1

Please sign in to comment.