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

Commit 65d27b2

Browse files
committed
Moved to statically allocated Semaphores in mbed implementation
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.
1 parent b5a79ea commit 65d27b2

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

equeue_mbed.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,26 @@ void equeue_mutex_unlock(equeue_mutex_t *m) {
6363
// Semaphore operations
6464
#ifdef MBED_CONF_RTOS_PRESENT
6565

66-
static inline Semaphore *sema(equeue_sema_t *s) {
67-
return static_cast<Semaphore*>(*s);
68-
}
69-
7066
int equeue_sema_create(equeue_sema_t *s) {
71-
*s = new Semaphore(0);
72-
return sema(s) ? 0 : -1;
67+
MBED_ASSERT(sizeof(equeue_sema_t) >= sizeof(Semaphore));
68+
new (s) Semaphore(0);
69+
return 0;
7370
}
7471

7572
void equeue_sema_destroy(equeue_sema_t *s) {
76-
delete sema(s);
73+
reinterpret_cast<Semaphore*>(s)->~Semaphore();
7774
}
7875

7976
void equeue_sema_signal(equeue_sema_t *s) {
80-
sema(s)->release();
77+
reinterpret_cast<Semaphore*>(s)->release();
8178
}
8279

8380
bool equeue_sema_wait(equeue_sema_t *s, int ms) {
84-
int t = sema(s)->wait(ms < 0 ? osWaitForever : ms);
85-
return t > 0;
81+
if (ms < 0) {
82+
ms = osWaitForever;
83+
}
84+
85+
return (reinterpret_cast<Semaphore*>(s)->wait(ms) > 0);
8686
}
8787

8888
#else

equeue_sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extern "C" {
2323
typedef sem_t equeue_sema_t;
2424
#elif defined(__MBED__)
2525
#ifdef MBED_CONF_RTOS_PRESENT
26-
typedef void *equeue_sema_t;
26+
typedef unsigned equeue_sema_t[8];
2727
#else
2828
typedef bool equeue_sema_t;
2929
#endif

0 commit comments

Comments
 (0)