refactor flockfile not to duplicate lock mechanism logic
[musl] / src / stdio / ftrylockfile.c
1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
3 #include <limits.h>
4
5 #define MAYBE_WAITERS 0x40000000
6
7 void __do_orphaned_stdio_locks()
8 {
9         FILE *f;
10         for (f=__pthread_self()->stdio_locks; f; f=f->next_locked)
11                 a_store(&f->lock, 0x40000000);
12 }
13
14 void __unlist_locked_file(FILE *f)
15 {
16         if (f->lockcount) {
17                 if (f->next_locked) f->next_locked->prev_locked = f->prev_locked;
18                 if (f->prev_locked) f->prev_locked->next_locked = f->next_locked;
19                 else __pthread_self()->stdio_locks = f->next_locked;
20         }
21 }
22
23 void __register_locked_file(FILE *f, pthread_t self)
24 {
25         f->lockcount = 1;
26         f->prev_locked = 0;
27         f->next_locked = self->stdio_locks;
28         if (f->next_locked) f->next_locked->prev_locked = f;
29         self->stdio_locks = f;
30 }
31
32 int ftrylockfile(FILE *f)
33 {
34         pthread_t self = __pthread_self();
35         int tid = self->tid;
36         int owner = f->lock;
37         if ((owner & ~MAYBE_WAITERS) == tid) {
38                 if (f->lockcount == LONG_MAX)
39                         return -1;
40                 f->lockcount++;
41                 return 0;
42         }
43         if (owner < 0) f->lock = owner = 0;
44         if (owner || a_cas(&f->lock, 0, tid))
45                 return -1;
46         __register_locked_file(f, self);
47         return 0;
48 }