Skip to content

Commit 984a65b

Browse files
committed
tests: lib: cpp: add tests for std::mutex and friends
Add tests for ISO C++ std::mutex. Signed-off-by: Christopher Friedt <[email protected]>
1 parent 1b3ba57 commit 984a65b

File tree

9 files changed

+241
-0
lines changed

9 files changed

+241
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(mutex)
6+
7+
FILE(GLOB_RECURSE app_sources src/*.cpp)
8+
target_sources(app PRIVATE ${app_sources})

tests/lib/cpp/std/mutex/prj.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CONFIG_ZTEST=y
2+
3+
CONFIG_CPP=y
4+
CONFIG_REQUIRES_FULL_LIBCPP=y
5+
CONFIG_CPP_EXCEPTIONS=y
6+
CONFIG_STD_CPP20=y
7+
8+
CONFIG_POSIX_API=y
9+
CONFIG_THREAD_STACK_INFO=y
10+
CONFIG_DYNAMIC_THREAD=y
11+
CONFIG_DYNAMIC_THREAD_STACK_SIZE=4096
12+
CONFIG_DYNAMIC_THREAD_POOL_SIZE=6
13+
14+
CONFIG_ZTEST_STACK_SIZE=4096

tests/lib/cpp/std/mutex/src/_main.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "_main.hpp"
8+
9+
#include <zephyr/ztest.h>
10+
11+
bool lock_succeeded;
12+
time_point t0, t1, t2, t3;
13+
14+
time_point now()
15+
{
16+
return std::chrono::steady_clock::now();
17+
}
18+
19+
void time_init()
20+
{
21+
t0 = now();
22+
t1 = t0 + 2 * dt;
23+
t2 = t0 + 3 * dt;
24+
t3 = t0 + 4 * dt;
25+
}
26+
27+
static void before(void *arg)
28+
{
29+
ARG_UNUSED(arg);
30+
31+
lock_succeeded = false;
32+
time_init();
33+
}
34+
35+
ZTEST_SUITE(std_mutex, nullptr, nullptr, before, nullptr, nullptr);

tests/lib/cpp/std/mutex/src/_main.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <chrono>
8+
#include <mutex>
9+
#include <thread>
10+
11+
using namespace std::chrono_literals;
12+
13+
using time_point = std::chrono::time_point<std::chrono::steady_clock>;
14+
15+
time_point now();
16+
void time_init();
17+
18+
extern bool lock_succeeded;
19+
extern time_point t0, t1, t2, t3;
20+
constexpr auto dt = 100ms;

tests/lib/cpp/std/mutex/src/plain.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "_main.hpp"
8+
9+
#include <zephyr/ztest.h>
10+
11+
static std::mutex mu;
12+
13+
ZTEST(std_mutex, test_plain)
14+
{
15+
mu.lock();
16+
17+
std::thread th([] {
18+
zassert_false(mu.try_lock());
19+
mu.lock();
20+
lock_succeeded = true;
21+
mu.unlock();
22+
});
23+
std::this_thread::sleep_until(t1);
24+
zassert_false(lock_succeeded);
25+
mu.unlock();
26+
27+
th.join();
28+
29+
zassert_true(lock_succeeded);
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "_main.hpp"
8+
9+
#include <zephyr/ztest.h>
10+
11+
static std::recursive_mutex mu;
12+
13+
ZTEST(std_mutex, test_recursive)
14+
{
15+
mu.lock();
16+
zassert_true(mu.try_lock());
17+
18+
std::thread th([] {
19+
zassert_false(mu.try_lock());
20+
std::this_thread::sleep_until(t2);
21+
mu.lock();
22+
mu.lock();
23+
lock_succeeded = true;
24+
mu.unlock();
25+
mu.unlock();
26+
});
27+
std::this_thread::sleep_until(t1);
28+
zassert_false(lock_succeeded);
29+
mu.unlock();
30+
mu.unlock();
31+
32+
th.join();
33+
34+
zassert_true(lock_succeeded);
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <iostream>
8+
9+
#include "_main.hpp"
10+
11+
#include <zephyr/ztest.h>
12+
13+
static std::recursive_timed_mutex mu;
14+
15+
ZTEST(std_mutex, test_resursive_timed)
16+
{
17+
mu.lock();
18+
zassert_true(mu.try_lock());
19+
20+
std::thread th([] {
21+
zassert_false(mu.try_lock());
22+
zassert_false(mu.try_lock_for(2 * dt));
23+
mu.lock();
24+
mu.lock();
25+
lock_succeeded = true;
26+
mu.unlock();
27+
mu.unlock();
28+
});
29+
std::this_thread::sleep_until(t1);
30+
zassert_false(lock_succeeded);
31+
mu.unlock();
32+
mu.unlock();
33+
34+
zassert_true(mu.try_lock_until(t3));
35+
mu.unlock();
36+
37+
th.join();
38+
39+
zassert_true(lock_succeeded);
40+
}

tests/lib/cpp/std/mutex/src/timed.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2020, Friedt Professional Engineering Services, Inc
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "_main.hpp"
8+
9+
#include <zephyr/ztest.h>
10+
11+
static std::timed_mutex mu;
12+
13+
ZTEST(std_mutex, test_timed)
14+
{
15+
mu.lock();
16+
17+
std::thread th([] {
18+
zassert_false(mu.try_lock());
19+
zassert_false(mu.try_lock_for(2 * dt));
20+
mu.lock();
21+
lock_succeeded = true;
22+
mu.unlock();
23+
});
24+
std::this_thread::sleep_until(t1);
25+
zassert_false(lock_succeeded);
26+
mu.unlock();
27+
28+
zassert_true(mu.try_lock_until(t3));
29+
mu.unlock();
30+
31+
th.join();
32+
33+
zassert_true(lock_succeeded);
34+
}

tests/lib/cpp/std/mutex/testcase.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
common:
2+
tags: cpp
3+
filter: CONFIG_FULL_LIBCPP_SUPPORTED
4+
integration_platforms:
5+
- qemu_cortex_a53
6+
- mps2/an385
7+
- qemu_riscv32
8+
- qemu_riscv64
9+
- qemu_x86
10+
- qemu_x86_64
11+
# llvm currently excluded due to 'inttypes.h' file not found
12+
toolchain_exclude:
13+
- llvm
14+
tests:
15+
cpp.mutex.newlib.except:
16+
tags: newlib
17+
filter: CONFIG_NEWLIB_LIBC_SUPPORTED
18+
extra_configs:
19+
- CONFIG_NEWLIB_LIBC=y
20+
- CONFIG_CPP_EXCEPTIONS=y
21+
cpp.mutex.newlib.noexcept:
22+
tags: newlib
23+
filter: CONFIG_NEWLIB_LIBC_SUPPORTED
24+
extra_configs:
25+
- CONFIG_NEWLIB_LIBC=y

0 commit comments

Comments
 (0)