Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uORBManager.cpp: Use InxedexStack for global semaphore list #690

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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