implement flockfile api, rework stdio locking
authorRich Felker <dalias@aerifal.cx>
Sun, 13 Mar 2011 02:55:45 +0000 (21:55 -0500)
committerRich Felker <dalias@aerifal.cx>
Sun, 13 Mar 2011 02:55:45 +0000 (21:55 -0500)
src/internal/libc.h
src/internal/stdio_impl.h
src/stdio/__lockfile.c [new file with mode: 0644]
src/stdio/__ofl.c [deleted file]
src/stdio/flockfile.c [new file with mode: 0644]
src/stdio/ftrylockfile.c [new file with mode: 0644]
src/stdio/funlockfile.c [new file with mode: 0644]
src/stdio/vsnprintf.c
src/thread/pthread_create.c

index e81ef76..3d09bf6 100644 (file)
@@ -15,6 +15,9 @@ struct __libc {
        int (*rsyscall)(int, long, long, long, long, long, long);
        void (**tsd_keys)(void *);
        void (*fork_handler)(int);
+       FILE *ofl_head;
+       int ofl_lock;
+       void (*lockfile)(FILE *);
 };
 
 
@@ -36,6 +39,7 @@ extern struct __libc *__libc_loc(void) __attribute__((const));
 
 /* Designed to avoid any overhead in non-threaded processes */
 void __lock(volatile int *);
+void __lockfile(FILE *);
 #define LOCK(x) (libc.threads_minus_1 ? (__lock(x),1) : ((void)(x),1))
 #define UNLOCK(x) (*(x)=0)
 #define CANCELPT(x) (libc.cancelpt ? libc.cancelpt((x)),0 : (void)(x),0)
index 1e9159f..13e5cfd 100644 (file)
@@ -23,8 +23,8 @@
 
 #define UNGET 4
 
-#define FLOCK(f) LOCK(&f->lock)
-#define FUNLOCK(f) UNLOCK(&f->lock)
+#define FLOCK(f) ((libc.lockfile && (f)->owner>=0) ? (libc.lockfile((f)),0) : 0)
+#define FUNLOCK(f) ((f)->lockcount && (--(f)->lockcount || ((f)->owner=(f)->lock=0)))
 
 #define F_PERM 1
 #define F_NORD 4
@@ -50,7 +50,7 @@ struct __FILE_s {
        signed char lbf;
        int lock;
        int lockcount;
-       void *owner;
+       void *dummy5;
        off_t off;
        int (*flush)(FILE *);
        void **wide_data; /* must be NULL */
@@ -59,6 +59,7 @@ struct __FILE_s {
        off_t (*seek)(FILE *, off_t, int);
        int mode;
        int (*close)(FILE *);
+       int owner;
 };
 
 size_t __stdio_read(FILE *, unsigned char *, size_t);
@@ -80,15 +81,9 @@ int __putc_unlocked(int, FILE *);
 
 FILE *__fdopen(int, const char *);
 
-extern struct ofl
-{
-       FILE *head;
-       int lock;
-} __ofl;
-
-#define OFLLOCK() LOCK(&__ofl.lock)
-#define OFLUNLOCK() UNLOCK(&__ofl.lock)
-#define ofl_head (__ofl.head)
+#define OFLLOCK() LOCK(&libc.ofl_lock)
+#define OFLUNLOCK() UNLOCK(&libc.ofl_lock)
+#define ofl_head (libc.ofl_head)
 
 #define feof(f) ((f)->flags & F_EOF)
 #define ferror(f) ((f)->flags & F_ERR)
diff --git a/src/stdio/__lockfile.c b/src/stdio/__lockfile.c
new file mode 100644 (file)
index 0000000..82f50b4
--- /dev/null
@@ -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;
+}
diff --git a/src/stdio/__ofl.c b/src/stdio/__ofl.c
deleted file mode 100644 (file)
index 7d9652c..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-#include "stdio_impl.h"
-
-struct ofl __ofl;
diff --git a/src/stdio/flockfile.c b/src/stdio/flockfile.c
new file mode 100644 (file)
index 0000000..1b6ef58
--- /dev/null
@@ -0,0 +1,9 @@
+#include "stdio_impl.h"
+#include "pthread_impl.h"
+
+void flockfile(FILE *f)
+{
+       pthread_self();
+       libc.lockfile = __lockfile;
+       __lockfile(f);
+}
diff --git a/src/stdio/ftrylockfile.c b/src/stdio/ftrylockfile.c
new file mode 100644 (file)
index 0000000..725b66e
--- /dev/null
@@ -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;
+}
diff --git a/src/stdio/funlockfile.c b/src/stdio/funlockfile.c
new file mode 100644 (file)
index 0000000..d69f68e
--- /dev/null
@@ -0,0 +1,7 @@
+#include "stdio_impl.h"
+#include "pthread_impl.h"
+
+void funlockfile(FILE *f)
+{
+       FUNLOCK(f);
+}
index bda6b49..ff792e1 100644 (file)
@@ -17,6 +17,7 @@ int vsnprintf(char *s, size_t n, const char *fmt, va_list ap)
        f.write = sn_write;
        f.buf_size = 1;
        f.buf = buf;
+       f.owner = -1;
        if (n > INT_MAX) {
                errno = EOVERFLOW;
                return -1;
index d829fa2..9b6385f 100644 (file)
@@ -150,6 +150,7 @@ static void init_threads()
 {
        struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
        libc.lock = __lock;
+       libc.lockfile = __lockfile;
        libc.cancelpt = cancelpt;
        libc.rsyscall = rsyscall;
        sa.sa_sigaction = cancel_handler;