Skip to content

Commit 1bb5c34

Browse files
akasonakartben
authored andcommitted
tests: kernel/sched: 32-bit tick wraparound during k_sleep() execution
This test reproduce the issue: #79863 In this test, instead of waiting the 32-bit tick wraparound for several days, patching the kernel internal tick with the test API. Signed-off-by: Akaiwa Wataru <[email protected]>
1 parent feefb7d commit 1bb5c34

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(sleep)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2024 Akaiwa Wataru <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <zephyr/kernel.h>
7+
#include <zephyr/ztest.h>
8+
9+
static k_tid_t thread_id;
10+
11+
static void alarm_callback(struct k_timer *timer)
12+
{
13+
k_wakeup(thread_id);
14+
}
15+
16+
K_TIMER_DEFINE(alarm, alarm_callback, NULL);
17+
18+
/**
19+
* @brief Test 32-bit tick wraparound during k_sleep() execution
20+
*/
21+
ZTEST(wraparound, test_tick_wraparound_in_sleep)
22+
{
23+
static const uint32_t start_ticks = 0xffffff00; /* It wraps around after 256 ticks! */
24+
static const uint32_t timeout_ticks = 300; /* 3 seconds @ 100Hz tick */
25+
static const uint32_t wakeup_ticks = 10; /* 100 ms @ 100Hz tick */
26+
27+
sys_clock_tick_set(start_ticks);
28+
29+
/* Wake up myself by alarm */
30+
thread_id = k_current_get();
31+
k_timer_start(&alarm, K_TICKS(wakeup_ticks), K_FOREVER);
32+
33+
/* Waiting alarm's k_wakeup() call */
34+
int32_t left_ms = k_sleep(K_TICKS(timeout_ticks));
35+
36+
zassert(left_ms > 0, "k_sleep() timed out");
37+
38+
k_timer_stop(&alarm);
39+
}
40+
41+
ZTEST_SUITE(wraparound, NULL, NULL, NULL, NULL, NULL);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tests:
2+
kernel.scheduler.wraparound:
3+
tags: kernel

0 commit comments

Comments
 (0)