From 5eb0d33ec0f08b123c5c10877d6258d05fa9453a Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sat, 12 Mar 2011 21:55:45 -0500 Subject: implement flockfile api, rework stdio locking --- src/stdio/__lockfile.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/stdio/__lockfile.c (limited to 'src/stdio/__lockfile.c') diff --git a/src/stdio/__lockfile.c b/src/stdio/__lockfile.c new file mode 100644 index 00000000..82f50b42 --- /dev/null +++ b/src/stdio/__lockfile.c @@ -0,0 +1,19 @@ +#include "stdio_impl.h" +#include "pthread_impl.h" + +void __lockfile(FILE *f) +{ + int spins; + if (f->owner < 0) return; + if (f->owner && f->owner == __pthread_self()->tid) { + while (f->lockcount == INT_MAX); + f->lockcount++; + return; + } + spins = 100000; + while (a_swap(&f->lock, 1)) + if (spins) spins--, a_spin(); + else syscall0(__NR_sched_yield); + f->owner = __pthread_self()->tid; + f->lockcount = 1; +} -- cgit v1.2.1