reverse definition dependency between PAGESIZE and PAGE_SIZE
[oweals/musl.git] / src / stdio / ftrylockfile.c
1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
3 #include <limits.h>
4
5 void __do_orphaned_stdio_locks()
6 {
7         FILE *f;
8         for (f=__pthread_self()->stdio_locks; f; f=f->next_locked)
9                 a_store(&f->lock, 0x40000000);
10 }
11
12 void __unlist_locked_file(FILE *f)
13 {
14         if (f->lockcount) {
15                 if (f->next_locked) f->next_locked->prev_locked = f->prev_locked;
16                 if (f->prev_locked) f->prev_locked->next_locked = f->next_locked;
17                 else __pthread_self()->stdio_locks = f->next_locked;
18         }
19 }
20
21 int ftrylockfile(FILE *f)
22 {
23         pthread_t self = __pthread_self();
24         int tid = self->tid;
25         if (f->lock == tid) {
26                 if (f->lockcount == LONG_MAX)
27                         return -1;
28                 f->lockcount++;
29                 return 0;
30         }
31         if (f->lock < 0) f->lock = 0;
32         if (f->lock || a_cas(&f->lock, 0, tid))
33                 return -1;
34         f->lockcount = 1;
35         f->prev_locked = 0;
36         f->next_locked = self->stdio_locks;
37         if (f->next_locked) f->next_locked->prev_locked = f;
38         self->stdio_locks = f;
39         return 0;
40 }