Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions common_smp/inc/tx_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,20 +643,12 @@ UINT first_bit_set;
ULONG local_priority_map;
UINT next_priority;

local_priority_map = _tx_thread_priority_maps[0];
local_priority_map = local_priority_map >> priority;
next_priority = priority;
if (local_priority_map == ((ULONG) 0))
{
next_priority = ((UINT) TX_MAX_PRIORITIES);
}
else
next_priority = ((UINT) TX_MAX_PRIORITIES);
if (priority < ((UINT) TX_MAX_PRIORITIES))
{
if (next_priority >= ((UINT) TX_MAX_PRIORITIES))
{
next_priority = ((UINT) TX_MAX_PRIORITIES);
}
else
local_priority_map = _tx_thread_priority_maps[0];
local_priority_map = local_priority_map >> priority;
if (local_priority_map != ((ULONG) 0))
{
TX_LOWEST_SET_BIT_CALCULATE(local_priority_map, first_bit_set)
next_priority = priority + first_bit_set;
Expand Down
18 changes: 5 additions & 13 deletions common_smp/src/tx_thread_smp_utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,12 @@ UINT first_bit_set;
ULONG local_priority_map;
UINT next_priority;

local_priority_map = _tx_thread_priority_maps[0];
local_priority_map = local_priority_map >> priority;
next_priority = priority;
if (local_priority_map == ((ULONG) 0))
{
next_priority = ((UINT) TX_MAX_PRIORITIES);
}
else
next_priority = ((UINT) TX_MAX_PRIORITIES);
if (priority < ((UINT) TX_MAX_PRIORITIES))
{
if (next_priority >= ((UINT) TX_MAX_PRIORITIES))
{
next_priority = ((UINT) TX_MAX_PRIORITIES);
}
else
local_priority_map = _tx_thread_priority_maps[0];
local_priority_map = local_priority_map >> priority;
if (local_priority_map != ((ULONG) 0))
{
TX_LOWEST_SET_BIT_CALCULATE(local_priority_map, first_bit_set)
next_priority = priority + first_bit_set;
Expand Down
6 changes: 6 additions & 0 deletions test/smp/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@ endif()
if(THREADX_ARCH STREQUAL "win64")
add_subdirectory(windows_port)
endif()

# Check both priority lookup implementations with GCC shift instrumentation.
if(THREADX_ARCH STREQUAL "linux" AND CMAKE_C_COMPILER_ID STREQUAL "GNU"
AND CMAKE_BUILD_TYPE STREQUAL "default_build_coverage")
add_subdirectory(next_priority)
endif()
26 changes: 26 additions & 0 deletions test/smp/cmake/next_priority/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2026 Eclipse ThreadX contributors
# SPDX-License-Identifier: MIT

get_filename_component(THREADX_SOURCE "${CMAKE_CURRENT_LIST_DIR}/../../../.." ABSOLUTE)

foreach(implementation inline out_of_line)
set(test_target smp_next_priority_${implementation})
add_executable(${test_target} next_priority.c)
target_include_directories(${test_target} PRIVATE
"${THREADX_SOURCE}/common_smp/inc"
"${THREADX_SOURCE}/ports_smp/linux/gnu/inc")
target_compile_definitions(${test_target} PRIVATE _GNU_SOURCE)

# Use a 32-bit priority map. Trap mode avoids a multilib libubsan dependency.
target_compile_options(${test_target} PRIVATE -UTX_MAX_PRIORITIES -DTX_MAX_PRIORITIES=32
-fsanitize=shift -fsanitize-undefined-trap-on-error -ffunction-sections -fdata-sections)
target_link_options(${test_target} PRIVATE -Wl,--gc-sections)

if(implementation STREQUAL "inline")
target_compile_definitions(${test_target} PRIVATE TX_TEST_INLINE_PRIORITY_FIND)
else()
target_sources(${test_target} PRIVATE
"${THREADX_SOURCE}/common_smp/src/tx_thread_smp_utilities.c")
endif()
add_test(NAME ${test_target} COMMAND ${test_target})
endforeach()
52 changes: 52 additions & 0 deletions test/smp/cmake/next_priority/next_priority.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/***************************************************************************
* Copyright (c) 2026 Eclipse ThreadX contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* AI Disclosure: This file was largely AI-generated by Codex (GPT-6.2-Sol).
* The AI-generated portions may be considered public domain (CC0-1.0)
* and not subject to the project's licence. The human contributor has
* reviewed and verified that the code is correct.
*
* SPDX-License-Identifier: MIT and CC0-1.0
**************************************************************************/

#define TX_THREAD_SMP_SOURCE_CODE
#include "tx_api.h"

#ifdef TX_TEST_INLINE_PRIORITY_FIND
/* MISRA C:2023 Rule 20.5 deviation. Enable the inline helper in this test translation unit. */
#undef TX_DISABLE_INLINE
#endif

#include "tx_thread.h"

ULONG _tx_thread_priority_maps[1];

/* Check the highest valid priority, the empty result, and the range limit. */
int main(void)
{
const UINT highest_priority = ((UINT) TX_MAX_PRIORITIES) - 1U;

_tx_thread_priority_maps[0] = 0x80000000UL;
if (_tx_thread_smp_next_priority_find(highest_priority) != highest_priority)
{
return(1);
}

_tx_thread_priority_maps[0] = 1UL;
if (_tx_thread_smp_next_priority_find(highest_priority) != ((UINT) TX_MAX_PRIORITIES))
{
return(2);
}

/* The range limit must return the sentinel. Shift sanitization checks the operation itself. */
if (_tx_thread_smp_next_priority_find(TX_MAX_PRIORITIES) != ((UINT) TX_MAX_PRIORITIES))
{
return(3);
}

return(0);
}
11 changes: 10 additions & 1 deletion test/smp/regression/threadx_thread_priority_change.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ extern unsigned long _tx_thread_priority_map;
/* Prototype for test control return. */
void test_control_return(UINT status);

/* Prototype for internal function. */
/* Prototype for internal functions. */
VOID _tx_thread_smp_simple_priority_change(TX_THREAD *thread_ptr,UINT new_priority);
UINT _tx_thread_smp_next_priority_find(UINT priority);


/* This test routine is used to get a thread of a non ready state. */
Expand Down Expand Up @@ -487,6 +488,14 @@ TX_THREAD *temp_thread;
thread_0.tx_thread_inherit_priority = TX_MAX_PRIORITIES;
TX_RESTORE

/* Verify that calling _tx_thread_smp_next_priority_find with a priority
at the maximum limit returns TX_MAX_PRIORITIES without an out-of-range shift. */
if (_tx_thread_smp_next_priority_find(TX_MAX_PRIORITIES) != ((UINT) TX_MAX_PRIORITIES))
{
printf("ERROR #16\n");
test_control_return(1);
}

/* Successful test. */
printf("SUCCESS!\n");
test_control_return(0);
Expand Down