summaryrefslogtreecommitdiff
path: root/test/smp/regression/threadx_object_delete_suspension_test.c
blob: 20633f08700f6388a3e2c12ceff79bbcd95ee0e0 (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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/***************************************************************************
 * 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 deletes a block pool, a byte pool, an event flags group and a
   queue while two threads are suspended on each of them, and checks that every
   suspended thread is released with TX_DELETED.

   Each of those four delete routines carries the same loop, and in this suite
   the whole loop body was uncovered -- thirty-six lines across the four files,
   the largest single gap in the SMP coverage report:

       while (suspended_count != TX_NO_SUSPENSIONS)
       {
           suspended_count--;
           TX_DISABLE
           thread_ptr -> tx_thread_suspend_cleanup =  TX_NULL;
           thread_ptr -> tx_thread_suspend_status =  TX_DELETED;
           next_thread =  thread_ptr -> tx_thread_suspended_next;
           ...
           _tx_thread_system_resume(thread_ptr);
           thread_ptr =  next_thread;
       }

   The reason it was uncovered is worth writing down, because it is not that
   the suite never deletes these objects -- it deletes all four after every
   single test. test_control_cleanup() in the ThreadX suite deletes the
   application's objects first and its threads last, so a test that ends with a
   thread parked on a queue has that thread walked out of the queue by
   tx_queue_delete. The SMP suite's cleanup deletes the threads first, on
   purpose: it was changed so that no application-owned object is still
   referenced when the object loops run, which is what stopped a class of
   teardown hang. Correct, and the side effect is that every one of those
   deletes now runs against an empty suspension list.

   So the coverage has to come from a test that means to delete a busy object
   rather than from teardown that happens to. threadx_semaphore_delete_test
   already does exactly this for semaphores, which is why tx_semaphore_delete.c
   is the one member of the family that was fully covered; this is the same
   idea for the other four.

   Two waiters per object rather than one, so the loop runs twice and its back
   edge is taken as well as its body.

   The threads are not confined to a core. On this port they start on separate
   cores and suspend in whatever order they get scheduled, so the control
   thread waits on the object's own suspended count through tx_*_info_get
   rather than on an ordering it cannot guarantee. The wait is bounded: a
   suspension that never arrives fails the test instead of hanging it.  */

#include   <stdio.h>
#include   "tx_api.h"
#include   "threadx_test_port.h"


void  test_control_return(UINT status);


/* The four objects, deleted in this order.  */

#define STAGE_BLOCK_POOL    ((UINT) 0)
#define STAGE_BYTE_POOL     ((UINT) 1)
#define STAGE_EVENT_FLAGS   ((UINT) 2)
#define STAGE_QUEUE         ((UINT) 3)
#define STAGE_COUNT         ((UINT) 4)

/* Number of threads that suspend on each object.  */

#define WAITERS             ((ULONG) 2)

/* How many ticks the control thread will wait for a suspension before giving
   up. Generous, because it is only ever reached when the test is failing --
   and finite, because the alternative is a run that hangs instead of one that
   reports.  */

#define WAIT_LIMIT_TICKS    ((UINT) 200)


static TX_THREAD            thread_0;
static TX_THREAD            thread_1;
static TX_THREAD            thread_2;

static TX_BLOCK_POOL        block_pool_0;
static TX_BYTE_POOL         byte_pool_0;
static TX_EVENT_FLAGS_GROUP group_0;
static TX_QUEUE             queue_0;

/* Eight blocks of 16 bytes, all of them taken before any waiter runs.  */

static UCHAR                block_pool_area[TX_TEST_BLOCK_POOL_BYTES(16, 8)];
static UCHAR                byte_pool_area[TX_TEST_BYTE_POOL_BYTES(256)];
static ULONG                queue_area[16];

/* Counts of waiters that saw TX_DELETED, one entry per stage.  */

static volatile ULONG       deleted_count[STAGE_COUNT];

static UINT                 error =  0;


static void    thread_0_entry(ULONG thread_input);
static void    waiter_entry(ULONG thread_input);


/* Define what the initial system looks like.  */

#ifdef CTEST
void test_application_define(void *first_unused_memory)
#else
void    threadx_object_delete_suspension_application_define(void *first_unused_memory)
#endif
{

UINT    status;
CHAR    *pointer;
void    *block_ptr;
void    *bytes_ptr;
UINT    i;


    /* Setup a pointer.  */
    pointer =  (CHAR *) first_unused_memory;

    for (i = 0; i < STAGE_COUNT; i++)
    {

        deleted_count[i] =  ((ULONG) 0);
    }

    /* Create the block pool and take every block, so that a waiter has nothing
       to be given and suspends instead.  */
    status =  tx_block_pool_create(&block_pool_0, "delete suspension block pool",
                                   16, block_pool_area, sizeof(block_pool_area));

    if (status != TX_SUCCESS)
    {

        printf("Running Object Delete Suspension Test............................... ERROR #1\n");
        test_control_return(1);
    }

    while (tx_block_allocate(&block_pool_0, &block_ptr, TX_NO_WAIT) == TX_SUCCESS)
    {

        /* Nothing to do with the block; the point is that the pool is empty.  */
    }

    /* Same idea for the byte pool: take enough of it that the 128-byte requests
       below cannot be met.  */
    status =  tx_byte_pool_create(&byte_pool_0, "delete suspension byte pool",
                                  byte_pool_area, sizeof(byte_pool_area));

    if (status != TX_SUCCESS)
    {

        printf("Running Object Delete Suspension Test............................... ERROR #2\n");
        test_control_return(1);
    }

    status =  tx_byte_allocate(&byte_pool_0, &bytes_ptr, 200, TX_NO_WAIT);

    if (status != TX_SUCCESS)
    {

        printf("Running Object Delete Suspension Test............................... ERROR #3\n");
        test_control_return(1);
    }

    /* An event flags group with nothing set. The waiters ask for a flag that is
       never set by anybody.  */
    status =  tx_event_flags_create(&group_0, "delete suspension group");

    if (status != TX_SUCCESS)
    {

        printf("Running Object Delete Suspension Test............................... ERROR #4\n");
        test_control_return(1);
    }

    /* An empty queue. The waiters receive from it and nobody ever sends.  */
    status =  tx_queue_create(&queue_0, "delete suspension queue", TX_1_ULONG,
                              queue_area, sizeof(queue_area));

    if (status != TX_SUCCESS)
    {

        printf("Running Object Delete Suspension Test............................... ERROR #5\n");
        test_control_return(1);
    }

    /* The control thread runs at a higher priority than the waiters, but that
       decides nothing here -- it waits on the suspended counts.  */
    status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
            pointer, TEST_STACK_SIZE_PRINTF,
            15, 15, TX_NO_TIME_SLICE, TX_AUTO_START);
    pointer =  pointer + TEST_STACK_SIZE_PRINTF;

    if (status != TX_SUCCESS)
    {

        printf("Running Object Delete Suspension Test............................... ERROR #6\n");
        test_control_return(1);
    }

    status =  tx_thread_create(&thread_1, "thread 1", waiter_entry, 1,
            pointer, TEST_STACK_SIZE_PRINTF,
            16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);
    pointer =  pointer + TEST_STACK_SIZE_PRINTF;

    if (status != TX_SUCCESS)
    {

        printf("Running Object Delete Suspension Test............................... ERROR #7\n");
        test_control_return(1);
    }

    status =  tx_thread_create(&thread_2, "thread 2", waiter_entry, 2,
            pointer, TEST_STACK_SIZE_PRINTF,
            16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);
    pointer =  pointer + TEST_STACK_SIZE_PRINTF;

    if (status != TX_SUCCESS)
    {

        printf("Running Object Delete Suspension Test............................... ERROR #8\n");
        test_control_return(1);
    }
}


