-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: lib: cpp: add tests for std::mutex and friends
Add tests for ISO C++ std::mutex. Signed-off-by: Christopher Friedt <[email protected]>
- Loading branch information
Showing
9 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.13.1) | ||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(mutex) | ||
|
||
FILE(GLOB_RECURSE app_sources src/*.cpp) | ||
target_sources(app PRIVATE ${app_sources}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
CONFIG_ZTEST=y | ||
|
||
CONFIG_CPP=y | ||
CONFIG_REQUIRES_FULL_LIBCPP=y | ||
CONFIG_CPP_EXCEPTIONS=y | ||
CONFIG_STD_CPP20=y | ||
|
||
CONFIG_POSIX_API=y | ||
CONFIG_THREAD_STACK_INFO=y | ||
CONFIG_DYNAMIC_THREAD=y | ||
CONFIG_DYNAMIC_THREAD_STACK_SIZE=4096 | ||
CONFIG_DYNAMIC_THREAD_POOL_SIZE=6 | ||
|
||
CONFIG_ZTEST_STACK_SIZE=4096 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "_main.hpp" | ||
|
||
#include <zephyr/ztest.h> | ||
|
||
bool lock_succeeded; | ||
time_point t0, t1, t2, t3; | ||
|
||
time_point now() | ||
{ | ||
return std::chrono::steady_clock::now(); | ||
} | ||
|
||
void time_init() | ||
{ | ||
t0 = now(); | ||
t1 = t0 + 2 * dt; | ||
t2 = t0 + 3 * dt; | ||
t3 = t0 + 4 * dt; | ||
} | ||
|
||
static void before(void *arg) | ||
{ | ||
ARG_UNUSED(arg); | ||
|
||
lock_succeeded = false; | ||
time_init(); | ||
} | ||
|
||
ZTEST_SUITE(std_mutex, nullptr, nullptr, before, nullptr, nullptr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <chrono> | ||
#include <mutex> | ||
#include <thread> | ||
|
||
using namespace std::chrono_literals; | ||
|
||
using time_point = std::chrono::time_point<std::chrono::steady_clock>; | ||
|
||
time_point now(); | ||
void time_init(); | ||
|
||
extern bool lock_succeeded; | ||
extern time_point t0, t1, t2, t3; | ||
constexpr auto dt = 100ms; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "_main.hpp" | ||
|
||
#include <zephyr/ztest.h> | ||
|
||
static std::mutex mu; | ||
|
||
ZTEST(std_mutex, test_plain) | ||
{ | ||
mu.lock(); | ||
|
||
std::thread th([] { | ||
zassert_false(mu.try_lock()); | ||
mu.lock(); | ||
lock_succeeded = true; | ||
mu.unlock(); | ||
}); | ||
std::this_thread::sleep_until(t1); | ||
zassert_false(lock_succeeded); | ||
mu.unlock(); | ||
|
||
th.join(); | ||
|
||
zassert_true(lock_succeeded); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "_main.hpp" | ||
|
||
#include <zephyr/ztest.h> | ||
|
||
static std::recursive_mutex mu; | ||
|
||
ZTEST(std_mutex, test_recursive) | ||
{ | ||
mu.lock(); | ||
zassert_true(mu.try_lock()); | ||
|
||
std::thread th([] { | ||
zassert_false(mu.try_lock()); | ||
std::this_thread::sleep_until(t2); | ||
mu.lock(); | ||
mu.lock(); | ||
lock_succeeded = true; | ||
mu.unlock(); | ||
mu.unlock(); | ||
}); | ||
std::this_thread::sleep_until(t1); | ||
zassert_false(lock_succeeded); | ||
mu.unlock(); | ||
mu.unlock(); | ||
|
||
th.join(); | ||
|
||
zassert_true(lock_succeeded); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
#include "_main.hpp" | ||
|
||
#include <zephyr/ztest.h> | ||
|
||
static std::recursive_timed_mutex mu; | ||
|
||
ZTEST(std_mutex, test_resursive_timed) | ||
{ | ||
mu.lock(); | ||
zassert_true(mu.try_lock()); | ||
|
||
std::thread th([] { | ||
zassert_false(mu.try_lock()); | ||
zassert_false(mu.try_lock_for(2 * dt)); | ||
mu.lock(); | ||
mu.lock(); | ||
lock_succeeded = true; | ||
mu.unlock(); | ||
mu.unlock(); | ||
}); | ||
std::this_thread::sleep_until(t1); | ||
zassert_false(lock_succeeded); | ||
mu.unlock(); | ||
mu.unlock(); | ||
|
||
zassert_true(mu.try_lock_until(t3)); | ||
mu.unlock(); | ||
|
||
th.join(); | ||
|
||
zassert_true(lock_succeeded); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "_main.hpp" | ||
|
||
#include <zephyr/ztest.h> | ||
|
||
static std::timed_mutex mu; | ||
|
||
ZTEST(std_mutex, test_timed) | ||
{ | ||
mu.lock(); | ||
|
||
std::thread th([] { | ||
zassert_false(mu.try_lock()); | ||
zassert_false(mu.try_lock_for(2 * dt)); | ||
mu.lock(); | ||
lock_succeeded = true; | ||
mu.unlock(); | ||
}); | ||
std::this_thread::sleep_until(t1); | ||
zassert_false(lock_succeeded); | ||
mu.unlock(); | ||
|
||
zassert_true(mu.try_lock_until(t3)); | ||
mu.unlock(); | ||
|
||
th.join(); | ||
|
||
zassert_true(lock_succeeded); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
common: | ||
tags: cpp | ||
filter: CONFIG_FULL_LIBCPP_SUPPORTED | ||
integration_platforms: | ||
- qemu_cortex_a53 | ||
- mps2/an385 | ||
- qemu_riscv32 | ||
- qemu_riscv64 | ||
- qemu_x86 | ||
- qemu_x86_64 | ||
# llvm currently excluded due to 'inttypes.h' file not found | ||
toolchain_exclude: | ||
- llvm | ||
tests: | ||
cpp.mutex.newlib.except: | ||
tags: newlib | ||
filter: CONFIG_NEWLIB_LIBC_SUPPORTED | ||
extra_configs: | ||
- CONFIG_NEWLIB_LIBC=y | ||
- CONFIG_CPP_EXCEPTIONS=y | ||
cpp.mutex.newlib.noexcept: | ||
tags: newlib | ||
filter: CONFIG_NEWLIB_LIBC_SUPPORTED | ||
extra_configs: | ||
- CONFIG_NEWLIB_LIBC=y |