Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | /*
* Copyright (c) 2018 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/zephyr.h>
#include <ztest.h>
#include <zephyr/spinlock.h>
BUILD_ASSERT(CONFIG_MP_NUM_CPUS > 1);
static struct k_spinlock lock;
static struct k_spinlock mylock;
static k_spinlock_key_t key;
static ZTEST_DMEM volatile bool valid_assert;
static inline void set_assert_valid(bool valid)
{
valid_assert = valid;
}
static void action_after_assert_fail(void)
{
k_spin_unlock(&lock, key);
ztest_test_pass();
}
#ifdef CONFIG_ASSERT_NO_FILE_INFO
void assert_post_action(void)
#else
void assert_post_action(const char *file, unsigned int line)
#endif
{
#ifndef CONFIG_ASSERT_NO_FILE_INFO
ARG_UNUSED(file);
ARG_UNUSED(line);
#endif
printk("Caught an assert.\n");
if (valid_assert) {
valid_assert = false; /* reset back to normal */
printk("Assert error expected as part of test case.\n");
/* do some action after fatal error happened */
action_after_assert_fail();
} else {
printk("Assert failed was unexpected, aborting...\n");
#ifdef CONFIG_USERSPACE
/* User threads aren't allowed to induce kernel panics; generate
* an oops instead.
*/
if (k_is_user_context()) {
k_oops();
}
#endif
k_panic();
}
}
/**
* @brief Test spinlock cannot be recursive
*
* @details Validate using spinlock recursive will trigger assertion.
*
* @ingroup kernel_spinlock_tests
*
* @see k_spin_lock()
*/
void test_spinlock_no_recursive(void)
{
k_spinlock_key_t re;
key = k_spin_lock(&lock);
set_assert_valid(true);
re = k_spin_lock(&lock);
ztest_test_fail();
}
/**
* @brief Test unlocking incorrect spinlock
*
* @details Validate unlocking incorrect spinlock will trigger assertion.
*
* @ingroup kernel_spinlock_tests
*
* @see k_spin_unlock()
*/
void test_spinlock_unlock_error(void)
{
key = k_spin_lock(&lock);
set_assert_valid(true);
k_spin_unlock(&mylock, key);
ztest_test_fail();
}
/**
* @brief Test unlocking incorrect spinlock
*
* @details Validate unlocking incorrect spinlock will trigger assertion.
*
* @ingroup kernel_spinlock_tests
*
* @see k_spin_release()
*/
void test_spinlock_release_error(void)
{
key = k_spin_lock(&lock);
set_assert_valid(true);
k_spin_release(&mylock);
ztest_test_fail();
}
|