add PTHREAD_NULL
[musl.git] / src / stdio / __lockfile.c
blob0f60a14990943605b12038178c595c7210cc0f73
1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
4 int __lockfile(FILE *f)
6 int owner = f->lock, tid = __pthread_self()->tid;
7 if ((owner & ~MAYBE_WAITERS) == tid)
8 return 0;
9 owner = a_cas(&f->lock, 0, tid);
10 if (!owner) return 1;
11 while ((owner = a_cas(&f->lock, 0, tid|MAYBE_WAITERS))) {
12 if ((owner & MAYBE_WAITERS) ||
13 a_cas(&f->lock, owner, owner|MAYBE_WAITERS)==owner)
14 __futexwait(&f->lock, owner|MAYBE_WAITERS, 1);
16 return 1;
19 void __unlockfile(FILE *f)
21 if (a_swap(&f->lock, 0) & MAYBE_WAITERS)
22 __wake(&f->lock, 1, 1);