/* Read the number of threads suspended on the object belonging to a stage.
   Returns TX_TRUE when the count could be read, so that a failing info_get is
   reported rather than mistaken for "nobody has suspended yet".  */

static UINT    suspended_on(UINT stage, ULONG *count)
{

UINT    status;


    /* Every destination but the suspended count is TX_NULL: each info_get
       tests its destinations individually and skips the ones it is not given,
       so there is nothing to declare here to throw away.  */
    switch (stage)
    {

    case STAGE_BLOCK_POOL:

        status =  tx_block_pool_info_get(&block_pool_0, TX_NULL, TX_NULL, TX_NULL,
                                         TX_NULL, count, TX_NULL);
        break;

    case STAGE_BYTE_POOL:

        status =  tx_byte_pool_info_get(&byte_pool_0, TX_NULL, TX_NULL, TX_NULL,
                                        TX_NULL, count, TX_NULL);
        break;

    case STAGE_EVENT_FLAGS:

        status =  tx_event_flags_info_get(&group_0, TX_NULL, TX_NULL,
                                          TX_NULL, count, TX_NULL);
        break;

    default:

        status =  tx_queue_info_get(&queue_0, TX_NULL, TX_NULL, TX_NULL,
                                    TX_NULL, count, TX_NULL);
        break;
    }

    return((status == TX_SUCCESS) ? ((UINT) TX_TRUE) : ((UINT) TX_FALSE));
}


