82f50b421efa1a0152351c63bcb1bba90ad8fb92
[musl] / src / stdio / __lockfile.c
1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
3
4 void __lockfile(FILE *f)
5 {
6         int spins;
7         if (f->owner < 0) return;
8         if (f->owner && f->owner == __pthread_self()->tid) {
9                 while (f->lockcount == INT_MAX);
10                 f->lockcount++;
11                 return;
12         }
13         spins = 100000;
14         while (a_swap(&f->lock, 1))
15                 if (spins) spins--, a_spin();
16                 else syscall0(__NR_sched_yield);
17         f->owner = __pthread_self()->tid;
18         f->lockcount = 1;
19 }