67f4b6a070ca2c1c833c8d92401e6f8ac75c251f
[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 (!libc.lockfile) libc.lockfile = __lockfile;
8         if (f->lock == tid) {
9                 if (f->lockcount == INT_MAX)
10                         return -1;
11                 f->lockcount++;
12                 return 0;
13         }
14         if (f->lock || a_cas(&f->lock, 0, tid))
15                 return -1;
16         f->lockcount = 1;
17         return 0;
18 }