Skip to content

Commit f8dfc38

Browse files
committed
test: Fix data race in ToxScenario virtual_clock.
- Consistently use `tox_scenario_get_time` to access `virtual_clock` under `clock_mutex`. - Initialize mutexes before setting the initial clock value.
1 parent 3831392 commit f8dfc38

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

auto_tests/scenarios/framework/framework.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,16 @@ ToxScenario *tox_scenario_new(int argc, char *const argv[], uint64_t timeout_ms)
159159

160160
ToxScenario *s = (ToxScenario *)calloc(1, sizeof(ToxScenario));
161161
ck_assert(s != nullptr);
162-
s->timeout_ms = timeout_ms;
163-
s->virtual_clock = 1000;
164-
s->trace_enabled = (getenv("TOX_TRACE") != nullptr);
165-
s->event_log_enabled = (getenv("TOX_EVENT_LOG") != nullptr);
166162

167163
pthread_mutex_init(&s->mutex, nullptr);
168164
pthread_mutex_init(&s->clock_mutex, nullptr);
169165
pthread_cond_init(&s->cond_runner, nullptr);
170166
pthread_cond_init(&s->cond_nodes, nullptr);
167+
168+
s->timeout_ms = timeout_ms;
169+
s->virtual_clock = 1000;
170+
s->trace_enabled = (getenv("TOX_TRACE") != nullptr);
171+
s->event_log_enabled = (getenv("TOX_EVENT_LOG") != nullptr);
171172
return s;
172173
}
173174

@@ -660,10 +661,10 @@ ToxScenarioStatus tox_scenario_run(ToxScenario *s)
660661

661662
pthread_mutex_lock(&s->mutex);
662663

663-
uint64_t start_clock = s->virtual_clock;
664+
uint64_t start_clock = tox_scenario_get_time(s);
664665
uint64_t deadline = start_clock + s->timeout_ms;
665666

666-
while (s->num_active > 0 && s->virtual_clock < deadline) {
667+
while (s->num_active > 0 && tox_scenario_get_time(s) < deadline) {
667668
// 1. Wait until all nodes (including finished ones) have reached the barrier
668669
while (s->num_ready < s->num_nodes) {
669670
pthread_cond_wait(&s->cond_runner, &s->mutex);
@@ -700,7 +701,7 @@ ToxScenarioStatus tox_scenario_run(ToxScenario *s)
700701

701702
ToxScenarioStatus result = (s->num_active == 0) ? TOX_SCENARIO_DONE : TOX_SCENARIO_TIMEOUT;
702703
if (result == TOX_SCENARIO_TIMEOUT) {
703-
tox_scenario_log(s, "Scenario TIMEOUT after %lu ms (virtual time)", (unsigned long)(s->virtual_clock - start_clock));
704+
tox_scenario_log(s, "Scenario TIMEOUT after %lu ms (virtual time)", (unsigned long)(tox_scenario_get_time(s) - start_clock));
704705
}
705706

706707
// Stop nodes

0 commit comments

Comments
 (0)