Skip to content

Commit

Permalink
Critical tests: use atomics
Browse files Browse the repository at this point in the history
Get rid of a volatile, and use atomics to synchronise with the interrupt
routine instead.

Useful as a non-RTOS basic compilation check for the atomics - the
fuller atomic test relies on the RTOS.
  • Loading branch information
kjbracey committed Mar 27, 2019
1 parent 4144d43 commit 3c36dfe
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions TESTS/mbed_platform/critical_section/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
using utest::v1::Case;


volatile bool callback_called;
bool callback_called;

void tiemout_callback(void)
{
callback_called = true;
core_util_atomic_store(&callback_called, true);
}

template<int N>
Expand All @@ -49,7 +49,7 @@ void critical_section_raii_recursive(Timeout &timeout)
wait_us(wait_time_us);
}
TEST_ASSERT_TRUE(core_util_in_critical_section());
TEST_ASSERT_FALSE(callback_called);
TEST_ASSERT_FALSE(core_util_atomic_load(&callback_called));
}


Expand Down Expand Up @@ -82,31 +82,30 @@ void test_C_API(void)

TEST_ASSERT_FALSE(core_util_in_critical_section());

callback_called = false;
core_util_atomic_store(&callback_called, false);
timeout.attach_us(callback(tiemout_callback), timeout_time_us);
wait_us(wait_time_us);
TEST_ASSERT_TRUE(callback_called);
TEST_ASSERT_TRUE(core_util_atomic_exchange(&callback_called, false));

for (int i = 0; i < N; i++) {
core_util_critical_section_enter();
TEST_ASSERT_TRUE(core_util_in_critical_section());
}

callback_called = false;
timeout.attach_us(callback(tiemout_callback), timeout_time_us);
wait_us(wait_time_us);
TEST_ASSERT_FALSE(callback_called);
TEST_ASSERT_FALSE(core_util_atomic_load(&callback_called));
TEST_ASSERT_TRUE(core_util_in_critical_section());

for (int i = 0; i < N - 1; i++) {
core_util_critical_section_exit();
TEST_ASSERT_TRUE(core_util_in_critical_section());
TEST_ASSERT_FALSE(callback_called);
TEST_ASSERT_FALSE(core_util_atomic_load(&callback_called));
}

core_util_critical_section_exit();
TEST_ASSERT_FALSE(core_util_in_critical_section());
TEST_ASSERT_TRUE(callback_called);
TEST_ASSERT_TRUE(core_util_atomic_load(&callback_called));
}

/** Template for tests
Expand Down Expand Up @@ -138,16 +137,15 @@ void test_CPP_API_constructor_destructor(void)

TEST_ASSERT_FALSE(core_util_in_critical_section());

callback_called = false;
core_util_atomic_store(&callback_called, false);
timeout.attach_us(callback(tiemout_callback), timeout_time_us);
wait_us(wait_time_us);
TEST_ASSERT_TRUE(callback_called);
TEST_ASSERT_TRUE(core_util_atomic_exchange(&callback_called, false));

callback_called = false;
critical_section_raii_recursive<N>(timeout);

TEST_ASSERT_FALSE(core_util_in_critical_section());
TEST_ASSERT_TRUE(callback_called);
TEST_ASSERT_TRUE(core_util_atomic_load(&callback_called));
}

/** Template for tests
Expand Down Expand Up @@ -179,31 +177,30 @@ void test_CPP_API_enable_disable(void)

TEST_ASSERT_FALSE(core_util_in_critical_section());

callback_called = false;
core_util_atomic_store(&callback_called, false);
timeout.attach_us(callback(tiemout_callback), timeout_time_us);
wait_us(wait_time_us);
TEST_ASSERT_TRUE(callback_called);
TEST_ASSERT_TRUE(core_util_atomic_exchange(&callback_called, false));

for (int i = 0; i < N; i++) {
CriticalSectionLock::enable();
TEST_ASSERT_TRUE(core_util_in_critical_section());
}

callback_called = false;
timeout.attach_us(callback(tiemout_callback), timeout_time_us);
wait_us(wait_time_us);
TEST_ASSERT_FALSE(callback_called);
TEST_ASSERT_FALSE(core_util_atomic_load(&callback_called));
TEST_ASSERT_TRUE(core_util_in_critical_section());

for (int i = 0; i < N - 1; i++) {
CriticalSectionLock::disable();
TEST_ASSERT_TRUE(core_util_in_critical_section());
TEST_ASSERT_FALSE(callback_called);
TEST_ASSERT_FALSE(core_util_atomic_load(&callback_called));
}

CriticalSectionLock::disable();
TEST_ASSERT_FALSE(core_util_in_critical_section());
TEST_ASSERT_TRUE(callback_called);
TEST_ASSERT_TRUE(core_util_atomic_load(&callback_called));
}


Expand Down

0 comments on commit 3c36dfe

Please sign in to comment.