fix logic error in fread
[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 == INT_MAX)
9                         return -1;
10                 f->lockcount++;
11                 return 0;
12         }
13         if (f->lock || a_cas(&f->lock, 0, tid))
14                 return -1;
15         f->lockcount = 1;
16         return 0;
17 }