Skip to content

Commit 9dfa701

Browse files
committed
testing/ostest: Queue and cancel work in the critical section.
Add a work queue test: 2 * CONFIG_SMP_NCPUS threads (2 threads without SMP) queue delayed and immediate work and cancel the delayed work in a loop. Half of the threads do it in the critical section. The threads are not pinned to a CPU. The work queue enters the critical section to start its timer and to wake a worker, so this checks that the two orders do not stop the CPUs on SMP. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent 5ff9666 commit 9dfa701

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

‎testing/ostest/wqueue.c‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <unistd.h>
4242

4343
#include <nuttx/clock.h>
44+
#include <nuttx/irq.h>
4445
#include <nuttx/wqueue.h>
4546

4647
#include "ostest.h"
@@ -59,6 +60,15 @@
5960
#define MULTI_WORK_PER_QUEUE 8
6061
#define CUSTOM_PRIORITY 100
6162

63+
#ifdef CONFIG_BUILD_FLAT
64+
# define CSECTION_LOOPS 2000
65+
# ifdef CONFIG_SMP
66+
# define CSECTION_THREADS (2 * CONFIG_SMP_NCPUS)
67+
# else
68+
# define CSECTION_THREADS 2
69+
# endif
70+
#endif
71+
6272
/****************************************************************************
6373
* Private Types
6474
****************************************************************************/
@@ -567,6 +577,16 @@ struct parallel_cancel_s
567577
sem_t finished;
568578
};
569579

580+
#ifdef CONFIG_BUILD_FLAT
581+
struct csection_s
582+
{
583+
FAR struct kwork_wqueue_s *wqueue;
584+
struct work_s delayed;
585+
struct work_s immediate;
586+
bool incsection;
587+
};
588+
#endif
589+
570590
static void requeue_worker(FAR void *arg)
571591
{
572592
FAR struct requeue_s *requeue = arg;
@@ -1122,6 +1142,78 @@ static void teardown_test(void)
11221142
printf("wqueue_test: teardown done\n");
11231143
}
11241144

1145+
#ifdef CONFIG_BUILD_FLAT
1146+
static FAR void *csection_thread(FAR void *arg)
1147+
{
1148+
FAR struct csection_s *test = arg;
1149+
irqstate_t flags = 0;
1150+
int i;
1151+
1152+
for (i = 0; i < CSECTION_LOOPS; i++)
1153+
{
1154+
if (test->incsection)
1155+
{
1156+
flags = enter_critical_section();
1157+
}
1158+
1159+
/* A new earliest delayed work starts the queue timer, its cancel
1160+
* stops or moves the timer, and immediate work wakes a worker.
1161+
*/
1162+
1163+
ASSERT(work_queue_wq(test->wqueue, &test->delayed, empty_worker,
1164+
NULL, 1 + (i % 3)) == OK);
1165+
ASSERT(work_queue_wq(test->wqueue, &test->immediate, empty_worker,
1166+
NULL, 0) == OK);
1167+
work_cancel_wq(test->wqueue, &test->delayed);
1168+
1169+
if (test->incsection)
1170+
{
1171+
leave_critical_section(flags);
1172+
}
1173+
}
1174+
1175+
work_cancel_sync_wq(test->wqueue, &test->delayed);
1176+
work_cancel_sync_wq(test->wqueue, &test->immediate);
1177+
return NULL;
1178+
}
1179+
1180+
static void csection_test(void)
1181+
{
1182+
struct csection_s test[CSECTION_THREADS];
1183+
pthread_t thread[CSECTION_THREADS];
1184+
FAR struct kwork_wqueue_s *wqueue;
1185+
int i;
1186+
1187+
/* Half of the threads queue and cancel work in the critical section,
1188+
* the other half do the same outside it. If the two lock orders stop
1189+
* the CPUs, ostest hangs here: no ASSERT reports it.
1190+
*/
1191+
1192+
printf("wqueue_test: critical section\n");
1193+
wqueue = work_queue_create("test", CUSTOM_PRIORITY, NULL, STACKSIZE, 2);
1194+
ASSERT(wqueue != NULL);
1195+
1196+
memset(test, 0, sizeof(test));
1197+
for (i = 0; i < CSECTION_THREADS; i++)
1198+
{
1199+
test[i].wqueue = wqueue;
1200+
test[i].incsection = (i & 1) != 0;
1201+
ASSERT(pthread_create(&thread[i], NULL, csection_thread,
1202+
&test[i]) == OK);
1203+
}
1204+
1205+
for (i = 0; i < CSECTION_THREADS; i++)
1206+
{
1207+
ASSERT(pthread_join(thread[i], NULL) == OK);
1208+
ASSERT(work_available(&test[i].delayed));
1209+
ASSERT(work_available(&test[i].immediate));
1210+
}
1211+
1212+
ASSERT(work_queue_free(wqueue) == OK);
1213+
printf("wqueue_test: critical section done\n");
1214+
}
1215+
#endif
1216+
11251217
static FAR void *wqueue_test_entry(FAR void *arg)
11261218
{
11271219
FAR void *wq;
@@ -1184,6 +1276,9 @@ static FAR void *wqueue_test_entry(FAR void *arg)
11841276
multiple_queue_test();
11851277
self_free_test();
11861278
teardown_test();
1279+
#ifdef CONFIG_BUILD_FLAT
1280+
csection_test();
1281+
#endif
11871282

11881283
return NULL;
11891284
}

0 commit comments

Comments
 (0)