Skip to content

Event loop #2801

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

Closed
wants to merge 5 commits into from
Closed
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
90 changes: 90 additions & 0 deletions TESTS/mbed_drivers/event/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES Ovoid CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity/unity.h"
#include "utest/utest.h"
#include "EventLoop.h"
#include "Event.h"
#include <stdio.h>

using namespace utest::v1;

static EventLoop queue(16);
static osThreadId main_thread_id;
static osThreadId expected_thread_id;
static volatile uint32_t call_count;

static void test_func() {
if (expected_thread_id == NULL) {
// We're testing the event queue, so save the queue thread ID for subsequent
// tests (and make sure that we're not running in the main context).
TEST_ASSERT_NOT_EQUAL((uint32_t)expected_thread_id, (uint32_t)main_thread_id);
expected_thread_id = Thread::gettid();
} else {
TEST_ASSERT_EQUAL((uint32_t)expected_thread_id, (uint32_t)Thread::gettid());
}
call_count ++;
}

static void test_immediate_events() {
expected_thread_id = main_thread_id;
call_count = 0;
// Post 10 "regular" events (they'll execute immediately in
// the context of the main thread)
for (unsigned i = 0; i < 10; i ++) {
event(test_func, NULL).call();
TEST_ASSERT_EQUAL_UINT32(call_count, i + 1);
}
}

static void test_queued_events() {
// We don't know the event thread ID. it'll be set by the first call to test_func
expected_thread_id = NULL;
call_count = 0;
// Start event thread now
queue.start();
// Post 10 events that will be queued
for (unsigned i = 0; i < 10; i ++) {
event(test_func, &queue).call();
}
// Wait for them to finish
while (call_count < 10) {
wait_ms(10);
}
// Wait a bit more and check count again to check that no more events are executed
wait_ms(200);
TEST_ASSERT_EQUAL_UINT32(call_count, 10);
}

static Case cases[] = {
Case("Test immediate events", test_immediate_events),
Case("Test queued events", test_queued_events)
};

static status_t greentea_test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(20, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}

static Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);

int main() {
main_thread_id = Thread::gettid();
// Run tests
Harness::run(specification);
}
76 changes: 76 additions & 0 deletions TESTS/mbed_drivers/event_loop/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* mbed Microcontroller Library
* Copyright (c) 2016 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES Ovoid CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity/unity.h"
#include "utest/utest.h"
#include "EventLoop.h"
#include "Callback.h"
#include <stdio.h>

using namespace utest::v1;

static EventLoop loop(16);
static osThreadId main_thread_id;
static osThreadId other_thread_id;
static volatile uint32_t cb_count;

static void cb1() {
if (cb_count == 0) {
// Check that we don't run in the main thread context
TEST_ASSERT_NOT_EQUAL((uint32_t)Thread::gettid(), (uint32_t)main_thread_id);
// And save current running context
other_thread_id = Thread::gettid();
} else {
// Check that we're running in the same context as before
TEST_ASSERT_EQUAL((uint32_t)Thread::gettid(), (uint32_t)other_thread_id);
}
cb_count ++;
}

static void test_event_loop_callbacks() {
// Post 10 callbacks
for (unsigned i = 0; i < 10; i ++) {
loop.post(callback(cb1));
}
// Wait for all callbacks to finish
while (cb_count < 10) {
wait_ms(10);
}
// Wait a bit more and check count again to check that no more events are executed
wait_ms(200);
TEST_ASSERT_EQUAL_UINT32(cb_count, 10);
}

static Case cases[] = {
Case("Test event loop callbacks", test_event_loop_callbacks)
};

static status_t greentea_test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(20, "default_auto");
return greentea_test_setup_handler(number_of_cases);
}

static Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);

int main() {
// Start event thread
main_thread_id = Thread::gettid();
loop.start();
// Run tests
Harness::run(specification);
}
38 changes: 31 additions & 7 deletions TESTS/mbedmicro-rtos-mbed/mail/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,21 @@ typedef struct {
Mail<mail_t, QUEUE_SIZE> mail_box;

void send_thread (void const *argument) {
static uint32_t i = 10;
uint32_t i = 10;
GenericMail<mail_t> *p_mail = (GenericMail<mail_t>*)argument;

while (true) {
i++; // fake data update
mail_t *mail = mail_box.alloc();
mail_t *mail = p_mail->alloc();
mail->voltage = CREATE_VOLTAGE(i);
mail->current = CREATE_CURRENT(i);
mail->counter = i;
mail_box.put(mail);
p_mail->put(mail);
Thread::wait(QUEUE_PUT_DELAY);
}
}

int main (void) {
GREENTEA_SETUP(20, "default_auto");

Thread thread(send_thread, NULL, osPriorityNormal, STACK_SIZE);
bool test_once(GenericMail<mail_t>& mail_box) {
bool result = true;
int result_counter = 0;

Expand All @@ -83,6 +82,31 @@ int main (void) {
}
}
}
return result;
}

int main (void) {
GREENTEA_SETUP(20, "default_auto");

Thread thread(osPriorityNormal, STACK_SIZE);

printf("Testing Mail\r\n");
thread.start(callback(&mail_box, send_thread));
bool result = test_once(mail_box);
thread.terminate();
if (!result) {
printf("Mail tests failed!\r\n");
GREENTEA_TESTSUITE_RESULT(false);
return 0;
}

printf("Testing DynamicMail\r\n");
DynamicMail<mail_t> dyn_mail(QUEUE_SIZE);
thread.start(callback(&dyn_mail, send_thread));
result = test_once(dyn_mail);
if (!result) {
printf("DynamicMail tests failed!\r\n");
}
GREENTEA_TESTSUITE_RESULT(result);
return 0;
}
6 changes: 3 additions & 3 deletions hal/api/CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "can_api.h"
#include "can_helper.h"
#include "Callback.h"
#include "Event.h"
#include "PlatformMutex.h"

namespace mbed {
Expand Down Expand Up @@ -214,7 +214,7 @@ class CAN {
* @param func A pointer to a void function, or 0 to set as none
* @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
*/
void attach(Callback<void()> func, IrqType type=RxIrq);
void attach(const Event& func, IrqType type=RxIrq);

/** Attach a member function to call whenever a CAN frame received interrupt
* is generated.
Expand Down Expand Up @@ -248,7 +248,7 @@ class CAN {
virtual void lock();
virtual void unlock();
can_t _can;
Callback<void()> _irq[IrqCnt];
Event _irq[IrqCnt];
PlatformMutex _mutex;
};

Expand Down
Loading