|
16 | 16 | #include "test_framework.h" |
17 | 17 | #include "cbm.h" |
18 | 18 | #include "grammar_cases.h" |
| 19 | +#include "../src/foundation/compat_thread.h" |
19 | 20 |
|
20 | | -#include <pthread.h> |
21 | | -#include <sched.h> |
22 | 21 | #include <stdatomic.h> |
23 | 22 |
|
24 | 23 | static int reg_has_def_any(CBMFileResult *r, const char *name) { |
@@ -406,37 +405,48 @@ TEST(grammar_regression_all) { |
406 | 405 | } |
407 | 406 |
|
408 | 407 | typedef struct { |
409 | | - atomic_int *ready; |
| 408 | + atomic_bool *go; |
410 | 409 | atomic_int *failures; |
411 | 410 | } PropertiesParallelArgs; |
412 | 411 |
|
413 | 412 | static void *properties_parallel_worker(void *arg) { |
414 | 413 | PropertiesParallelArgs *args = arg; |
415 | | - atomic_fetch_add(args->ready, 1); |
416 | | - while (atomic_load(args->ready) != 8) |
417 | | - sched_yield(); |
| 414 | + while (!atomic_load(args->go)) {} |
418 | 415 | for (int i = 0; i < 8; i++) { |
419 | 416 | CBMFileResult *r = extract("# generated probe\nalpha=one\nbeta=two\ngamma=three\n", |
420 | 417 | CBM_LANG_PROPERTIES, "parallel", "probe.properties"); |
421 | | - if (!r) |
| 418 | + if (!r) { |
| 419 | + atomic_fetch_add(args->failures, 1); |
| 420 | + continue; |
| 421 | + } |
| 422 | + if (r->has_error || r->parse_incomplete || !reg_has_def_any(r, "alpha") || |
| 423 | + !reg_has_def_any(r, "beta") || !reg_has_def_any(r, "gamma")) |
422 | 424 | atomic_fetch_add(args->failures, 1); |
423 | 425 | cbm_free_result(r); |
424 | 426 | } |
| 427 | + cbm_destroy_thread_parser(); |
425 | 428 | return NULL; |
426 | 429 | } |
427 | 430 |
|
428 | 431 | TEST(properties_scanner_parallel_extract) { |
429 | 432 | enum { WORKERS = 8 }; |
430 | | - pthread_t threads[WORKERS]; |
431 | | - atomic_int ready = 0; |
| 433 | + cbm_thread_t threads[WORKERS]; |
| 434 | + atomic_bool go = false; |
432 | 435 | atomic_int failures = 0; |
433 | | - PropertiesParallelArgs args = {.ready = &ready, .failures = &failures}; |
434 | | - for (int i = 0; i < WORKERS; i++) |
435 | | - ASSERT_EQ(pthread_create(&threads[i], NULL, properties_parallel_worker, &args), 0); |
436 | | - while (atomic_load(&ready) != WORKERS) |
437 | | - sched_yield(); |
438 | | - for (int i = 0; i < WORKERS; i++) |
439 | | - ASSERT_EQ(pthread_join(threads[i], NULL), 0); |
| 436 | + PropertiesParallelArgs args = {.go = &go, .failures = &failures}; |
| 437 | + int started = 0; |
| 438 | + int create_error = 0; |
| 439 | + for (; started < WORKERS; started++) { |
| 440 | + create_error = cbm_thread_create(&threads[started], 0, properties_parallel_worker, &args); |
| 441 | + if (create_error != 0) |
| 442 | + break; |
| 443 | + } |
| 444 | + atomic_store(&go, true); |
| 445 | + int join_errors = 0; |
| 446 | + for (int i = 0; i < started; i++) |
| 447 | + join_errors += cbm_thread_join(&threads[i]) != 0; |
| 448 | + ASSERT_EQ(create_error, 0); |
| 449 | + ASSERT_EQ(join_errors, 0); |
440 | 450 | ASSERT_EQ(atomic_load(&failures), 0); |
441 | 451 | PASS(); |
442 | 452 | } |
|
0 commit comments