ggml : use atomic_flag for critical section
This commit is contained in:
parent
6bd12ce409
commit
45979b05f4
1 changed files with 5 additions and 9 deletions
14
ggml.c
14
ggml.c
|
@ -2883,24 +2883,20 @@ struct ggml_state {
|
||||||
|
|
||||||
// global state
|
// global state
|
||||||
static struct ggml_state g_state;
|
static struct ggml_state g_state;
|
||||||
static atomic_int g_state_barrier = 0;
|
static atomic_flag g_state_critical = ATOMIC_FLAG_INIT;
|
||||||
|
|
||||||
// barrier via spin lock
|
// barrier via spin lock
|
||||||
inline static void ggml_critical_section_start(void) {
|
inline static void ggml_critical_section_start(void) {
|
||||||
int processing = atomic_fetch_add(&g_state_barrier, 1);
|
while (atomic_flag_test_and_set(&g_state_critical)) {
|
||||||
|
// spin
|
||||||
while (processing > 0) {
|
sched_yield();
|
||||||
// wait for other threads to finish
|
|
||||||
atomic_fetch_sub(&g_state_barrier, 1);
|
|
||||||
sched_yield(); // TODO: reconsider this
|
|
||||||
processing = atomic_fetch_add(&g_state_barrier, 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: make this somehow automatically executed
|
// TODO: make this somehow automatically executed
|
||||||
// some sort of "sentry" mechanism
|
// some sort of "sentry" mechanism
|
||||||
inline static void ggml_critical_section_end(void) {
|
inline static void ggml_critical_section_end(void) {
|
||||||
atomic_fetch_sub(&g_state_barrier, 1);
|
atomic_flag_clear(&g_state_critical);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__gnu_linux__)
|
#if defined(__gnu_linux__)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue