File size: 555 Bytes
56fa795
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
// Keep-awake helper for run.py: a user-interactive thread that spins (GIL released, called through ctypes) while
// requests are being served. Without it macOS lowers the CPU cluster clock while the encode thread waits on the
// GPU; MLX then encodes slower, the GPU starves and also clocks down, and per-request latency drifts 5 -> 15 ms.
#include <pthread.h>
#include <pthread/qos.h>

void spin_while(volatile int *flag) {
    pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0);
    while (*flag) {
        __asm__ volatile("yield");
    }
}