add proper fuxed-based locking for stdio
[musl] / src / stdio / ftrylockfile.c
1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
3
4 int ftrylockfile(FILE *f)
5 {
6         int tid = pthread_self()->tid;
7         if (f->lock == tid) {
8                 if (f->lockcount == LONG_MAX)
9                         return -1;
10                 f->lockcount++;
11                 return 0;
12         }
13         if (f->lock < 0) f->lock = 0;
14         if (f->lock || a_cas(&f->lock, 0, tid))
15                 return -1;
16         f->lockcount = 1;
17         return 0;
18 }