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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/***************************************************************************
* 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 the block allocate service's trace update in place.
The service records its event, remembers where the entry went and what its
time stamp was, and comes back to write the address of the block it handed
out into that same entry once it knows it. It does so only if the entry is
still the one it wrote, which it decides by comparing the time stamp. There
are two of those regions: one on the path that finds a block immediately, and
one on the path that suspends and is given a block by a later release.
The test walks both, and then drives the suspending one a second time with
the trace buffer deliberately written all the way round in between, so the
entry the service comes back to is a different event and the update is
correctly skipped.
Both threads are created with the suite's default core mapping, so both are
eligible for core 0 alone and only one of them runs at a time. The handovers
below are therefore preemptions on a single core, exactly as they are on the
monoprocessor, rather than two cores running the allocate and the release at
once. That is what makes the ordering the test relies on a property of the
arrangement instead of a race.
One configuration reaches less of this than the others, and it is a property
of the port rather than of the test. Under TX_MISRA_ENABLE the trace time
source is _tx_misra_time_stamp_get, which returns a constant, so every entry
in the buffer carries the same time stamp and the service's comparison reads
equal whatever has been written over the entry. The overwritten round below
therefore drives the same arm as the intact one in misra_trace_build, and the
arm that declines to update is reached in trace_build alone.
Without event tracing compiled in none of that code exists, so the test is
then an ordinary allocate, suspend and release exercise. */
#include <stdio.h>
#include "tx_api.h"
#define TX_SOURCE_CODE
#include "tx_trace.h"
/* The resident pool holds four blocks. The test thread takes all of them, so
that the helper thread's request has to suspend. */
#define BLOCK_SIZE 64
#define BLOCK_POOL_BLOCKS 4
#define BLOCK_POOL_SIZE (BLOCK_POOL_BLOCKS * (BLOCK_SIZE + sizeof(VOID *)))
/* The helper thread makes one request per round: one that finds the entry
unchanged, and one that finds it overwritten. */
#define HELPER_ROUNDS 2
static unsigned long thread_0_counter = 0;
static TX_THREAD thread_0;
static unsigned long thread_1_counter = 0;
static TX_THREAD thread_1;
static TX_BLOCK_POOL pool_0;
static UCHAR pool_0_memory[BLOCK_POOL_SIZE];
static VOID *blocks[BLOCK_POOL_BLOCKS];
static VOID *helper_block;
static UINT helper_status = TX_SUCCESS;
/* The buffer is large enough to hold a whole suspension and resumption without
coming round on itself, so the unchanged case is reached by the ordinary path
and the overwritten case only when the test asks for it. */
static UCHAR trace_buffer[2048];
#define TRACE_REGISTRY_ENTRIES 4
/* Define thread prototypes. */
static void thread_0_entry(ULONG thread_input);
static void thread_1_entry(ULONG thread_input);
/* Prototype for test control return. */
void test_control_return(UINT status);
/* Write one trace event into every slot of the buffer, so that whatever entry a
suspended service is holding on to has been replaced by a later event. One
pass of the buffer's own length is enough whatever position it starts from;
the two extra entries keep it so if an interrupt inserts an event of its own
while the loop runs. */
static void overwrite_trace_buffer(void)
{
#ifdef TX_ENABLE_EVENT_TRACE
ULONG entries;
entries = (ULONG) (_tx_trace_buffer_end_ptr - _tx_trace_buffer_start_ptr);
while (entries != ((ULONG) 0))
{
(VOID) tx_block_pool_info_get(&pool_0, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
entries--;
}
(VOID) tx_block_pool_info_get(&pool_0, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
(VOID) tx_block_pool_info_get(&pool_0, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL);
#endif
}
/* Define what the initial system looks like. */
#ifdef CTEST
void test_application_define(void *first_unused_memory)
#else
void threadx_block_pool_trace_update_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 Block Pool Trace Update Test............................... ERROR #1\n");
test_control_return(1);
}
/* The helper thread runs at a higher priority than the test thread, so that
it takes the processor the moment it is resumed and the moment a block is
released to it. Every handover in the test is therefore a preemption
rather than something that waits for a tick. */
status = tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
pointer, TEST_STACK_SIZE_PRINTF,
15, 15, TX_NO_TIME_SLICE, TX_DONT_START);
pointer = pointer + TEST_STACK_SIZE_PRINTF;
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Block Pool Trace Update Test............................... ERROR #2\n");
test_control_return(1);
}
/* Create the pool both threads allocate from. */
status = tx_block_pool_create(&pool_0, "pool 0", BLOCK_SIZE, pool_0_memory, sizeof(pool_0_memory));
/* Check for status. */
if (status != TX_SUCCESS)
{
printf("Running Block Pool Trace Update Test............................... ERROR #3\n");
test_control_return(1);
}
}
/* Define the test thread. */
static void thread_0_entry(ULONG thread_input)
{
UINT status;
UINT block;
/* Increment thread 0 counter. */
thread_0_counter++;
/* Inform user of success getting to this test. */
printf("Running Block Pool Trace Update Test............................... ");
/* Enable event tracing. Without it compiled in the service reports the
feature as disabled and the rest of the test runs unchanged. */
status = tx_trace_enable(trace_buffer, sizeof(trace_buffer), TRACE_REGISTRY_ENTRIES);
#ifdef TX_ENABLE_EVENT_TRACE
if (status != TX_SUCCESS)
#else
if (status != TX_FEATURE_NOT_ENABLED)
#endif
{
printf("ERROR #4\n");
test_control_return(1);
}
/* A block is available, so the service takes it immediately and comes back
to the entry it has just written. */
status = tx_block_allocate(&pool_0, &blocks[0], TX_NO_WAIT);
if (status != TX_SUCCESS)
{
printf("ERROR #5\n");
test_control_return(1);
}
status = tx_block_release(blocks[0]);
if (status != TX_SUCCESS)
{
printf("ERROR #6\n");
test_control_return(1);
}
/* Take every block, so that the helper thread's request has to suspend. */
for (block = 0; block < BLOCK_POOL_BLOCKS; block++)
{
status = tx_block_allocate(&pool_0, &blocks[block], TX_NO_WAIT);
if (status != TX_SUCCESS)
{
printf("ERROR #7\n");
test_control_return(1);
}
}
/* First round: the helper suspends, a block is released to it, and the
entry it wrote on the way in is still there. */
status = tx_thread_resume(&thread_1);
if (status != TX_SUCCESS)
{
printf("ERROR #8\n");
test_control_return(1);
}
status = tx_block_release(blocks[0]);
if (status != TX_SUCCESS)
{
printf("ERROR #9\n");
test_control_return(1);
}
/* The helper took the block, gave it back and suspended itself, so the pool
has exactly one free block again. Take it, and exhaust the pool for the
second round. */
status = tx_block_allocate(&pool_0, &blocks[0], TX_NO_WAIT);
if (status != TX_SUCCESS)
{
printf("ERROR #10\n");
test_control_return(1);
}
/* Second round: the same handover, but this time the trace buffer is
written all the way round while the helper is suspended, so the entry it
comes back to belongs to a later event. */
status = tx_thread_resume(&thread_1);
if (status != TX_SUCCESS)
{
printf("ERROR #11\n");
test_control_return(1);
}
overwrite_trace_buffer();
status = tx_block_release(blocks[0]);
if (status != TX_SUCCESS)
{
printf("ERROR #12\n");
test_control_return(1);
}
/* Both rounds have to have succeeded from the helper's side. A failure
there means the handover did not happen the way the test assumes, which
would leave the regions above undriven. */
if (helper_status != TX_SUCCESS)
{
printf("ERROR #13\n");
test_control_return(1);
}
if (thread_1_counter != HELPER_ROUNDS)
{
printf("ERROR #14\n");
test_control_return(1);
}
/* Give the rest of the pool back. */
for (block = 1; block < BLOCK_POOL_BLOCKS; block++)
{
status = tx_block_release(blocks[block]);
if (status != TX_SUCCESS)
{
printf("ERROR #15\n");
test_control_return(1);
}
}
status = tx_trace_disable();
#ifdef TX_ENABLE_EVENT_TRACE
if (status != TX_SUCCESS)
#else
if (status != TX_FEATURE_NOT_ENABLED)
#endif
{
printf("ERROR #16\n");
test_control_return(1);
}
printf("SUCCESS!\n");
test_control_return(0);
}
/* Define the helper thread. Each round suspends on an exhausted pool, takes the
block the test thread releases to it, gives it straight back, and then stands
down until the test thread wants it again. */
static void thread_1_entry(ULONG thread_input)
{
UINT round;
for (round = 0; round < HELPER_ROUNDS; round++)
{
if (tx_block_allocate(&pool_0, &helper_block, TX_WAIT_FOREVER) != TX_SUCCESS)
{
helper_status = TX_NOT_DONE;
}
else if (tx_block_release(helper_block) != TX_SUCCESS)
{
helper_status = TX_NOT_DONE;
}
else
{
/* Round complete. */
thread_1_counter++;
}
(VOID) tx_thread_suspend(&thread_1);
}
}
|