@@ -3168,16 +3168,32 @@ struct ggml_state {
31683168
31693169// global state
31703170static struct ggml_state g_state;
3171- static atomic_flag g_state_critical = ATOMIC_FLAG_INIT;
31723171
3172+ #if !defined(_MSC_VER)
3173+ // critical section via pthread mutex
3174+ static pthread_mutex_t g_state_mutex = PTHREAD_MUTEX_INITIALIZER;
3175+ static void ggml_critical_section_start(void) {
3176+     pthread_mutex_lock(&g_state_mutex);
3177+ }
3178+ 
3179+ static void ggml_critical_section_end(void) {
3180+     pthread_mutex_unlock(&g_state_mutex);
3181+ }
3182+ #else
31733183// critical section via spin lock
3174- inline static void ggml_critical_section_start(void) {
3184+ static atomic_flag g_state_critical = ATOMIC_FLAG_INIT;
3185+ static void ggml_critical_section_start(void) {
31753186    while (atomic_flag_test_and_set(&g_state_critical)) {
31763187        // spin
31773188        sched_yield();
31783189    }
31793190}
31803191
3192+ static void ggml_critical_section_end(void) {
3193+     atomic_flag_clear(&g_state_critical);
3194+ }
3195+ #endif
3196+ 
31813197#ifdef GGML_USE_OPENMP
31823198static void ggml_barrier(struct ggml_threadpool * threadpool) {
31833199    if (threadpool->n_threads_cur == 1) {
@@ -3214,12 +3230,6 @@ static void ggml_barrier(struct ggml_threadpool * threadpool) {
32143230}
32153231#endif
32163232
3217- // TODO: make this somehow automatically executed
3218- //       some sort of "sentry" mechanism
3219- inline static void ggml_critical_section_end(void) {
3220-     atomic_flag_clear(&g_state_critical);
3221- }
3222- 
32233233#if defined(__gnu_linux__)
32243234static cpu_set_t ggml_get_numa_affinity(void) {
32253235    cpu_set_t cpuset;
0 commit comments