correct locking in stdio functions that tried to be lock-free
[musl] / src / stdio / __lockfile.c
1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
3
4 int __lockfile(FILE *f)
5 {
6         int owner, tid = __pthread_self()->tid;
7         if (f->lock == tid)
8                 return 0;
9         while ((owner = a_cas(&f->lock, 0, tid)))
10                 __wait(&f->lock, &f->waiters, owner, 1);
11         return 1;
12 }
13
14 void __unlockfile(FILE *f)
15 {
16         a_store(&f->lock, 0);
17         if (f->waiters) __wake(&f->lock, 1, 1);
18 }