]> git.sesse.net Git - bcachefs-tools-debian/blob - linux/preempt.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[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;
19
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);
27 }
28
29 void preempt_disable(void)
30 {
31         pthread_mutex_lock(&preempt_lock);
32 }
33
34 void preempt_enable(void)
35 {
36         pthread_mutex_unlock(&preempt_lock);
37 }