]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/preempt.c
Change preempt_disable() etc. to use a recursive mutex.
[bcachefs-tools-debian] / linux / preempt.c
1 #include <pthread.h>
2
3 #include "linux/preempt.h"
4
5 /*
6  * In userspace, pthreads are preemptible and can migrate CPUs at any time.
7  *
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.
12  *
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.
16  */
17
18 static pthread_mutex_t preempt_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
19
20 void preempt_disable(void)
21 {
22         pthread_mutex_lock(&preempt_lock);
23 }
24
25 void preempt_enable(void)
26 {
27         pthread_mutex_unlock(&preempt_lock);
28 }