3 #include "linux/preempt.h"
6 * In userspace, pthreads are preemptible and can migrate CPUs at any time.
8 * In the kernel, preempt_disable() logic essentially guarantees that a marked
9 * critical section owns its CPU for the relevant block. This is necessary for
10 * various code paths, critically including the percpu system as it allows for
11 * non-atomic reads and writes to CPU-local data structures.
13 * The high performance userspace equivalent would be to use thread local
14 * storage to replace percpu data, but that would be complicated. It should be
15 * correct to instead guarantee mutual exclusion for the critical sections.
18 static pthread_mutex_t preempt_lock;
20 __attribute__((constructor))
21 static void preempt_init(void) {
22 pthread_mutexattr_t attr;
23 pthread_mutexattr_init(&attr);
24 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
25 pthread_mutex_init(&preempt_lock, &attr);
26 pthread_mutexattr_destroy(&attr);
29 void preempt_disable(void)
31 pthread_mutex_lock(&preempt_lock);
34 void preempt_enable(void)
36 pthread_mutex_unlock(&preempt_lock);