Skip to content

Commit 267bf57

Browse files
author
fmz
committed
fix potential race condition in check_for_work
1 parent 718df0d commit 267bf57

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

ggml/src/ggml.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18937,7 +18937,6 @@ void ggml_release_threadpool(struct ggml_compute_threadpool* threadpool) {
1893718937
if (!threadpool->disposable) {
1893818938
ggml_mutex_lock(&threadpool->mutex);
1893918939
}
18940-
threadpool->n_threads_cur = n_threads;
1894118940
threadpool->stop = true;
1894218941
threadpool->pause = false;
1894318942
if (!threadpool->disposable) {
@@ -19230,21 +19229,27 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {
1923019229
static bool ggml_graph_compute_check_for_work(struct ggml_compute_state * state) {
1923119230
struct ggml_compute_threadpool * threadpool = state->threadpool;
1923219231

19233-
do {
19234-
if (threadpool->poll) {
19235-
while (!threadpool->new_work && !threadpool->stop && !threadpool->pause) {
19236-
// No new work. Yield and keep polling.
19237-
__cpu_relax();
19238-
}
19239-
} else {
19240-
ggml_mutex_lock_shared(&threadpool->mutex);
19241-
while (!threadpool->new_work && !threadpool->stop && !threadpool->pause) {
19242-
// No new work. Wait for the signal.
19243-
ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
19244-
}
19245-
ggml_mutex_unlock_shared(&threadpool->mutex);
19232+
if (threadpool->poll) {
19233+
while (!((threadpool->new_work && state->ith < threadpool->n_threads_cur) ||
19234+
threadpool->stop ||
19235+
threadpool->pause
19236+
)
19237+
) {
19238+
// No new work. Yield and keep polling.
19239+
__cpu_relax();
1924619240
}
19247-
} while (state->ith >= threadpool->n_threads_cur);
19241+
} else {
19242+
ggml_mutex_lock_shared(&threadpool->mutex);
19243+
while (!((threadpool->new_work && state->ith < threadpool->n_threads_cur) ||
19244+
threadpool->stop ||
19245+
threadpool->pause
19246+
)
19247+
) {
19248+
// No new work. Wait for the signal.
19249+
ggml_cond_wait(&threadpool->cond, &threadpool->mutex);
19250+
}
19251+
ggml_mutex_unlock_shared(&threadpool->mutex);
19252+
}
1924819253
return threadpool->new_work;
1924919254
}
1925019255

0 commit comments

Comments
 (0)