Skip to content

Commit

Permalink
tests: lib: cpp: add tests for std::mutex and friends
Browse files Browse the repository at this point in the history
Add tests for ISO C++ std::mutex.

Signed-off-by: Christopher Friedt <[email protected]>
  • Loading branch information
cfriedt authored and Chris Friedt committed Aug 11, 2024
1 parent 1b3ba57 commit 984a65b
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/lib/cpp/std/mutex/CMakeLists.txt
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})
14 changes: 14 additions & 0 deletions tests/lib/cpp/std/mutex/prj.conf
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
35 changes: 35 additions & 0 deletions tests/lib/cpp/std/mutex/src/_main.cpp
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);
20 changes: 20 additions & 0 deletions tests/lib/cpp/std/mutex/src/_main.hpp
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;
30 changes: 30 additions & 0 deletions tests/lib/cpp/std/mutex/src/plain.cpp
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);
}
35 changes: 35 additions & 0 deletions tests/lib/cpp/std/mutex/src/recursive.cpp
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);
}
40 changes: 40 additions & 0 deletions tests/lib/cpp/std/mutex/src/recursive_timed.cpp
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);
}
34 changes: 34 additions & 0 deletions tests/lib/cpp/std/mutex/src/timed.cpp
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);
}
25 changes: 25 additions & 0 deletions tests/lib/cpp/std/mutex/testcase.yaml
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

0 comments on commit 984a65b

Please sign in to comment.