Skip to content

Commit 2317ddf

Browse files
committed
RTX2: Clean mbed wrapper classes
Make the member names consistent, use RTX objects as a memory backend.
1 parent 308b3e0 commit 2317ddf

File tree

8 files changed

+74
-72
lines changed

8 files changed

+74
-72
lines changed

rtos/MemoryPool.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,28 @@ class MemoryPool {
4141
public:
4242
/** Create and Initialize a memory pool. */
4343
MemoryPool() {
44-
memset(_pool_m, 0, sizeof(_pool_m));
45-
_pool_attr.mp_mem = _pool_m;
46-
_pool_attr.mp_size = sizeof(_pool_m);
47-
_pool_attr.cb_mem = _ob_m;
48-
_pool_attr.cb_size = sizeof(_ob_m);
49-
_pool_id = osMemoryPoolNew(pool_sz, sizeof(T), &_pool_attr);
50-
MBED_ASSERT(_pool_id);
44+
memset(_pool_mem, 0, sizeof(_pool_mem));
45+
memset(&_obj_mem, 0, sizeof(_obj_mem));
46+
_attr.mp_mem = _pool_mem;
47+
_attr.mp_size = sizeof(_pool_mem);
48+
_attr.cb_mem = &_obj_mem;
49+
_attr.cb_size = sizeof(_obj_mem);
50+
_id = osMemoryPoolNew(pool_sz, sizeof(T), &_attr);
51+
MBED_ASSERT(_id);
5152
}
5253

5354
/** Allocate a memory block of type T from a memory pool.
5455
@return address of the allocated memory block or NULL in case of no memory available.
5556
*/
5657
T* alloc(void) {
57-
return (T*)osMemoryPoolAlloc(_pool_id, 0);
58+
return (T*)osMemoryPoolAlloc(_id, 0);
5859
}
5960

6061
/** Allocate a memory block of type T from a memory pool and set memory block to zero.
6162
@return address of the allocated memory block or NULL in case of no memory available.
6263
*/
6364
T* calloc(void) {
64-
T *item = (T*)osMemoryPoolAlloc(_pool_id, 0);
65+
T *item = (T*)osMemoryPoolAlloc(_id, 0);
6566
if (item != NULL) {
6667
memset(item, 0, sizeof(T));
6768
}
@@ -73,14 +74,14 @@ class MemoryPool {
7374
@return status code that indicates the execution status of the function.
7475
*/
7576
osStatus_t free(T *block) {
76-
return osMemoryPoolFree(_pool_id, (void*)block);
77+
return osMemoryPoolFree(_id, (void*)block);
7778
}
7879

7980
private:
80-
osMemoryPoolId_t _pool_id;
81-
osMemoryPoolAttr_t _pool_attr;
82-
char _pool_m[sizeof(T) * pool_sz];
83-
char _ob_m[sizeof(os_memory_pool_t)];
81+
osMemoryPoolId_t _id;
82+
osMemoryPoolAttr_t _attr;
83+
char _pool_mem[sizeof(T) * pool_sz];
84+
os_memory_pool_t _obj_mem;
8485
};
8586

8687
}

rtos/Mutex.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,28 @@
2828
namespace rtos {
2929

3030
Mutex::Mutex() {
31-
memset(_mutex_data, 0, sizeof(_mutex_data));
32-
_osMutexAttr.cb_mem = _mutex_data;
33-
_osMutexAttr.cb_size = sizeof(_mutex_data);
34-
_osMutexAttr.attr_bits = osMutexRecursive;
35-
_osMutexId = osMutexNew(&_osMutexAttr);
36-
MBED_ASSERT(_osMutexId);
31+
memset(&_obj_mem, 0, sizeof(_obj_mem));
32+
_attr.cb_mem = &_obj_mem;
33+
_attr.cb_size = sizeof(_obj_mem);
34+
_attr.attr_bits = osMutexRecursive;
35+
_id = osMutexNew(&_attr);
36+
MBED_ASSERT(_id);
3737
}
3838

3939
osStatus_t Mutex::lock(uint32_t millisec) {
40-
return osMutexAcquire(_osMutexId, millisec);
40+
return osMutexAcquire(_id, millisec);
4141
}
4242

4343
bool Mutex::trylock() {
44-
return (osMutexAcquire(_osMutexId, 0) == osOK);
44+
return (osMutexAcquire(_id, 0) == osOK);
4545
}
4646

4747
osStatus_t Mutex::unlock() {
48-
return osMutexRelease(_osMutexId);
48+
return osMutexRelease(_id);
4949
}
5050

5151
Mutex::~Mutex() {
52-
osMutexDelete(_osMutexId);
52+
osMutexDelete(_id);
5353
}
5454

5555
}

rtos/Mutex.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class Mutex {
5757
~Mutex();
5858

5959
private:
60-
osMutexId_t _osMutexId;
61-
osMutexAttr_t _osMutexAttr;
62-
char _mutex_data[sizeof(os_mutex_t)];
60+
osMutexId_t _id;
61+
osMutexAttr_t _attr;
62+
os_mutex_t _obj_mem;
6363
};
6464

6565
}

