Files
threadx/common/src/tx_timer_create.c
T
Frédéric DesbiensandTilen Majerle cf577c7137 Made ThreadX object names const-qualifiable behind an option (#761)
Fixes #61

Object names are exposed as writable pointers throughout the kernel API, which
rejects string literals in C++ and lets a caller modify a name an object still
holds. Information services return those names through writable double
pointers.

Create services, control blocks, information services, the module manager and
trace registration now preserve const qualification, behind
TX_ENABLE_CONST_NAMES. The option defaults to off, so a build that says nothing
gets exactly the types it got before. It is opt-in rather than opt-out because
it changes the type of a public struct field: application code that copies a
name into a writable CHAR * stops compiling, which is a reasonable thing to ask
of a minor release and not of a patch one. Issue #780 tracks making it the
default in 6.6.

Two things the option reaches that its own call sites do not.
TX_CHAR_TO_UCHAR_POINTER_CONVERT has exactly two users, both of them reading an
object name in _tx_trace_object_register, and every form of that macro but the
MISRA one casts the qualifier away without saying so; the conversion is now
const in and const out, so nothing launders const to make the build pass. The
FreeRTOS adapter holds the name pcTaskGetName retrieves in a TX_NAME_CONST
pointer so that it tracks whichever declaration tx_thread_info_get has, and
keeps its writable return type through an explicit MISRA C:2012 Rule 11.8 cast,
because that signature is part of the FreeRTOS API.

Default build: all seven host configurations and all five SMP configurations
build with zero warnings and pass -- 113/113 on five host configurations,
100/100 on the two MISRA builds, 118/118 on SMP, 3/3 FreeRTOS. With
TX_ENABLE_CONST_NAMES set, the host default and both MISRA configurations, the
SMP trace configuration and the FreeRTOS adapter build with zero warnings and
pass.

Co-authored-by: Tilen Majerle <tilen@majerle.eu>
Assisted-by: Codex (gpt-6-astra) <noreply@openai.com>
Assisted-by: Claude Code (Opus 5) <noreply@anthropic.com>
2026-09-28 14:08:13 -04:00

164 lines
7.3 KiB
C

/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2026-present 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.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/* Portions of this file were generated with AI assistance. */
/**************************************************************************/
/**************************************************************************/
/** */
/** ThreadX Component */
/** */
/** Timer */
/** */
/**************************************************************************/
/**************************************************************************/
#define TX_SOURCE_CODE
/* Include necessary system files. */
#include "tx_api.h"
#include "tx_trace.h"
#include "tx_timer.h"
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _tx_timer_create PORTABLE C */
/* 6.1 */
/* AUTHOR */
/* */
/* William E. Lamie, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function creates an application timer from the specified */
/* input. */
/* */
/* INPUT */
/* */
/* timer_ptr Pointer to timer control block */
/* name_ptr Pointer to timer name */
/* expiration_function Application expiration function */
/* initial_ticks Initial expiration ticks */
/* reschedule_ticks Reschedule ticks */
/* auto_activate Automatic activation flag */
/* */
/* OUTPUT */
/* */
/* TX_SUCCESS Successful completion status */
/* */
/* CALLS */
/* */
/* _tx_timer_system_activate Timer activation function */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _tx_timer_create(TX_TIMER *timer_ptr, TX_NAME_CONST CHAR *name_ptr,
VOID (*expiration_function)(ULONG id), ULONG expiration_input,
ULONG initial_ticks, ULONG reschedule_ticks, UINT auto_activate)
{
TX_INTERRUPT_SAVE_AREA
TX_TIMER *next_timer;
TX_TIMER *previous_timer;
/* Initialize timer control block to all zeros. */
TX_MEMSET(timer_ptr, 0, (sizeof(TX_TIMER)));
/* Setup the basic timer fields. */
timer_ptr -> tx_timer_name = name_ptr;
timer_ptr -> tx_timer_internal.tx_timer_internal_remaining_ticks = initial_ticks;
timer_ptr -> tx_timer_internal.tx_timer_internal_re_initialize_ticks = reschedule_ticks;
timer_ptr -> tx_timer_internal.tx_timer_internal_timeout_function = expiration_function;
timer_ptr -> tx_timer_internal.tx_timer_internal_timeout_param = expiration_input;
/* Disable interrupts to put the timer on the created list. */
TX_DISABLE
/* Setup the timer ID to make it valid. */
timer_ptr -> tx_timer_id = TX_TIMER_ID;
/* Place the timer on the list of created application timers. First,
check for an empty list. */
if (_tx_timer_created_count == TX_EMPTY)
{
/* The created timer list is empty. Add timer to empty list. */
_tx_timer_created_ptr = timer_ptr;
timer_ptr -> tx_timer_created_next = timer_ptr;
timer_ptr -> tx_timer_created_previous = timer_ptr;
}
else
{
/* This list is not NULL, add to the end of the list. */
next_timer = _tx_timer_created_ptr;
previous_timer = next_timer -> tx_timer_created_previous;
/* Place the new timer in the list. */
next_timer -> tx_timer_created_previous = timer_ptr;
previous_timer -> tx_timer_created_next = timer_ptr;
/* Setup this timer's created links. */
timer_ptr -> tx_timer_created_previous = previous_timer;
timer_ptr -> tx_timer_created_next = next_timer;
}
/* Increment the number of created timers. */
_tx_timer_created_count++;
/* Optional timer create extended processing. */
TX_TIMER_CREATE_EXTENSION(timer_ptr)
/* If trace is enabled, register this object. */
TX_TRACE_OBJECT_REGISTER(TX_TRACE_OBJECT_TYPE_TIMER, timer_ptr, name_ptr, initial_ticks, reschedule_ticks)
/* If trace is enabled, insert this call in the trace buffer. */
TX_TRACE_IN_LINE_INSERT(TX_TRACE_TIMER_CREATE, timer_ptr, initial_ticks, reschedule_ticks, auto_activate, TX_TRACE_TIMER_EVENTS)
/* Log this kernel call. */
TX_EL_TIMER_CREATE_INSERT
/* Determine if this timer needs to be activated. */
if (auto_activate == TX_AUTO_ACTIVATE)
{
#ifdef TX_TIMER_ENABLE_PERFORMANCE_INFO
/* Increment the total activations counter. */
_tx_timer_performance_activate_count++;
/* Increment the number of activations on this timer. */
timer_ptr -> tx_timer_performance_activate_count++;
#endif
/* Call actual activation function. */
_tx_timer_system_activate(&(timer_ptr -> tx_timer_internal));
}
/* Restore interrupts. */
TX_RESTORE
/* Return TX_SUCCESS. */
return(TX_SUCCESS);
}