about summary refs log tree commit diff
path: root/src/stdio/ftrylockfile.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-03-12 21:55:45 -0500
committerRich Felker <dalias@aerifal.cx>2011-03-12 21:55:45 -0500
commit5eb0d33ec0f08b123c5c10877d6258d05fa9453a (patch)
tree85ff7f5397424685d89af8176ff7ce6d6f46f6e2 /src/stdio/ftrylockfile.c
parentd8d19f4d46284d5b6124710a5235b6fe090c013f (diff)
downloadmusl-5eb0d33ec0f08b123c5c10877d6258d05fa9453a.tar.gz
musl-5eb0d33ec0f08b123c5c10877d6258d05fa9453a.tar.xz
musl-5eb0d33ec0f08b123c5c10877d6258d05fa9453a.zip
implement flockfile api, rework stdio locking
Diffstat (limited to 'src/stdio/ftrylockfile.c')
-rw-r--r--src/stdio/ftrylockfile.c18
1 files changed, 18 insertions, 0 deletions
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;
+}