rtos/Queue.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ class Queue {
4444
public:
4545
/** Create and initialize a message Queue. */
4646
Queue() {
47-
memset(_queue_q, 0, sizeof(_queue_q));
48-
_queue_attr.mq_mem = _queue_q;
49-
_queue_attr.mq_size = sizeof(_queue_q);
50-
_queue_attr.cb_mem = _ob_m;
51-
_queue_attr.cb_size = sizeof(_ob_m);
52-
_queue_id = osMessageQueueNew(queue_sz, sizeof(T*), &_queue_attr);
53-
MBED_ASSERT(_queue_id);
47+
memset(&_obj_mem, 0, sizeof(_obj_mem));
48+
_attr.mq_mem = _queue_mem;
49+
_attr.mq_size = sizeof(_queue_mem);
50+
_attr.cb_mem = &_obj_mem;
51+
_attr.cb_size = sizeof(_obj_mem);
52+
_id = osMessageQueueNew(queue_sz, sizeof(T*), &_attr);
53+
MBED_ASSERT(_id);
5454
}
5555

5656
/** Put a message in a Queue.
@@ -60,7 +60,7 @@ class Queue {
6060
@return status code that indicates the execution status of the function.
6161
*/
6262
osStatus_t put(T* data, uint32_t millisec=0, uint8_t prio=0) {
63-
return osMessageQueuePut(_queue_id, &data, prio, millisec);
63+
return osMessageQueuePut(_id, &data, prio, millisec);
6464
}
6565

6666
/** Get a message or Wait for a message from a Queue.
@@ -69,18 +69,18 @@ class Queue {
6969
*/
7070
T *get(uint32_t millisec=osWaitForever) {
7171
T *data;
72-
osStatus_t res = osMessageQueueGet(_queue_id, &data, NULL, millisec);
72+
osStatus_t res = osMessageQueueGet(_id, &data, NULL, millisec);
7373
if (res != osOK) {
7474
data = NULL;
7575
}
7676
return data;
7777
}
7878

7979
private:
80-
osMessageQueueId_t _queue_id;
81-
osMessageQueueAttr_t _queue_attr;
82-
char _queue_q[queue_sz * (sizeof(T*) + sizeof(os_message_t))];
83-
char _ob_m[sizeof(os_message_queue_t)];
80+
osMessageQueueId_t _id;
81+
osMessageQueueAttr_t _attr;
82+
char _queue_mem[queue_sz * (sizeof(T*) + sizeof(os_message_t))];
83+
os_message_queue_t _obj_mem;
8484
};
8585

8686
}

rtos/Semaphore.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@
2727
namespace rtos {
2828

2929
Semaphore::Semaphore(int32_t count, uint16_t max_count) {
30-
memset(_semaphore_data, 0, sizeof(_semaphore_data));
31-
_osSemaphoreAttr.cb_mem = _semaphore_data;
32-
_osSemaphoreAttr.cb_size = sizeof(_semaphore_data);
33-
_osSemaphoreId = osSemaphoreNew(max_count, count, &_osSemaphoreAttr);
34-
MBED_ASSERT(_osSemaphoreId != NULL);
30+
memset(&_obj_mem, 0, sizeof(_obj_mem));
31+
_attr.cb_mem = &_obj_mem;
32+
_attr.cb_size = sizeof(_obj_mem);
33+
_id = osSemaphoreNew(max_count, count, &_attr);
34+
MBED_ASSERT(_id != NULL);
3535
}
3636

3737
osStatus_t Semaphore::wait(uint32_t millisec) {
38-
return osSemaphoreAcquire(_osSemaphoreId, millisec);
38+
return osSemaphoreAcquire(_id, millisec);
3939
}
4040

4141
osStatus_t Semaphore::release(void) {
42-
return osSemaphoreRelease(_osSemaphoreId);
42+
return osSemaphoreRelease(_id);
4343
}
4444

4545
Semaphore::~Semaphore() {
46-
osSemaphoreDelete(_osSemaphoreId);
46+
osSemaphoreDelete(_id);
4747
}
4848

4949
}

