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: Cleanup for callbacks, semaphores etc #516

Merged
merged 2 commits into from
Oct 6, 2023
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
2 changes: 1 addition & 1 deletion platforms/common/uORB/uORBDeviceNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class DeviceNode
px4::atomic<unsigned> _generation{0}; /**< object generation count */

struct EventWaitItem {
struct SubscriptionCallback *subscriber;
class SubscriptionCallback *subscriber;
hrt_abstime last_update;
uint32_t interval_us;
int8_t lock;
Expand Down
9 changes: 6 additions & 3 deletions platforms/common/uORB/uORBManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,7 @@ int uORB::Manager::orb_poll(orb_poll_struct_t *fds, unsigned int nfds, int timeo
}
}

// recover from releasing multiple times
g_sem_pool.set(lock_idx, 0);
// release the semaphore
g_sem_pool.free(lock_idx);

return err ? -1 : count;
Expand Down Expand Up @@ -486,7 +485,7 @@ uORB::Manager::callback_thread(int argc, char *argv[])
while (true) {
lockThread(per_process_lock);

SubscriptionCallback *sub = dequeueCallback(per_process_lock);
class SubscriptionCallback *sub = dequeueCallback(per_process_lock);

// Pass nullptr to this thread to exit
if (sub == nullptr) {
Expand Down Expand Up @@ -518,6 +517,7 @@ void uORB::Manager::GlobalSemPool::free(int8_t i)
{
lock();

_global_sem[i].free();
_global_sem[i].in_use = false;

unlock();
Expand All @@ -543,6 +543,9 @@ int8_t uORB::Manager::GlobalSemPool::reserve()
return -1;
}

// 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;

Expand Down
25 changes: 9 additions & 16 deletions platforms/common/uORB/uORBManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,6 @@ class Manager
int8_t reserve();
void free(int8_t i);

void set(int8_t i, int val) {_global_sem[i].set(val);}
void take(int8_t i) { do {} while (_global_sem[i].take() != 0); }
int take_interruptible(int8_t i) { return _global_sem[i].take(); }
int take_timedwait(int8_t i, struct timespec *abstime) { return _global_sem[i].take_timedwait(abstime); }
Expand All @@ -643,8 +642,8 @@ class Manager

void cb_lock(int8_t i) { do {} while (_global_sem[i].cb_lock() != 0); }
void cb_unlock(int8_t i) { _global_sem[i].cb_unlock(); }
void cb_set(int8_t i, struct SubscriptionCallback *callback_ptr) { _global_sem[i].cb_set(callback_ptr); }
struct SubscriptionCallback *cb_get(int8_t i) { return _global_sem[i].cb_get(); }
void cb_set(int8_t i, class SubscriptionCallback *callback_ptr) { _global_sem[i].cb_set(callback_ptr); }
class SubscriptionCallback *cb_get(int8_t i) { return _global_sem[i].cb_get(); }

class GlobalLock
{
Expand All @@ -658,27 +657,21 @@ class Manager
sem_setprotocol(&_lock, SEM_PRIO_NONE);
#endif
in_use = false;
_callback_ptr = nullptr;
}
void set(int val)
{
px4_sem_destroy(&_sem);
px4_sem_init(&_sem, 1, val);
#if __PX4_NUTTX
sem_setprotocol(&_sem, SEM_PRIO_NONE);
#endif
}
int take() {return px4_sem_wait(&_sem);}
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); }
void release() { px4_sem_post(&_sem); }
int value() { int value; px4_sem_getvalue(&_sem, &value); return value; }
bool in_use{false};

int cb_lock() { return px4_sem_wait(&_lock); }
void cb_unlock() { px4_sem_post(&_lock); }
void cb_set(struct SubscriptionCallback *callback_ptr) { _callback_ptr = callback_ptr; }
struct SubscriptionCallback *cb_get() { return _callback_ptr; }
void cb_set(class SubscriptionCallback *callback_ptr) { _callback_ptr = callback_ptr; }
class SubscriptionCallback *cb_get() { return _callback_ptr; }
private:
struct SubscriptionCallback *_callback_ptr {nullptr};
class SubscriptionCallback *_callback_ptr {nullptr};
px4_sem_t _sem; /* For signaling to the callback thread */
px4_sem_t _lock; /* For signaling back from the callback thread */
};
Expand Down