Skip to content
This repository has been archived by the owner on Aug 19, 2021. It is now read-only.

Commit

Permalink
Moved to statically allocated Semaphores in mbed implementation
Browse files Browse the repository at this point in the history
Used static buffer + placement new + reinterpret cast to allow
static allocation of the C++ Semaphore class in C. An assertion
is used to protect against invalid size, although this should be
changed to a static-assertion if available.
  • Loading branch information
geky committed Aug 6, 2016
1 parent b5a79ea commit 65d27b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions equeue_mbed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,26 @@ void equeue_mutex_unlock(equeue_mutex_t *m) {
// Semaphore operations
#ifdef MBED_CONF_RTOS_PRESENT

static inline Semaphore *sema(equeue_sema_t *s) {
return static_cast<Semaphore*>(*s);
}

int equeue_sema_create(equeue_sema_t *s) {
*s = new Semaphore(0);
return sema(s) ? 0 : -1;
MBED_ASSERT(sizeof(equeue_sema_t) >= sizeof(Semaphore));
new (s) Semaphore(0);
return 0;
}

void equeue_sema_destroy(equeue_sema_t *s) {
delete sema(s);
reinterpret_cast<Semaphore*>(s)->~Semaphore();
}

void equeue_sema_signal(equeue_sema_t *s) {
sema(s)->release();
reinterpret_cast<Semaphore*>(s)->release();
}

bool equeue_sema_wait(equeue_sema_t *s, int ms) {
int t = sema(s)->wait(ms < 0 ? osWaitForever : ms);
return t > 0;
if (ms < 0) {
ms = osWaitForever;
}

return (reinterpret_cast<Semaphore*>(s)->wait(ms) > 0);
}

#else
Expand Down
2 changes: 1 addition & 1 deletion equeue_sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extern "C" {
typedef sem_t equeue_sema_t;
#elif defined(__MBED__)
#ifdef MBED_CONF_RTOS_PRESENT
typedef void *equeue_sema_t;
typedef unsigned equeue_sema_t[8];
#else
typedef bool equeue_sema_t;
#endif
Expand Down

0 comments on commit 65d27b2

Please sign in to comment.