Skip to content

Commit c70be02

Browse files
committed
Rationalise Queue/Mail get/put APIs
To line up with MemoryPool/Mail alloc, rework naming of get/put Queue::get -> try_get, try_get_for Queue::put -> try_put, try_put_for Mail::get -> try_get, try_get_for Mail::put (no change, but assert that it works) In the future the names `get` and `put` can be used for untimed blocking operations. In the interim, you have to use `try_get_for(Kernel::wait_for_u32_forever)`. `Mail::put` differs in that it has always been a non-blocking call, but it can be assumed to always succeed when used correctly, because the Queue has enough room to store a pointer to every block in the MemoryPool. It could in future be made a `void` return, similar to the change made to `Mutex::lock`.
1 parent f28a5e9 commit c70be02

File tree

4 files changed

+229
-212
lines changed

4 files changed

+229
-212
lines changed

TESTS/mbedmicro-rtos-mbed/mail/main.cpp

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ void receive_thread(Mail<mail_t, queue_size> *m, uint8_t thread_id, milliseconds
8383

8484
ThisThread::sleep_for(wait);
8585
for (uint32_t i = 0; i < queue_size; i++) {
86-
osEvent evt = m->get();
87-
if (evt.status == osEventMail) {
88-
mail_t *mail = (mail_t *)evt.value.p;
86+
mail_t *mail = m->try_get_for(Kernel::wait_for_u32_forever);
87+
if (mail) {
8988
const uint8_t id = mail->thread_id;
9089

9190
// verify thread id
@@ -121,9 +120,8 @@ void test_single_thread_order(void)
121120

122121
for (uint32_t i = 0; i < QUEUE_SIZE; i++) {
123122
// mail receive (main thread)
124-
osEvent evt = mail_box.get();
125-
if (evt.status == osEventMail) {
126-
mail_t *mail = (mail_t *)evt.value.p;
123+
mail_t *mail = mail_box.try_get_for(Kernel::wait_for_u32_forever);
124+
if (mail) {
127125
const uint8_t id = mail->thread_id;
128126

129127
// verify thread id
@@ -163,9 +161,8 @@ void test_multi_thread_order(void)
163161

164162
for (uint32_t i = 0; i < QUEUE_SIZE; i++) {
165163
// mail receive (main thread)
166-
osEvent evt = mail_box.get();
167-
if (evt.status == osEventMail) {
168-
mail_t *mail = (mail_t *)evt.value.p;
164+
mail_t *mail = mail_box.try_get_for(Kernel::wait_for_u32_forever);
165+
if (mail) {
169166
const uint8_t id = mail->thread_id;
170167

171168
// verify thread id
@@ -279,32 +276,32 @@ void test_free_null()
279276
/** Test get from empty mailbox with timeout set
280277
281278
Given an empty mailbox
282-
When @a get is called on the mailbox with timeout of 50
283-
Then mailbox returns status of osOK, but no data after specified amount of time
279+
When @a try_get_for is called on the mailbox with timeout of 50ms
280+
Then mailbox returns no data
284281
*/
285282
void test_get_empty_timeout()
286283
{
287284
Mail<uint32_t, 4> mail_box;
288285
Timer timer;
289286

290287
timer.start();
291-
osEvent evt = mail_box.get(50ms);
288+
uint32_t *mail = mail_box.try_get_for(50ms);
292289
TEST_ASSERT_DURATION_WITHIN(5ms, 50ms, timer.elapsed_time());
293-
TEST_ASSERT_EQUAL(osEventTimeout, evt.status);
290+
TEST_ASSERT_NULL(mail);
294291
}
295292

296293
/** Test get from empty mailbox with 0 timeout
297294
298295
Given an empty mailbox
299-
When @a get is called on the mailbox with timeout of 0
300-
Then mailbox returns status of osOK, but no data
296+
When @a try_get is called on the mailbox
297+
Then mailbox returns no data
301298
*/
302299
void test_get_empty_no_timeout()
303300
{
304301
Mail<uint32_t, 4> mail_box;
305302

306-
osEvent evt = mail_box.get(0ms);
307-
TEST_ASSERT_EQUAL(osOK, evt.status);
303+
uint32_t *mail = mail_box.try_get();
304+
TEST_ASSERT_NULL(mail);
308305
}
309306

310307
/** Test mail order
@@ -317,7 +314,6 @@ void test_get_empty_no_timeout()
317314
void test_order(void)
318315
{
319316
osStatus status;
320-
osEvent evt;
321317
Mail<int32_t, 4> mail_box;
322318
const int32_t TEST_VAL1 = 123;
323319
const int32_t TEST_VAL2 = 456;
@@ -326,27 +322,21 @@ void test_order(void)
326322
TEST_ASSERT_NOT_EQUAL(NULL, mail1);
327323

328324
*mail1 = TEST_VAL1;
329-
status = mail_box.put(mail1);
330-
TEST_ASSERT_EQUAL(osOK, status);
325+
mail_box.put(mail1);
331326

332327
int32_t *mail2 = mail_box.try_alloc();
333328
TEST_ASSERT_NOT_EQUAL(NULL, mail2);
334329

335330
*mail2 = TEST_VAL2;
336-
status = mail_box.put(mail2);
337-
TEST_ASSERT_EQUAL(osOK, status);
338-
331+
mail_box.put(mail2);
339332

340-
evt = mail_box.get();
341-
TEST_ASSERT_EQUAL(evt.status, osEventMail);
342333

343-
mail1 = (int32_t *)evt.value.p;
334+
mail1 = mail_box.try_get_for(Kernel::wait_for_u32_forever);
335+
TEST_ASSERT_NOT_NULL(mail1);
344336
TEST_ASSERT_EQUAL(TEST_VAL1, *mail1);
345337

346-
evt = mail_box.get();
347-
TEST_ASSERT_EQUAL(evt.status, osEventMail);
348-
349-
mail2 = (int32_t *)evt.value.p;
338+
mail2 = mail_box.try_get_for(Kernel::wait_for_u32_forever);
339+
TEST_ASSERT_NOT_NULL(mail2);
350340
TEST_ASSERT_EQUAL(TEST_VAL2, *mail2);
351341

352342

@@ -369,7 +359,6 @@ void test_max_size()
369359
{
370360
osStatus status;
371361
Mail<uint32_t, 4> mail_box;
372-
const uint32_t TEST_VAL = 123;
373362

374363
// 1 OK
375364
uint32_t *mail1 = mail_box.try_alloc();
@@ -422,13 +411,10 @@ void test_data_type(void)
422411
TEST_ASSERT_NOT_EQUAL(NULL, mail);
423412

424413
*mail = TEST_VAL;
425-
status = mail_box.put(mail);
426-
TEST_ASSERT_EQUAL(osOK, status);
427-
428-
osEvent evt = mail_box.get();
429-
TEST_ASSERT_EQUAL(evt.status, osEventMail);
414+
mail_box.put(mail);
430415

431-
mail = (T *)evt.value.p;
416+
mail = mail_box.try_get_for(Kernel::wait_for_u32_forever);
417+
TEST_ASSERT_NOT_NULL(mail);
432418
TEST_ASSERT_EQUAL(TEST_VAL, *mail);
433419

434420

0 commit comments

Comments
 (0)