Skip to content

Commit 26a2ecc

Browse files
committed
fix(grammar): preserve properties scanner review fixes
1 parent badb8cc commit 26a2ecc

4 files changed

Lines changed: 36 additions & 16 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ void cbm_reset_thread_parser(void) {
391391

392392
void cbm_destroy_thread_parser(void) {
393393
// Full cleanup: delete the parser. Call on worker thread exit.
394+
cbm_destroy_thread_kind_cache();
394395
if (tl_parser) {
395396
ts_parser_delete(tl_parser);
396397
tl_parser = NULL;

‎internal/cbm/helpers.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,13 @@ typedef struct {
500500
enum { KS_SLOTS = 512, KS_SLOT_MASK = 511, KS_PROBE = 8 };
501501
static CBM_TLS ks_slot_t ks_cache[KS_SLOTS];
502502

503+
void cbm_destroy_thread_kind_cache(void) {
504+
for (int i = 0; i < KS_SLOTS; i++) {
505+
free(ks_cache[i].bits);
506+
memset(&ks_cache[i], 0, sizeof(ks_cache[i]));
507+
}
508+
}
509+
503510
static ks_slot_t *ks_build(const TSLanguage *lang, const char *const *types, ks_slot_t *s) {
504511
s->lang = lang;
505512
s->types = types;

‎internal/cbm/helpers.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "cbm.h"
55

6+
void cbm_destroy_thread_kind_cache(void);
7+
68
// Portable memmem: find first occurrence of `needle` (needle_len bytes) within
79
// `haystack` (haystack_len bytes). Returns a pointer into haystack, or NULL.
810
// Hand-rolled so it compiles identically on all platforms (GNU/BSD-only

‎tests/test_grammar_regression.c‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
#include "test_framework.h"
1717
#include "cbm.h"
1818
#include "grammar_cases.h"
19+
#include "../src/foundation/compat_thread.h"
1920

20-
#include <pthread.h>
21-
#include <sched.h>
2221
#include <stdatomic.h>
2322

2423
static int reg_has_def_any(CBMFileResult *r, const char *name) {
@@ -406,37 +405,48 @@ TEST(grammar_regression_all) {
406405
}
407406

408407
typedef struct {
409-
atomic_int *ready;
408+
atomic_bool *go;
410409
atomic_int *failures;
411410
} PropertiesParallelArgs;
412411

413412
static void *properties_parallel_worker(void *arg) {
414413
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)) {}
418415
for (int i = 0; i < 8; i++) {
419416
CBMFileResult *r = extract("# generated probe\nalpha=one\nbeta=two\ngamma=three\n",
420417
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"))
422424
atomic_fetch_add(args->failures, 1);
423425
cbm_free_result(r);
424426
}
427+
cbm_destroy_thread_parser();
425428
return NULL;
426429
}
427430

428431
TEST(properties_scanner_parallel_extract) {
429432
enum { WORKERS = 8 };
430-
pthread_t threads[WORKERS];
431-
atomic_int ready = 0;
433+
cbm_thread_t threads[WORKERS];
434+
atomic_bool go = false;
432435
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);
440450
ASSERT_EQ(atomic_load(&failures), 0);
441451
PASS();
442452
}

0 commit comments

Comments
 (0)