summaryrefslogtreecommitdiff
path: root/test/tx/thread_transition/threadx_thread_transition_host_test_port.h
blob: 204933e3b88f77212cadfa1915f04e8e83fda97f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/***************************************************************************
 * 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
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** ThreadX Test                                                          */
/**                                                                       */
/**   Thread lifecycle transition host test port shim                     */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/* TX_NOT_INTERRUPTABLE and TX_DISABLE_ERROR_CHECKING are not among the five
   build configurations either regression tree compiles, and both trees build
   the whole ThreadX library once per configuration, so there is no way to
   reach those two from inside them.  The completion and termination
   transitions have code that only exists under TX_NOT_INTERRUPTABLE -- the
   callback there runs with interrupts disabled and the transition marker has
   to be set and cleared explicitly rather than by the interruptable
   suspension path -- and that code is exactly where a marker cleared at the
   wrong point would make a normally completed thread permanently undeletable.
   It therefore needs a test rather than a compile.

   This harness compiles the four sources that change, plus the two error
   wrappers whose absence a TX_DISABLE_ERROR_CHECKING build is defined by,
   directly into a test executable, once per configuration.  It is deliberately
   not part of the coverage report: the same file compiled under different
   feature macros has different line sets, and merging them would confuse the
   union rather than add to it.  The configurations the trees do build are what
   the coverage figure is taken from.

   This header is force-included ahead of each source under test.  It brings
   the base port's headers in first, so every declaration a source needs is the
   port's own, and then replaces the primitives that would otherwise reach the
   host's pthread emulation:

     - the interrupt lock becomes a counter, which is also how the test asserts
       that the transition really did run with interrupts disabled and left the
       nesting balanced,
     - the delete and reset port completions become recorders, because on this
       port they cancel and join the pthread backing the thread, which is not
       something a host test that owns no such thread can be asked to do.  */

#ifndef THREADX_THREAD_TRANSITION_HOST_TEST_PORT_H
#define THREADX_THREAD_TRANSITION_HOST_TEST_PORT_H

#define TX_SOURCE_CODE

#include "tx_api.h"

#undef TX_INTERRUPT_SAVE_AREA
#undef TX_DISABLE
#undef TX_RESTORE
#undef TX_THREAD_DELETE_PORT_COMPLETION
#undef TX_THREAD_RESET_PORT_COMPLETION

/* Nesting depth of the stand-in interrupt lock, the deepest it has reached,
   and the number of times a restore was issued without a matching disable.
   Defined by the test, which is the only translation unit that reads them.  */

extern unsigned int test_interrupt_disable_depth;
extern unsigned int test_interrupt_disable_max_depth;
extern unsigned int test_interrupt_restore_underflows;

/* Counts of the two port completions, and the interrupt depth each was
   entered at.  */

extern unsigned int test_delete_port_completion_count;
extern unsigned int test_reset_port_completion_count;

#define TX_INTERRUPT_SAVE_AREA

#define TX_DISABLE                                                              \
    {                                                                           \
        test_interrupt_disable_depth++;                                         \
        if (test_interrupt_disable_depth > test_interrupt_disable_max_depth)    \
        {                                                                       \
            test_interrupt_disable_max_depth = test_interrupt_disable_depth;    \
        }                                                                       \
    }

#define TX_RESTORE                                                              \
    {                                                                           \
        if (test_interrupt_disable_depth == 0U)                                 \
        {                                                                       \
            test_interrupt_restore_underflows++;                                \
        }                                                                       \
        else                                                                    \
        {                                                                       \
            test_interrupt_disable_depth--;                                     \
        }                                                                       \
    }

#define TX_THREAD_DELETE_PORT_COMPLETION(thread_ptr)                            \
    {                                                                           \
        (void) (thread_ptr);                                                    \
        test_delete_port_completion_count++;                                    \
    }

#define TX_THREAD_RESET_PORT_COMPLETION(thread_ptr)                             \
    {                                                                           \
        (void) (thread_ptr);                                                    \
        test_reset_port_completion_count++;                                     \
    }

#endif