From 73756fdb3a4f016b4f7e8969147b4980980c3ef4 Mon Sep 17 00:00:00 2001 From: Greg Link Date: Thu, 9 Jan 2025 11:00:09 -0500 Subject: [PATCH] Two basic structures for consideration This is not a mergeable commit, as it includes modifications to things that should not be committed upstream. --- CMakeLists.txt | 5 ++- common/CMakeLists.txt | 1 + common/inc/tx_api.h | 23 +++++++++++-- common/src/tx_thread_resume.c | 11 +++--- common/src/tx_thread_suspend.c | 4 +++ .../src/tx_thread_suspend_resume_extension.c | 34 +++++++++++++++++++ common/src/tx_thread_system_resume.c | 12 +++++++ common/src/tx_thread_system_suspend.c | 6 ++++ custom_tx_user.h | 33 ++++++++++++++++++ 9 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 common/src/tx_thread_suspend_resume_extension.c create mode 100644 custom_tx_user.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 05baf6c7b..f7f4e94ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,9 @@ target_sources(freertos-threadx ) target_link_libraries(freertos-threadx PUBLIC threadx) + +set(TX_USER_FILE "${CMAKE_CURRENT_LIST_DIR}/custom_tx_user.h" CACHE PATH "Path to custom tx_user.h") + # If the user provided an override, copy it to the custom directory if (NOT TX_USER_FILE) message(STATUS "Using default tx_user.h file") @@ -55,7 +58,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC ${CUSTOM_INC_DIR} ) -target_compile_definitions(${PROJECT_NAME} PUBLIC "TX_INCLUDE_USER_DEFINE_FILE" ) +target_compile_definitions(${PROJECT_NAME} PUBLIC "TX_INCLUDE_USER_DEFINE_FILE" "TX_ENABLE_THREAD_SYSTEM_RESUME_EXTENSION") # Enable a build target that produces a ZIP file of all sources set(CPACK_SOURCE_GENERATOR "ZIP") diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 715e64c75..9338d42a4 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -101,6 +101,7 @@ target_sources(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_stack_error_handler.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_stack_error_notify.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_suspend.c + ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_suspend_resume_extension.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_system_preempt_check.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_system_resume.c ${CMAKE_CURRENT_LIST_DIR}/src/tx_thread_system_suspend.c diff --git a/common/inc/tx_api.h b/common/inc/tx_api.h index dbf0d3460..641127c58 100644 --- a/common/inc/tx_api.h +++ b/common/inc/tx_api.h @@ -25,7 +25,7 @@ /* APPLICATION INTERFACE DEFINITION RELEASE */ /* */ /* tx_api.h PORTABLE C */ -/* 6.4.1 */ +/* 6.4.2 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ @@ -106,7 +106,10 @@ /* 03-01-2024 Tiejun Zhou Modified comment(s), */ /* update version number, */ /* resulting in version 6.4.1 */ -/* */ +/* XX-XX-2025 Greg Link Modified comment(s), */ +/* update version number, */ +/* add resume/suspend hooks, */ +/* resulting in version 6.4.2 */ /**************************************************************************/ #ifndef TX_API_H @@ -1779,6 +1782,22 @@ UINT _tx_thread_terminate(TX_THREAD *thread_ptr); UINT _tx_thread_time_slice_change(TX_THREAD *thread_ptr, ULONG new_time_slice, ULONG *old_time_slice); UINT _tx_thread_wait_abort(TX_THREAD *thread_ptr); +/* Two alternative ways to include the proposed extensions of Issue 426. + The first expects the extension to always have the same function prototype, + but it is a proper function, while the second uses a #define in tx_user.h + to extend the functionality instead. */ + +#ifdef TX_ENABLE_THREAD_SYSTEM_RESUME_EXTENSION +UINT _tx_thread_system_resume_extension(TX_THREAD *thread_ptr); +#define TX_THREAD_SYSTEM_RESUME_EXTENSION(a) _tx_thread_system_resume_extension(a) +#else +#warning TX_ENABLE_THREAD_SYSTEM_RESUME_EXTENSION is disabled +#define TX_THREAD_SYSTEM_RESUME_EXTENSION(a) ((void)(a)) +#endif + +#ifndef TX_THREAD_SYSTEM_SUSPEND_EXTENSION +#define TX_THREAD_SYSTEM_SUSPEND_EXTENSION(a) ((void)(a)) +#endif /* Define error checking shells for API services. These are only referenced by the application. */ diff --git a/common/src/tx_thread_resume.c b/common/src/tx_thread_resume.c index b97bdb4b7..54c2f78b2 100644 --- a/common/src/tx_thread_resume.c +++ b/common/src/tx_thread_resume.c @@ -90,12 +90,12 @@ TX_THREAD *current_thread; ULONG combined_flags; #ifdef TX_ENABLE_EVENT_TRACE -TX_TRACE_BUFFER_ENTRY *entry_ptr; -ULONG time_stamp = ((ULONG) 0); + TX_TRACE_BUFFER_ENTRY *entry_ptr; + ULONG time_stamp = ((ULONG) 0); #endif #if TX_MAX_PRIORITIES > 32 -UINT map_index; + UINT map_index; #endif @@ -241,6 +241,9 @@ UINT map_index; /* Log the thread status change. */ TX_EL_THREAD_STATUS_CHANGE_INSERT(thread_ptr, TX_READY) + /* Handle the thread resume extension, if defined*/ + TX_THREAD_SYSTEM_RESUME_EXTENSION(thread_ptr); + #ifdef TX_THREAD_ENABLE_PERFORMANCE_INFO /* Increment the total number of thread resumptions. */ @@ -261,7 +264,7 @@ UINT map_index; thread_ptr -> tx_thread_ready_next = thread_ptr; thread_ptr -> tx_thread_ready_previous = thread_ptr; -#if TX_MAX_PRIORITIES > 32 + #if TX_MAX_PRIORITIES > 32 /* Calculate the index into the bit map array. */ map_index = priority/((UINT) 32); diff --git a/common/src/tx_thread_suspend.c b/common/src/tx_thread_suspend.c index ccf3ebc33..64221354f 100644 --- a/common/src/tx_thread_suspend.c +++ b/common/src/tx_thread_suspend.c @@ -309,6 +309,10 @@ ULONG time_stamp = ((ULONG) 0); /* Log the thread status change. */ TX_EL_THREAD_STATUS_CHANGE_INSERT(thread_ptr, thread_ptr -> tx_thread_state) + + /* Handle the thread suspend extension, if defined*/ + TX_THREAD_SYSTEM_SUSPEND_EXTENSION(thread_ptr); + #ifdef TX_ENABLE_EVENT_TRACE /* If trace is enabled, save the current event pointer. */ diff --git a/common/src/tx_thread_suspend_resume_extension.c b/common/src/tx_thread_suspend_resume_extension.c new file mode 100644 index 000000000..fc78781de --- /dev/null +++ b/common/src/tx_thread_suspend_resume_extension.c @@ -0,0 +1,34 @@ +/*************************************************************************** + * Copyright (c) 2025 Microsoft Corporation + * + * 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/* Include necessary system files. */ + +#include "tx_api.h" +#include "tx_trace.h" +#include "tx_thread.h" +#include "tx_initialize.h" + +/* Test only, demo of user extension. If the below is not defined, + no need to include this function at all - it will never be called */ +#ifdef TX_ENABLE_THREAD_SYSTEM_RESUME_EXTENSION + +UINT _tx_thread_system_resume_extension(TX_THREAD *thread_ptr) +{ + UINT status = 0; + if (!thread_ptr->resume_recorded) + { + thread_ptr->resume_recorded_at += 1; + } + + /* Return completion status. */ + return(status); +} + +#endif //TX_ENABLE_THREAD_SYSTEM_RESUME_EXTENSION \ No newline at end of file diff --git a/common/src/tx_thread_system_resume.c b/common/src/tx_thread_system_resume.c index 7b9f557d7..98caefe76 100644 --- a/common/src/tx_thread_system_resume.c +++ b/common/src/tx_thread_system_resume.c @@ -182,6 +182,9 @@ UINT map_index; /* Log the thread status change. */ TX_EL_THREAD_STATUS_CHANGE_INSERT(thread_ptr, TX_READY) + /* Handle the thread resume extension, if defined*/ + TX_THREAD_SYSTEM_RESUME_EXTENSION(thread_ptr); + #ifdef TX_THREAD_ENABLE_PERFORMANCE_INFO /* Increment the total number of thread resumptions. */ @@ -435,6 +438,9 @@ UINT map_index; /* Log the thread status change. */ TX_EL_THREAD_STATUS_CHANGE_INSERT(thread_ptr, TX_READY) + + /* Handle the thread resume extension, if defined*/ + TX_THREAD_SYSTEM_RESUME_EXTENSION(thread_ptr); } else { @@ -600,6 +606,9 @@ UINT state; /* Log the thread status change. */ TX_EL_THREAD_STATUS_CHANGE_INSERT(thread_ptr, TX_READY) + /* Handle the thread resume extension, if defined*/ + TX_THREAD_SYSTEM_RESUME_EXTENSION(thread_ptr); + #ifdef TX_THREAD_ENABLE_PERFORMANCE_INFO /* Increment the total number of thread resumptions. */ @@ -702,6 +711,9 @@ UINT map_index; /* Log the thread status change. */ TX_EL_THREAD_STATUS_CHANGE_INSERT(thread_ptr, TX_READY) + /* Handle the thread resume extension, if defined*/ + TX_THREAD_SYSTEM_RESUME_EXTENSION(thread_ptr); + /* Pickup priority of thread. */ priority = thread_ptr -> tx_thread_priority; diff --git a/common/src/tx_thread_system_suspend.c b/common/src/tx_thread_system_suspend.c index 33f8cac28..3899bf260 100644 --- a/common/src/tx_thread_system_suspend.c +++ b/common/src/tx_thread_system_suspend.c @@ -169,6 +169,9 @@ ULONG time_stamp = ((ULONG) 0); /* Log the thread status change. */ TX_EL_THREAD_STATUS_CHANGE_INSERT(thread_ptr, thread_ptr -> tx_thread_state) + /* Handle the thread suspend extension, if defined*/ + TX_THREAD_SYSTEM_SUSPEND_EXTENSION(thread_ptr); + #ifdef TX_ENABLE_EVENT_TRACE /* If trace is enabled, save the current event pointer. */ @@ -761,6 +764,9 @@ ULONG time_stamp = ((ULONG) 0); /* Log the thread status change. */ TX_EL_THREAD_STATUS_CHANGE_INSERT(thread_ptr, thread_ptr -> tx_thread_state) + /* Handle the thread suspend extension, if defined*/ + TX_THREAD_SYSTEM_SUSPEND_EXTENSION(thread_ptr); + #ifdef TX_ENABLE_EVENT_TRACE /* If trace is enabled, save the current event pointer. */ diff --git a/custom_tx_user.h b/custom_tx_user.h new file mode 100644 index 000000000..d319c5d4f --- /dev/null +++ b/custom_tx_user.h @@ -0,0 +1,33 @@ +/*************************************************************************** + * Copyright (c) 2024 Microsoft Corporation + * + * 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. + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +#ifndef TX_USER_H +#define TX_USER_H + +/* Define user-specified additional fields to be stored in TX_THREAD */ +#define TX_THREAD_USER_EXTENSION UCHAR resume_recorded; \ + ULONG resume_recorded_at; \ + UCHAR suspend_recorded; \ + ULONG suspend_recorded_at; + +/* This is one way to define the extension. Because it's a macro, it + cannot handle static allocations internally, nor does it have a + proper calling context for tracing purposes. */ +#define TX_THREAD_SYSTEM_SUSPEND_EXTENSION(thread_ptr) \ +do { \ + if (!thread_ptr->suspend_recorded) \ + { \ + thread_ptr->suspend_recorded_at += 1; \ + thread_ptr->suspend_recorded = 1; \ + } \ +} while(0) + +#endif +