1d0a1ff8662baad6ce8593348c4179530b70ca4e
[musl] / src / stdio / ftrylockfile.c
1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
3
4 int ftrylockfile(FILE *f)
5 {
6         if (!libc.lockfile) libc.lockfile = __lockfile;
7         if (f->owner && f->owner == pthread_self()->tid) {
8                 if (f->lockcount == INT_MAX)
9                         return -1;
10                 f->lockcount++;
11                 return 0;
12         }
13         if (a_swap(&f->lock, 1))
14                 return -1;
15         f->owner = pthread_self()->tid;
16         f->lockcount = 1;
17         return 0;
18 }