|
41 | 41 | #include <unistd.h> |
42 | 42 |
|
43 | 43 | #include <nuttx/clock.h> |
| 44 | +#include <nuttx/irq.h> |
44 | 45 | #include <nuttx/wqueue.h> |
45 | 46 |
|
46 | 47 | #include "ostest.h" |
|
59 | 60 | #define MULTI_WORK_PER_QUEUE 8 |
60 | 61 | #define CUSTOM_PRIORITY 100 |
61 | 62 |
|
| 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 | + |
62 | 72 | /**************************************************************************** |
63 | 73 | * Private Types |
64 | 74 | ****************************************************************************/ |
@@ -567,6 +577,16 @@ struct parallel_cancel_s |
567 | 577 | sem_t finished; |
568 | 578 | }; |
569 | 579 |
|
| 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 | + |
570 | 590 | static void requeue_worker(FAR void *arg) |
571 | 591 | { |
572 | 592 | FAR struct requeue_s *requeue = arg; |
@@ -1122,6 +1142,78 @@ static void teardown_test(void) |
1122 | 1142 | printf("wqueue_test: teardown done\n"); |
1123 | 1143 | } |
1124 | 1144 |
|
| 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 | + |
1125 | 1217 | static FAR void *wqueue_test_entry(FAR void *arg) |
1126 | 1218 | { |
1127 | 1219 | FAR void *wq; |
@@ -1184,6 +1276,9 @@ static FAR void *wqueue_test_entry(FAR void *arg) |
1184 | 1276 | multiple_queue_test(); |
1185 | 1277 | self_free_test(); |
1186 | 1278 | teardown_test(); |
| 1279 | +#ifdef CONFIG_BUILD_FLAT |
| 1280 | + csection_test(); |
| 1281 | +#endif |
1187 | 1282 |
|
1188 | 1283 | return NULL; |
1189 | 1284 | } |
|
0 commit comments