Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
thread_competition.c File Reference
#include <zephyr.h>
#include <ztest.h>
#include <sys/mutex.h>

Macros

#define HIGH_T1   0xaaa
 
#define HIGH_T2   0xbbb
 
#define LOW_PRO   0xccc
 
#define STACKSIZE   (512 + CONFIG_TEST_EXTRA_STACKSIZE)
 

Functions

static K_THREAD_STACK_DEFINE (thread_low_stack,(512+CONFIG_TEST_EXTRA_STACKSIZE))
 
static K_THREAD_STACK_DEFINE (thread_high_stack1,(512+CONFIG_TEST_EXTRA_STACKSIZE))
 
static K_THREAD_STACK_DEFINE (thread_high_stack2,(512+CONFIG_TEST_EXTRA_STACKSIZE))
 
 SYS_MUTEX_DEFINE (mutex)
 
static void low_prio_wait_for_mutex (void *p1, void *p2, void *p3)
 
static void high_prio_t1_wait_for_mutex (void *p1, void *p2, void *p3)
 
static void high_prio_t2_wait_for_mutex (void *p1, void *p2, void *p3)
 
void test_mutex_multithread_competition (void)
 Test multi-threads to take mutex. More...
 

Variables

static struct k_thread thread_low_data
 
static struct k_thread thread_high_data1
 
static struct k_thread thread_high_data2
 
static uint32_t flag [3]
 
static uint32_t index
 

Macro Definition Documentation

◆ HIGH_T1

#define HIGH_T1   0xaaa

◆ HIGH_T2

#define HIGH_T2   0xbbb

◆ LOW_PRO

#define LOW_PRO   0xccc

◆ STACKSIZE

#define STACKSIZE   (512 + CONFIG_TEST_EXTRA_STACKSIZE)

Function Documentation

◆ high_prio_t1_wait_for_mutex()

static void high_prio_t1_wait_for_mutex ( void p1,
void p2,
void p3 
)
static

◆ high_prio_t2_wait_for_mutex()

static void high_prio_t2_wait_for_mutex ( void p1,
void p2,
void p3 
)
static

◆ K_THREAD_STACK_DEFINE() [1/3]

static K_THREAD_STACK_DEFINE ( thread_high_stack1  ,
(512+CONFIG_TEST_EXTRA_STACKSIZE)   
)
static

◆ K_THREAD_STACK_DEFINE() [2/3]

static K_THREAD_STACK_DEFINE ( thread_high_stack2  ,
(512+CONFIG_TEST_EXTRA_STACKSIZE)   
)
static

◆ K_THREAD_STACK_DEFINE() [3/3]

static K_THREAD_STACK_DEFINE ( thread_low_stack  ,
(512+CONFIG_TEST_EXTRA_STACKSIZE)   
)
static

◆ low_prio_wait_for_mutex()

static void low_prio_wait_for_mutex ( void p1,
void p2,
void p3 
)
static

◆ SYS_MUTEX_DEFINE()

SYS_MUTEX_DEFINE ( mutex  )

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

Variable Documentation

◆ flag

uint32_t flag[3]
static

◆ index

uint32_t index
static

◆ thread_high_data1

struct k_thread thread_high_data1
static

◆ thread_high_data2

struct k_thread thread_high_data2
static

◆ thread_low_data

struct k_thread thread_low_data
static