Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
main.c File Reference

Test kernel mutex APIs. More...

#include <zephyr/tc_util.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
#include <zephyr/sys/mutex.h>

Macros

#define STACKSIZE   (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
 
#define ZTEST_USER_OR_NOT   ZTEST_USER
 
#define PARTICIPANT_THREAD_OPTIONS   (K_USER | K_INHERIT_PERMS)
 
#define DEFINE_PARTICIPANT_THREAD(id)
 
#define CREATE_PARTICIPANT_THREAD(id, pri)
 
#define START_PARTICIPANT_THREAD(id)   k_thread_start(&(thread_##id##_thread_data));
 
#define JOIN_PARTICIPANT_THREAD(id)   k_thread_join(&(thread_##id##_thread_data), K_FOREVER);
 

Functions

ZTEST_BMEM SYS_MUTEX_DEFINE (private_mutex)
 
ZTEST_BMEM SYS_MUTEX_DEFINE (mutex_1)
 
ZTEST_BMEM SYS_MUTEX_DEFINE (mutex_2)
 
ZTEST_BMEM SYS_MUTEX_DEFINE (mutex_3)
 
ZTEST_BMEM SYS_MUTEX_DEFINE (mutex_4)
 
static SYS_MUTEX_DEFINE (no_access_mutex)
 
static ZTEST_BMEM SYS_MUTEX_DEFINE (not_my_mutex)
 
static ZTEST_BMEM SYS_MUTEX_DEFINE (bad_count_mutex)
 
void thread_05 (void *p1, void *p2, void *p3)
 thread_05 -
 
void thread_06 (void *p1, void *p2, void *p3)
 thread_06 -
 
void thread_07 (void *p1, void *p2, void *p3)
 thread_07 -
 
void thread_08 (void *p1, void *p2, void *p3)
 thread_08 -
 
void thread_09 (void *p1, void *p2, void *p3)
 thread_09 -
 