rtos/Semaphore.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ class Semaphore {
5353
~Semaphore();
5454

5555
private:
56-
osSemaphoreId_t _osSemaphoreId;
57-
osSemaphoreAttr_t _osSemaphoreAttr;
58-
char _semaphore_data[sizeof(os_semaphore_t)];
56+
osSemaphoreId_t _id;
57+
osSemaphoreAttr_t _attr;
58+
os_semaphore_t _obj_mem;
5959
};
6060

6161
}

rtos/Thread.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ void Thread::constructor(osPriority_t priority,
3939
_tid = 0;
4040
_dynamic_stack = (stack_mem == NULL);
4141

42-
_thread_attr.priority = priority;
43-
_thread_attr.stack_size = stack_size;
44-
_thread_attr.stack_mem = (uint32_t*)stack_mem;
42+
_attr.priority = priority;
43+
_attr.stack_size = stack_size;
44+
_attr.stack_mem = (uint32_t*)stack_mem;
4545
}
4646

4747
void Thread::constructor(Callback<void()> task,
@@ -70,24 +70,25 @@ osStatus_t Thread::start(Callback<void()> task) {
7070
return osErrorParameter;
7171
}
7272

73-
if (_thread_attr.stack_mem == NULL) {
74-
_thread_attr.stack_mem = new uint32_t[_thread_attr.stack_size/sizeof(uint32_t)];
75-
MBED_ASSERT(_thread_attr.stack_mem != NULL);
73+
if (_attr.stack_mem == NULL) {
74+
_attr.stack_mem = new uint32_t[_attr.stack_size/sizeof(uint32_t)];
75+
MBED_ASSERT(_attr.stack_mem != NULL);
7676
}
7777

7878
//Fill the stack with a magic word for maximum usage checking
79-
for (uint32_t i = 0; i < (_thread_attr.stack_size / sizeof(uint32_t)); i++) {
80-
((uint32_t *)_thread_attr.stack_mem)[i] = 0xE25A2EA5;
79+
for (uint32_t i = 0; i < (_attr.stack_size / sizeof(uint32_t)); i++) {
80+
((uint32_t *)_attr.stack_mem)[i] = 0xE25A2EA5;
8181
}
8282

83-
_thread_attr.cb_size = sizeof(_ob_mem);
84-
_thread_attr.cb_mem = _ob_mem;
83+
memset(&_obj_mem, 0, sizeof(_obj_mem));
84+
_attr.cb_size = sizeof(_obj_mem);
85+
_attr.cb_mem = &_obj_mem;
8586
_task = task;
86-
_tid = osThreadNew(Thread::_thunk, this, &_thread_attr);
87+
_tid = osThreadNew(Thread::_thunk, this, &_attr);
8788
if (_tid == NULL) {
8889
if (_dynamic_stack) {
89-
delete[] (uint32_t *)(_thread_attr.stack_mem);
90-
_thread_attr.stack_mem = (uint32_t*)NULL;
90+
delete[] (uint32_t *)(_attr.stack_mem);
91+
_attr.stack_mem = (uint32_t*)NULL;
9192
}
9293
_mutex.unlock();
9394
_join_sem.release();
@@ -256,8 +257,8 @@ Thread::~Thread() {
256257
// terminate is thread safe
257258
terminate();
258259
if (_dynamic_stack) {
259-
delete[] (uint32_t*)(_thread_attr.stack_mem);
260-
_thread_attr.stack_mem = (uint32_t*)NULL;
260+
delete[] (uint32_t*)(_attr.stack_mem);
261+
_attr.stack_mem = (uint32_t*)NULL;
261262
}
262263
}
263264

rtos/Thread.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,12 @@ class Thread {
326326
static void _thunk(void * thread_ptr);
327327

328328
mbed::Callback<void()> _task;
329-
osThreadId_t _tid;
330-
osThreadAttr_t _thread_attr;
331-
bool _dynamic_stack;
332-
Semaphore _join_sem;
333-
Mutex _mutex;
334-
char _ob_mem[sizeof(os_thread_t)];
329+
osThreadId_t _tid;
330+
osThreadAttr_t _attr;
331+
bool _dynamic_stack;
332+
Semaphore _join_sem;
333+
Mutex _mutex;
334+
os_thread_t _obj_mem;
335335
};
336336

337337
}

0 commit comments

Comments
 (0)