/*************************************************************************** * 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 Claude Code (Opus 5). * 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 **************************************************************************/ /* This test drives every mutex service with event tracing enabled. Each service carries one in-line trace insert, and that insert behaves differently according to the system state it runs in, whether its event is filtered, whether it is the entry that wraps the trace buffer, and whether a buffer-full notification is registered. The test walks the mutex services once per combination, so every service's insert is driven through all of them. Every one of these services carries its insert ahead of its own ownership and suspension logic, so nothing the service goes on to do can write an event buffer entry in front of it. The buffer is therefore positioned one entry short of its end rather than two, and the pass asserts a wrap per service so that a service whose boundary entry was taken by something else fails here instead of quietly losing coverage. The resident mutex is deliberately created TX_NO_INHERIT. A release with nothing suspended on it then takes the simple path and leaves the calling thread's priority alone, so no pass here depends on the inheritance behaviour, which has tests of its own. The SMP insert reads the system state and the current thread of the core it is running on rather than a single global, so the passes below that force a system state write the entry of the core this test is running on, under the SMP protection primitive. Every thread in this suite is created mapped to core 0 alone, but the core is read rather than assumed. Without event tracing compiled in the inserts expand to nothing, so the test only checks that the trace services report the feature as disabled. */ #include #include "tx_api.h" #define TX_SOURCE_CODE /* The system state is per core on SMP, and tx_thread.h remaps the name to a function call returning the running core's entry unless this macro says the file wants the array itself. The forced-state passes below have to write it, so they need the array. */ #define TX_THREAD_SMP_SOURCE_CODE #include "tx_trace.h" #include "tx_thread.h" #include "tx_initialize.h" /* Define the ISR dispatch. */ extern VOID (*test_isr_dispatch)(void); /* Define the mutex services, in the order the test drives them. The scratch mutex is created by the first and deleted by the last, and the get is balanced by the put that follows it, so a pass over the whole list leaves the system as it found it. */ #define MUTEX_SERVICE_CREATE 0 #define MUTEX_SERVICE_GET 1 #define MUTEX_SERVICE_PRIORITIZE 2 #define MUTEX_SERVICE_INFO_GET 3 #define MUTEX_SERVICE_PUT 4 #define MUTEX_SERVICE_DELETE 5 #define MUTEX_SERVICES 6 /* The services an ISR is allowed to call. The error checking shell rejects a create, a get and a put from an interrupt, and a delete from any caller that is not a thread, which leaves the prioritize and the information service. */ #define MUTEX_ISR_FIRST_SERVICE MUTEX_SERVICE_PRIORITIZE #define MUTEX_ISR_LAST_SERVICE MUTEX_SERVICE_INFO_GET static unsigned long thread_0_counter = 0; static TX_THREAD thread_0; static TX_MUTEX mutex_0; static TX_MUTEX mutex_1; static ULONG buffer_full_count = 0; static ULONG isr_pass_done = 0; static UINT isr_status = TX_SUCCESS; /* The trace buffer is deliberately small. Several passes over the services fit in it many times over, so the wrap path is reached by ordinary use as well as by the positioned wrap below. */ static UCHAR trace_buffer[512]; #define TRACE_REGISTRY_ENTRIES 4 /* Define thread prototypes. */ static void thread_0_entry(ULONG thread_input); /* Prototype for test control return. */ void test_control_return(UINT status); /* Drive one mutex service through its published entry point. */ static UINT mutex_service(UINT service) { UINT status; switch (service) { case MUTEX_SERVICE_CREATE: status = tx_mutex_create(&mutex_1, "mutex 1", TX_NO_INHERIT); break; case MUTEX_SERVICE_GET: status = tx_mutex_get(&mutex_0, TX_NO_WAIT); break; case MUTEX_SERVICE_PRIORITIZE: status = tx_mutex_prioritize(&mutex_0); break; case MUTEX_SERVICE_INFO_GET: status = tx_mutex_info_get(&mutex_0, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL); break; case MUTEX_SERVICE_PUT: status = tx_mutex_put(&mutex_0); break; default: status = tx_mutex_delete(&mutex_1); break; } return(status); } /* The status a service returns when the pass above is run to completion. No mutex service is compiled out by any of the eight configurations, so every one of them returns success. */ static UINT mutex_service_expected(UINT service) { return(TX_SUCCESS); } #ifdef TX_ENABLE_EVENT_TRACE /* Drive one mutex service through its internal entry point. The error checking shell rejects a create, a get and a put from an interrupt and a delete from any caller that is not a thread, so the inserts inside those services cannot be reached through the published entry points at a forced system state. The shell itself is covered by its own tests; what is under test here is the insert behind it. */ static UINT mutex_service_internal(UINT service) { UINT status; switch (service) { case MUTEX_SERVICE_CREATE: status = _tx_mutex_create(&mutex_1, "mutex 1", TX_NO_INHERIT); break; case MUTEX_SERVICE_GET: status = _tx_mutex_get(&mutex_0, TX_NO_WAIT); break; case MUTEX_SERVICE_PRIORITIZE: status = _tx_mutex_prioritize(&mutex_0); break; case MUTEX_SERVICE_INFO_GET: status = _tx_mutex_info_get(&mutex_0, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL); break; case MUTEX_SERVICE_PUT: status = _tx_mutex_put(&mutex_0); break; default: status = _tx_mutex_delete(&mutex_1); break; } return(status); } /* Run a pass over the services with the system state forced to the value the insert has to see. The SMP protection is held across the pass, so no other core and no interrupt can observe the forced value, and the interrupt nesting count the same array element carries is restored before anything else reads it. The element is the running core's, read rather than assumed. */ static UINT pass_with_system_state(ULONG system_state) { TX_INTERRUPT_SAVE_AREA UINT service; UINT core; UINT status = TX_SUCCESS; ULONG saved_system_state; TX_DISABLE core = tx_thread_smp_core_get(); saved_system_state = _tx_thread_system_state[core]; _tx_thread_system_state[core] = system_state; for (service = 0; service < MUTEX_SERVICES; service++) { if (mutex_service_internal(service) != mutex_service_expected(service)) { status = TX_NOT_DONE; } } _tx_thread_system_state[core] = saved_system_state; TX_RESTORE return(status); } /* Position the trace buffer one entry short of its end, so that the next event inserted is the one that wraps it. A wrap reached by filling the buffer lands on whichever service happens to write the last entry; positioning puts each service's own insert on the boundary in turn. */ static void position_at_buffer_end(void) { _tx_trace_buffer_current_ptr = _tx_trace_buffer_end_ptr - 1; _tx_trace_header_ptr -> tx_trace_header_buffer_current_pointer = (ULONG) _tx_trace_buffer_current_ptr; } /* Run a pass over the services, wrapping the trace buffer on every one of them. The positioning and the call are held under one SMP protection, so the boundary entry is the service's own by construction: every trace insert in the kernel is written inside that same protection, so nothing else can take the slot in between. Once a notification is registered, every service in the pass has to report a wrap of its own. Checking that per service rather than in total is what makes the positioning prove itself: a service whose boundary entry was taken by something else fails here instead of quietly losing coverage. */ static UINT pass_with_buffer_wrap(UINT check_wraps) { TX_INTERRUPT_SAVE_AREA UINT service; UINT status = TX_SUCCESS; ULONG before; for (service = 0; service < MUTEX_SERVICES; service++) { before = buffer_full_count; TX_DISABLE position_at_buffer_end(); if (mutex_service(service) != mutex_service_expected(service)) { status = TX_NOT_DONE; } TX_RESTORE if ((check_wraps == TX_TRUE) && (buffer_full_count == before)) { status = TX_NOT_DONE; } } return(status); } /* Count the trace buffer wraps the kernel reports. */ static VOID trace_buffer_full(void *buffer) { buffer_full_count++; } /* Drive the services an ISR may call, once, from interrupt context. */ static void test_isr(void) { UINT service; if (isr_pass_done == 0) { for (service = MUTEX_ISR_FIRST_SERVICE; service <= MUTEX_ISR_LAST_SERVICE; service++) { if (mutex_service(service) != mutex_service_expected(service)) { isr_status = TX_NOT_DONE; } } isr_pass_done = 1; } } #endif /* Define what the initial system looks like. */ #ifdef CTEST void test_application_define(void *first_unused_memory) #else void threadx_mutex_trace_application_define(void *first_unused_memory) #endif { UINT status; CHAR *pointer; /* Put first available memory address into a character pointer. */ pointer = (CHAR *) first_unused_memory; /* Put system definition stuff in here, e.g. thread creates and other assorted create information. */ status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1, pointer, TEST_STACK_SIZE_PRINTF, 16, 16, TX_NO_TIME_SLICE, TX_AUTO_START); pointer = pointer + TEST_STACK_SIZE_PRINTF; /* Check for status. */ if (status != TX_SUCCESS) { printf("Running Mutex Trace Test........................................... ERROR #1\n"); test_control_return(1); } /* Create the mutex the services are driven against. Priority inheritance is off, so a release with nothing suspended leaves priorities alone. */ status = tx_mutex_create(&mutex_0, "mutex 0", TX_NO_INHERIT); /* Check for status. */ if (status != TX_SUCCESS) { printf("Running Mutex Trace Test........................................... ERROR #2\n"); test_control_return(1); } } /* Define the test thread. */ static void thread_0_entry(ULONG thread_input) { UINT status; UINT service; #ifdef TX_ENABLE_EVENT_TRACE ULONG wait; #endif /* Increment thread 0 counter. */ thread_0_counter++; /* Inform user of success getting to this test. */ printf("Running Mutex Trace Test........................................... "); /* Enable event tracing. */ status = tx_trace_enable(trace_buffer, sizeof(trace_buffer), TRACE_REGISTRY_ENTRIES); #ifndef TX_ENABLE_EVENT_TRACE /* Without tracing compiled in the inserts expand to nothing, so there is nothing here to drive. Check that the service says so and finish. */ if (status != TX_FEATURE_NOT_ENABLED) { printf("ERROR #3\n"); test_control_return(1); } /* One pass over the services, so the test still exercises them. */ for (service = 0; service < MUTEX_SERVICES; service++) { if (mutex_service(service) != mutex_service_expected(service)) { printf("ERROR #4\n"); test_control_return(1); } } printf("SUCCESS!\n"); test_control_return(0); #else if (status != TX_SUCCESS) { printf("ERROR #5\n"); test_control_return(1); } /* Thread context, every event enabled: the insert runs and takes the current thread's priority. */ for (service = 0; service < MUTEX_SERVICES; service++) { if (mutex_service(service) != mutex_service_expected(service)) { printf("ERROR #6\n"); test_control_return(1); } } /* Mutex events filtered out: the insert is reached and declines to record anything. */ status = tx_trace_event_filter(TX_TRACE_MUTEX_EVENTS); if (status != TX_SUCCESS) { printf("ERROR #7\n"); test_control_return(1); } for (service = 0; service < MUTEX_SERVICES; service++) { if (mutex_service(service) != mutex_service_expected(service)) { printf("ERROR #8\n"); test_control_return(1); } } status = tx_trace_event_unfilter(TX_TRACE_MUTEX_EVENTS); if (status != TX_SUCCESS) { printf("ERROR #9\n"); test_control_return(1); } /* Interrupt context: the insert records the interrupted thread instead of a priority. Bounded, so a tick that never arrives fails the test rather than hanging it. */ test_isr_dispatch = test_isr; wait = 0; while ((isr_pass_done == 0) && (wait < 100)) { tx_thread_sleep(1); wait++; } test_isr_dispatch = TX_NULL; if (isr_pass_done == 0) { printf("ERROR #10\n"); test_control_return(1); } if (isr_status != TX_SUCCESS) { printf("ERROR #11\n"); test_control_return(1); } /* Initialization context: the insert has no thread to record at all. */ if (pass_with_system_state(TX_INITIALIZE_IN_PROGRESS) != TX_SUCCESS) { printf("ERROR #12\n"); test_control_return(1); } /* The same state an interrupt presents, for the services an ISR may not call. */ if (pass_with_system_state((ULONG) 1) != TX_SUCCESS) { printf("ERROR #13\n"); test_control_return(1); } /* Wrap the trace buffer on every service with no notification registered, then again with one. */ if (pass_with_buffer_wrap(TX_FALSE) != TX_SUCCESS) { printf("ERROR #14\n"); test_control_return(1); } status = tx_trace_buffer_full_notify(trace_buffer_full); if (status != TX_SUCCESS) { printf("ERROR #15\n"); test_control_return(1); } if (pass_with_buffer_wrap(TX_TRUE) != TX_SUCCESS) { printf("ERROR #16\n"); test_control_return(1); } /* Every service in the last pass wrapped the buffer, so the notification has to have been called once for each of them. */ if (buffer_full_count < MUTEX_SERVICES) { printf("ERROR #17\n"); test_control_return(1); } status = tx_trace_disable(); if (status != TX_SUCCESS) { printf("ERROR #18\n"); test_control_return(1); } printf("SUCCESS!\n"); test_control_return(0); #endif }