rewrite popen to use posix_spawn instead of fragile vfork hacks
[musl] / src / stdio / ftrylockfile.c
1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
3 #include <limits.h>
4
5 int ftrylockfile(FILE *f)
6 {
7         int tid = pthread_self()->tid;
8         if (f->lock == tid) {
9                 if (f->lockcount == LONG_MAX)
10                         return -1;
11                 f->lockcount++;
12                 return 0;
13         }
14         if (f->lock < 0) f->lock = 0;
15         if (f->lock || a_cas(&f->lock, 0, tid))
16                 return -1;
17         f->lockcount = 1;
18         return 0;
19 }