Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
main.c File Reference

Test kernel mutex APIs. More...

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

Macros

#define STACKSIZE   (512 + CONFIG_TEST_EXTRA_STACKSIZE)
 

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 test_mutex_multithread_competition (void)
 Test multi-threads to take mutex. More...
 
void thread_05 (void)
 
void thread_06 (void)
 
void thread_07 (void)
 
void thread_08 (void)
 
void thread_09 (void)
 
void thread_11 (void)
 
 K_THREAD_STACK_DEFINE (thread_12_stack_area,(512+CONFIG_TEST_EXTRA_STACKSIZE))
 
void thread_12 (void)
 
void test_mutex (void)
 Main thread to test thread_mutex_xxx interfaces. More...
 
void test_supervisor_access (void)
 
void test_user_access (void)
 
 K_THREAD_DEFINE (THREAD_05,(512+CONFIG_TEST_EXTRA_STACKSIZE), thread_05, NULL, NULL, NULL, 5, K_USER, 0)
 
 K_THREAD_DEFINE (THREAD_06,(512+CONFIG_TEST_EXTRA_STACKSIZE), thread_06, NULL, NULL, NULL, 6, K_USER, 0)
 
 K_THREAD_DEFINE (THREAD_07,(512+CONFIG_TEST_EXTRA_STACKSIZE), thread_07, NULL, NULL, NULL, 7, K_USER, 0)
 
 K_THREAD_DEFINE (THREAD_08,(512+CONFIG_TEST_EXTRA_STACKSIZE), thread_08, NULL, NULL, NULL, 8, K_USER, 0)
 
 K_THREAD_DEFINE (THREAD_09,(512+CONFIG_TEST_EXTRA_STACKSIZE), thread_09, NULL, NULL, NULL, 9, K_USER, 0)
 
 K_THREAD_DEFINE (THREAD_11,(512+CONFIG_TEST_EXTRA_STACKSIZE), thread_11, NULL, NULL, NULL, 11, K_USER, 0)
 
void test_main (void)
 

Variables

static ZTEST_DMEM int tc_rc = TC_PASS
 
struct k_thread thread_12_thread_data
 

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

◆ STACKSIZE

#define STACKSIZE   (512 + CONFIG_TEST_EXTRA_STACKSIZE)

Function Documentation

◆ K_THREAD_DEFINE() [1/6]

K_THREAD_DEFINE ( THREAD_05  ,
(512+CONFIG_TEST_EXTRA_STACKSIZE)  ,
thread_05  ,
NULL  ,
NULL  ,
NULL  ,
,
K_USER  ,
 
)

◆ K_THREAD_DEFINE() [2/6]

K_THREAD_DEFINE ( THREAD_06  ,
(512+CONFIG_TEST_EXTRA_STACKSIZE)  ,
thread_06  ,
NULL  ,
NULL  ,
NULL  ,
,
K_USER  ,
 
)

◆ K_THREAD_DEFINE() [3/6]

K_THREAD_DEFINE ( THREAD_07  ,
(512+CONFIG_TEST_EXTRA_STACKSIZE)  ,
thread_07  ,
NULL  ,
NULL  ,
NULL  ,
,
K_USER  ,
 
)

◆ K_THREAD_DEFINE() [4/6]

K_THREAD_DEFINE ( THREAD_08  ,
(512+CONFIG_TEST_EXTRA_STACKSIZE)  ,
thread_08  ,
NULL  ,
NULL  ,
NULL  ,
,
K_USER  ,
 
)

◆ K_THREAD_DEFINE() [5/6]

K_THREAD_DEFINE ( THREAD_09  ,
(512+CONFIG_TEST_EXTRA_STACKSIZE)  ,
thread_09  ,
NULL  ,
NULL  ,
NULL  ,
,
K_USER  ,
 
)

◆ K_THREAD_DEFINE() [6/6]

K_THREAD_DEFINE ( THREAD_11  ,
(512+CONFIG_TEST_EXTRA_STACKSIZE)  ,
thread_11  ,
NULL  ,
NULL  ,
NULL  ,
11  ,
K_USER  ,
 
)

◆ K_THREAD_STACK_DEFINE()

K_THREAD_STACK_DEFINE ( thread_12_stack_area  ,
(512+CONFIG_TEST_EXTRA_STACKSIZE)   
)

◆ 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  )

◆ test_main()

void test_main ( void  )

test case main entry

◆ test_mutex()

void test_mutex ( void  )

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.

Returns
N/A

◆ test_mutex_multithread_competition()

void test_mutex_multithread_competition ( void  )

Test multi-threads to take mutex.

Define three threads, and set a higher priority for two of them, and set a lower priority for the last one. Then Add a delay between creating the two high priority threads. Test point:

  1. Any number of threads may wait on a mutex locked by others simultaneously.
  2. When the mutex is released, it is took by the highest priority thread that has waited longest.

◆ test_supervisor_access()

void test_supervisor_access ( void  )

◆ test_user_access()

void test_user_access ( void  )

◆ thread_05()

void thread_05 ( void  )

thread_05 -

Returns
N/A

◆ thread_06()

void thread_06 ( void  )

thread_06 -

Returns
N/A

◆ thread_07()

void thread_07 ( void  )

thread_07 -

Returns
N/A

◆ thread_08()

void thread_08 ( void  )

thread_08 -

Returns
N/A

◆ thread_09()

void thread_09 ( void  )

thread_09 -

Returns
N/A

◆ thread_11()

void thread_11 ( void  )

thread_11 -

Returns
N/A

◆ thread_12()

void thread_12 ( void  )

thread_12 - thread that participates in recursive locking tests

Returns
N/A

Variable Documentation

◆ tc_rc

ZTEST_DMEM int tc_rc = TC_PASS
static

◆ thread_12_thread_data

struct k_thread thread_12_thread_data