void thread_11 (void *p1, void *p2, void *p3)
 thread_11 -
 
 K_THREAD_STACK_DEFINE (thread_12_stack_area,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
void thread_12 (void *p1, void *p2, void *p3)
 thread_12 - thread that participates in recursive locking tests
 
 K_THREAD_STACK_DEFINE (thread_05_stack_area,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
 K_THREAD_STACK_DEFINE (thread_06_stack_area,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
 K_THREAD_STACK_DEFINE (thread_07_stack_area,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
 K_THREAD_STACK_DEFINE (thread_08_stack_area,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
 K_THREAD_STACK_DEFINE (thread_09_stack_area,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
 K_THREAD_STACK_DEFINE (thread_11_stack_area,(512+CONFIG_TEST_EXTRA_STACK_SIZE))
 
void create_participant_threads (void)
 
void start_participant_threads (void)
 
void join_participant_threads (void)
 
 ZTEST_USER (mutex_complex, test_mutex)
 Main thread to test thread_mutex_xxx interfaces.
 
 ZTEST (mutex_complex, test_supervisor_access)
 
 ZTEST_USER (mutex_complex, test_user_access)
 
static void * sys_mutex_tests_setup (void)
 
 ZTEST_SUITE (mutex_complex, NULL, sys_mutex_tests_setup, NULL, NULL, NULL)
 

Variables

static ZTEST_DMEM int tc_rc = TC_PASS
 
struct k_thread thread_12_thread_data
 
struct k_thread thread_05_thread_data
 
k_tid_t thread_05_tid
 
struct k_thread thread_06_thread_data
 
k_tid_t thread_06_tid
 
struct k_thread thread_07_thread_data
 
k_tid_t thread_07_tid
 
struct k_thread thread_08_thread_data
 
k_tid_t thread_08_tid
 
struct k_thread thread_09_thread_data
 
k_tid_t thread_09_tid
 
struct k_thread thread_11_thread_data
 
k_tid_t thread_11_tid
 

Detailed Description

Test kernel mutex APIs.

This module demonstrates the kernel's priority inheritance algorithm. A thread that owns a mutex is promoted to the priority level of the highest-priority thread attempting to lock the mutex.

In addition, recursive locking capabilities and the use of a private mutex are also tested.

This module tests the following mutex routines:

sys_mutex_lock sys_mutex_unlock

Timeline for priority inheritance testing:

  • 0.0 sec: thread_05, thread_06, thread_07, thread_08, thread_09, sleep : main thread takes mutex_1 then sleeps
  • 0.0 sec: thread_11 sleeps
  • 0.5 sec: thread_09 wakes and waits on mutex_1
  • 1.0 sec: main thread (@ priority 9) takes mutex_2 then sleeps
  • 1.5 sec: thread_08 wakes and waits on mutex_2
  • 2.0 sec: main thread (@ priority 8) takes mutex_3 then sleeps
  • 2.5 sec: thread_07 wakes and waits on mutex_3
  • 3.0 sec: main thread (@ priority 7) takes mutex_4 then sleeps
  • 3.5 sec: thread_05 wakes and waits on mutex_4
  • 3.5 sec: thread_11 wakes and waits on mutex_3
  • 3.75 sec: thread_06 wakes and waits on mutex_4
  • 4.0 sec: main thread wakes (@ priority 5) then sleeps
  • 4.5 sec: thread_05 times out
  • 5.0 sec: main thread wakes (@ priority 6) then gives mutex_4 : main thread (@ priority 7) sleeps
  • 5.5 sec: thread_07 times out on mutex_3
  • 6.0 sec: main thread (@ priority 8) gives mutex_3 : main thread (@ priority 8) gives mutex_2 : main thread (@ priority 9) gives mutex_1 : main thread (@ priority 10) sleeps

Macro Definition Documentation

◆ CREATE_PARTICIPANT_THREAD

#define CREATE_PARTICIPANT_THREAD (   id,
  pri 
)
Value:
k_thread_create(&thread_##id##_thread_data, thread_##id##_stack_area, \
K_THREAD_STACK_SIZEOF(thread_##id##_stack_area), \
thread_##id, \
NULL, NULL, NULL, \
#define K_FOREVER
Generate infinite timeout delay.
Definition kernel.h:1363
k_tid_t k_thread_create(struct k_thread *new_thread, k_thread_stack_t *stack, size_t stack_size, k_thread_entry_t entry, void *p1, void *p2, void *p3, int prio, uint32_t options, k_timeout_t delay)
Create a thread.
#define K_THREAD_STACK_SIZEOF(sym)
Return the size in bytes of a stack memory region.
Definition thread_stack.h:437
#define PARTICIPANT_THREAD_OPTIONS
Definition main.c:78

◆ DEFINE_PARTICIPANT_THREAD

#define DEFINE_PARTICIPANT_THREAD (   id)
Value:
K_THREAD_STACK_DEFINE(thread_##id##_stack_area, STACKSIZE); \
struct k_thread thread_##id##_thread_data; \
k_tid_t thread_##id##_tid;
#define K_THREAD_STACK_DEFINE(sym, size)
Define a toplevel thread stack memory region.
Definition thread_stack.h:517
#define STACKSIZE
Definition main.c:53
Thread Structure.
Definition thread.h:259

◆ JOIN_PARTICIPANT_THREAD

#define JOIN_PARTICIPANT_THREAD (   id)    k_thread_join(&(thread_##id##_thread_data), K_FOREVER);

◆ PARTICIPANT_THREAD_OPTIONS

#define PARTICIPANT_THREAD_OPTIONS   (K_USER | K_INHERIT_PERMS)

◆ STACKSIZE

#define STACKSIZE   (512 + CONFIG_TEST_EXTRA_STACK_SIZE)

◆ START_PARTICIPANT_THREAD

#define START_PARTICIPANT_THREAD (   id)    k_thread_start(&(thread_##id##_thread_data));

◆ ZTEST_USER_OR_NOT

#define ZTEST_USER_OR_NOT   ZTEST_USER

Function Documentation

◆ create_participant_threads()

void create_participant_threads ( void  )

◆ join_participant_threads()

void join_participant_threads ( void  )

◆ K_THREAD_STACK_DEFINE() [1/7]

K_THREAD_STACK_DEFINE ( thread_05_stack_area  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)

◆ K_THREAD_STACK_DEFINE() [2/7]

K_THREAD_STACK_DEFINE ( thread_06_stack_area  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)

◆ K_THREAD_STACK_DEFINE() [3/7]

K_THREAD_STACK_DEFINE ( thread_07_stack_area  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)

◆ K_THREAD_STACK_DEFINE() [4/7]

K_THREAD_STACK_DEFINE ( thread_08_stack_area  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)

◆ K_THREAD_STACK_DEFINE() [5/7]

K_THREAD_STACK_DEFINE ( thread_09_stack_area  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)

◆ K_THREAD_STACK_DEFINE() [6/7]

K_THREAD_STACK_DEFINE ( thread_11_stack_area  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)

◆ K_THREAD_STACK_DEFINE() [7/7]

K_THREAD_STACK_DEFINE ( thread_12_stack_area  ,
(512+CONFIG_TEST_EXTRA_STACK_SIZE)   
)

◆ start_participant_threads()

void start_participant_threads ( void  )

◆ SYS_MUTEX_DEFINE() [1/8]

static ZTEST_BMEM SYS_MUTEX_DEFINE ( bad_count_mutex  )
static

◆ SYS_MUTEX_DEFINE() [2/8]

ZTEST_BMEM SYS_MUTEX_DEFINE ( mutex_1  )

◆ SYS_MUTEX_DEFINE() [3/8]

ZTEST_BMEM SYS_MUTEX_DEFINE ( mutex_2  )

◆ SYS_MUTEX_DEFINE() [4/8]

ZTEST_BMEM SYS_MUTEX_DEFINE ( mutex_3  )

◆ SYS_MUTEX_DEFINE() [5/8]

ZTEST_BMEM SYS_MUTEX_DEFINE ( mutex_4  )

◆ SYS_MUTEX_DEFINE() [6/8]

static SYS_MUTEX_DEFINE ( no_access_mutex  )
static

◆ SYS_MUTEX_DEFINE() [7/8]

static ZTEST_BMEM SYS_MUTEX_DEFINE ( not_my_mutex  )
static

◆ SYS_MUTEX_DEFINE() [8/8]

ZTEST_BMEM SYS_MUTEX_DEFINE ( private_mutex  )

◆ sys_mutex_tests_setup()

static void * sys_mutex_tests_setup ( void  )
static

◆ thread_05()

void thread_05 ( void *  p1,
void *  p2,
void *  p3 
)

thread_05 -

◆ thread_06()

void thread_06 ( void *  p1,
void *  p2,
void *  p3 
)

thread_06 -

◆ thread_07()

void thread_07 ( void *  p1,
void *  p2,
void *  p3 
)

thread_07 -

◆ thread_08()

void thread_08 ( void *  p1,
void *  p2,
void *  p3 
)

thread_08 -

◆ thread_09()

void thread_09 ( void *  p1,
void *  p2,
void *  p3 
)

thread_09 -

◆ thread_11()

void thread_11 ( void *  p1,
void *  p2,
void *  p3 
)

thread_11 -

◆ thread_12()

void thread_12 ( void *  p1,
void *  p2,
void *  p3 
)
extern

thread_12 - thread that participates in recursive locking tests

◆ ZTEST()

ZTEST ( mutex_complex  ,
test_supervisor_access   
)

◆ ZTEST_SUITE()

ZTEST_SUITE ( mutex_complex  ,
NULL  ,
sys_mutex_tests_setup  ,
NULL  ,
NULL  ,
NULL   
)

◆ ZTEST_USER() [1/2]

ZTEST_USER ( mutex_complex  ,
test_mutex   
)

Main thread to test thread_mutex_xxx interfaces.

This thread will lock on mutex_1, mutex_2, mutex_3 and mutex_4. It later recursively locks private_mutex, releases it, then re-locks it.

◆ ZTEST_USER() [2/2]

ZTEST_USER ( mutex_complex  ,
test_user_access   
)

Variable Documentation

◆ tc_rc

ZTEST_DMEM int tc_rc = TC_PASS
static

◆ thread_05_thread_data

struct k_thread thread_05_thread_data

◆ thread_05_tid

k_tid_t thread_05_tid

◆ thread_06_thread_data

struct k_thread thread_06_thread_data

◆ thread_06_tid

k_tid_t thread_06_tid

◆ thread_07_thread_data

struct k_thread thread_07_thread_data

◆ thread_07_tid

k_tid_t thread_07_tid

◆ thread_08_thread_data

struct k_thread thread_08_thread_data

◆ thread_08_tid

k_tid_t thread_08_tid

◆ thread_09_thread_data

struct k_thread thread_09_thread_data

◆ thread_09_tid

k_tid_t thread_09_tid

◆ thread_11_thread_data

struct k_thread thread_11_thread_data

◆ thread_11_tid

k_tid_t thread_11_tid

◆ thread_12_thread_data

struct k_thread thread_12_thread_data