/* Sleep until every waiter has suspended on this stage's object, or until the
   budget runs out. Returns TX_TRUE if the waiters arrived.  */

static UINT    wait_for_waiters(UINT stage)
{

UINT    ticks;
ULONG   count;


    count =  ((ULONG) 0);
    for (ticks = 0; ticks < WAIT_LIMIT_TICKS; ticks++)
    {

        if (suspended_on(stage, &count) == ((UINT) TX_FALSE))
        {

            return((UINT) TX_FALSE);
        }

        if (count == WAITERS)
        {

            return((UINT) TX_TRUE);
        }

        tx_thread_sleep(1);
    }

    return((UINT) TX_FALSE);
}


/* Sleep until every waiter has reported TX_DELETED for this stage, or until the
   budget runs out.  */

static UINT    wait_for_reports(UINT stage)
{

UINT    ticks;


    for (ticks = 0; ticks < WAIT_LIMIT_TICKS; ticks++)
    {

        if (deleted_count[stage] == WAITERS)
        {

            return((UINT) TX_TRUE);
        }

        tx_thread_sleep(1);
    }

    return((UINT) TX_FALSE);
}


static void    thread_0_entry(ULONG thread_input)
{

UINT    status;
UINT    stage;


    /* Inform user.  */
    printf("Running Object Delete Suspension Test............................... ");

    for (stage = 0; stage < STAGE_COUNT; stage++)
    {

        /* Both waiters have to be on the list before the delete, or the loop
           under test walks a shorter list than this test means it to.  */
        if (wait_for_waiters(stage) == ((UINT) TX_FALSE))
        {

            printf("ERROR #9 (stage %u)\n", stage);
            test_control_return(1);
        }

        switch (stage)
        {

        case STAGE_BLOCK_POOL:

            status =  tx_block_pool_delete(&block_pool_0);
            break;

        case STAGE_BYTE_POOL:

            status =  tx_byte_pool_delete(&byte_pool_0);
            break;

        case STAGE_EVENT_FLAGS:

            status =  tx_event_flags_delete(&group_0);
            break;

        default:

            status =  tx_queue_delete(&queue_0);
            break;
        }

        if (status != TX_SUCCESS)
        {

            printf("ERROR #10 (stage %u)\n", stage);
            test_control_return(1);
        }

        /* Every waiter must come out of the delete with TX_DELETED, which is
           the status the loop under test writes into each suspended thread.  */
        if (wait_for_reports(stage) == ((UINT) TX_FALSE))
        {

            printf("ERROR #11 (stage %u)\n", stage);
            test_control_return(1);
        }
    }

    if (error)
    {

        printf("ERROR #12\n");
        test_control_return(1);
    }
    else
    {

        /* Successful test.  */
        printf("SUCCESS!\n");
        test_control_return(0);
    }
}


static void    waiter_entry(ULONG thread_input)
{

UINT    status;
UINT    stage;
void    *block_ptr;
void    *bytes_ptr;
ULONG   actual_flags;
ULONG   message;


    for (stage = 0; stage < STAGE_COUNT; stage++)
    {

        switch (stage)
        {

        case STAGE_BLOCK_POOL:

            /* The pool was emptied before any thread ran, so this suspends.  */
            status =  tx_block_allocate(&block_pool_0, &block_ptr, TX_WAIT_FOREVER);
            break;

        case STAGE_BYTE_POOL:

            /* More than is left after the allocation made at define time.  */
            status =  tx_byte_allocate(&byte_pool_0, &bytes_ptr, 128, TX_WAIT_FOREVER);
            break;

        case STAGE_EVENT_FLAGS:

            /* Nobody ever sets this flag.  */
            status =  tx_event_flags_get(&group_0, ((ULONG) 0x1), TX_AND_CLEAR,
                                         &actual_flags, TX_WAIT_FOREVER);
            break;

        default:

            /* Nobody ever sends to this queue.  */
            status =  tx_queue_receive(&queue_0, &message, TX_WAIT_FOREVER);
            break;
        }

        if (status == TX_DELETED)
        {

            deleted_count[stage]++;
        }
        else
        {

            /* Anything else means the object was not deleted out from under
               this thread, and the control thread will say so.  */
            error++;
            return;
        }
    }
}