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/ftrylockfile.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/stdio/ftrylockfile.c (limited to 'src/stdio/ftrylockfile.c') diff --git a/src/stdio/ftrylockfile.c b/src/stdio/ftrylockfile.c new file mode 100644 index 00000000..725b66e7 --- /dev/null +++ b/src/stdio/ftrylockfile.c @@ -0,0 +1,18 @@ +#include "stdio_impl.h" +#include "pthread_impl.h" + +int ftrylockfile(FILE *f) +{ + libc.lockfile = __lockfile; + if (f->owner && f->owner == pthread_self()->tid) { + if (f->lockcount == INT_MAX) + return -1; + f->lockcount++; + return 0; + } + if (a_swap(&f->lock, 1)) + return -1; + f->owner = pthread_self()->tid; + f->lockcount = 1; + return 0; +} -- cgit v1.2.1