fix race condition in file locking
[musl] / src / stdio / __lockfile.c
1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
3
4 #define MAYBE_WAITERS 0x40000000
5
6 int __lockfile(FILE *f)
7 {
8         int owner = f->lock, tid = __pthread_self()->tid;
9         if ((owner & ~MAYBE_WAITERS) == tid)
10                 return 0;
11         owner = a_cas(&f->lock, 0, tid);
12         if (!owner) return 1;
13         while ((owner = a_cas(&f->lock, 0, tid|MAYBE_WAITERS))) {
14                 if ((owner & MAYBE_WAITERS) ||
15                     a_cas(&f->lock, owner, owner|MAYBE_WAITERS)==owner)
16                         __futexwait(&f->lock, owner|MAYBE_WAITERS, 1);
17         }
18         return 1;
19 }
20
21 void __unlockfile(FILE *f)
22 {
23         if (a_swap(&f->lock, 0) & MAYBE_WAITERS)
24                 __wake(&f->lock, 1, 1);
25 }