better thread management
diff --git a/lib/Makefile.am b/lib/Makefile.am
index f970b11..7f289f7 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -4,4 +4,5 @@
libfuse_a_SOURCES = \
fuse.c \
+ fuse_mt.c \
fuse_i.h
diff --git a/lib/fuse.c b/lib/fuse.c
index ed352c6..6bd32ae 100644
--- a/lib/fuse.c
+++ b/lib/fuse.c
@@ -301,6 +301,7 @@
attr->atime = stbuf->st_atime;
attr->mtime = stbuf->st_mtime;
attr->ctime = stbuf->st_ctime;
+ attr->_dummy = 4096;
}
static int fill_dir(struct fuse_dirhandle *dh, char *name, int type)
@@ -745,19 +746,11 @@
send_reply(f, in, res, NULL, 0);
}
-struct cmd {
- struct fuse *f;
- char *buf;
- size_t buflen;
-};
-
-static void *do_command(void *data)
+void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
{
- struct cmd *cmd = (struct cmd *) data;
struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
void *inarg = cmd->buf + sizeof(struct fuse_in_header);
size_t argsize;
- struct fuse *f = cmd->f;
if((f->flags & FUSE_DEBUG)) {
printf("unique: %i, opcode: %i, ino: %li, insize: %i\n", in->unique,
@@ -836,66 +829,46 @@
if(in->unique != 0)
send_reply(f, in, -ENOSYS, NULL, 0);
}
-
+
free(cmd->buf);
free(cmd);
-
- return NULL;
}
-/* This hack makes it possible to link FUSE with or without the
- pthread library */
-__attribute__((weak))
-int pthread_create(pthread_t *thrid __attribute__((unused)),
- const pthread_attr_t *attr __attribute__((unused)),
- void *(*func)(void *) __attribute__((unused)),
- void *arg __attribute__((unused)))
+struct fuse_cmd *__fuse_read_cmd(struct fuse *f)
{
- return ENOSYS;
+ ssize_t res;
+ char inbuf[FUSE_MAX_IN];
+ struct fuse_cmd *cmd;
+
+ res = read(f->fd, inbuf, sizeof(inbuf));
+ if(res == -1) {
+ perror("reading fuse device");
+ /* BAD... This will happen again */
+ return NULL;
+ }
+ if((size_t) res < sizeof(struct fuse_in_header)) {
+ fprintf(stderr, "short read on fuse device\n");
+ /* Cannot happen */
+ return NULL;
+ }
+
+ cmd = (struct fuse_cmd *) malloc(sizeof(*cmd));
+ cmd->buflen = res;
+ cmd->buf = (char *) malloc(cmd->buflen);
+ memcpy(cmd->buf, inbuf, cmd->buflen);
+
+ return cmd;
}
+
void fuse_loop(struct fuse *f)
{
- int res;
- char inbuf[FUSE_MAX_IN];
- pthread_attr_t attr;
- pthread_t thrid;
-
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
while(1) {
- struct cmd *cmd;
-
- res = read(f->fd, inbuf, sizeof(inbuf));
- if(res == -1) {
- perror("reading fuse device");
- /* BAD... This will happen again */
+ struct fuse_cmd *cmd = __fuse_read_cmd(f);
+ if(cmd == NULL)
exit(1);
- }
- if((size_t) res < sizeof(struct fuse_in_header)) {
- fprintf(stderr, "short read on fuse device\n");
- /* Cannot happen */
- exit(1);
- }
- cmd = (struct cmd *) malloc(sizeof(struct cmd));
- cmd->f = f;
- cmd->buflen = res;
- cmd->buf = (char *) malloc(cmd->buflen);
- memcpy(cmd->buf, inbuf, cmd->buflen);
-
- if(f->flags & FUSE_MULTITHREAD) {
- res = pthread_create(&thrid, &attr, do_command, cmd);
- if(res == 0)
- continue;
-
- fprintf(stderr, "Error creating thread: %s\n", strerror(res));
- fprintf(stderr, "Will run in single thread mode\n");
- f->flags &= ~FUSE_MULTITHREAD;
- }
-
- do_command(cmd);
+ __fuse_process_cmd(f, cmd);
}
}
@@ -909,6 +882,7 @@
f->flags = flags;
f->fd = fd;
f->ctr = 0;
+ /* FIXME: Dynamic hash table */
f->name_table_size = 14057;
f->name_table = (struct node **)
calloc(1, sizeof(struct node *) * f->name_table_size);
@@ -934,6 +908,7 @@
void fuse_destroy(struct fuse *f)
{
+ /* FIXME: Kill all threads... */
size_t i;
for(i = 0; i < f->ino_table_size; i++) {
struct node *node;
diff --git a/lib/fuse_i.h b/lib/fuse_i.h
index 4d3e042..6740608 100644
--- a/lib/fuse_i.h
+++ b/lib/fuse_i.h
@@ -40,3 +40,9 @@
fino_t dir;
FILE *fp;
};
+
+struct fuse_cmd {
+ struct fuse *f;
+ char *buf;
+ size_t buflen;
+};
diff --git a/lib/fuse_mt.c b/lib/fuse_mt.c
new file mode 100644
index 0000000..ac616fe
--- /dev/null
+++ b/lib/fuse_mt.c
@@ -0,0 +1,111 @@
+/*
+ FUSE: Filesystem in Userspace
+ Copyright (C) 2001 Miklos Szeredi (mszeredi@inf.bme.hu)
+
+ This program can be distributed under the terms of the GNU GPL.
+ See the file COPYING.
+*/
+
+#include "fuse.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <pthread.h>
+#include <signal.h>
+#include <sys/time.h>
+
+
+struct thread_common {
+ struct fuse *f;
+ struct fuse_cmd *cmd;
+ pthread_mutex_t lock;
+ pthread_cond_t cond;
+ int avail;
+};
+
+/* Called with c->lock held */
+static void *do_work(void *data)
+{
+ struct thread_common *c = (struct thread_common *) data;
+ struct fuse *f = c->f;
+
+ c->avail ++;
+ while(1) {
+ int res;
+ struct timespec timeout;
+ struct timeval now;
+ struct fuse_cmd *cmd;
+
+ gettimeofday(&now, NULL);
+ timeout.tv_sec = now.tv_sec + 1;
+ timeout.tv_nsec = now.tv_usec * 1000;
+
+ res = 0;
+ while(c->cmd == NULL && res != ETIMEDOUT)
+ res = pthread_cond_timedwait(&c->cond, &c->lock, &timeout);
+ if(res == ETIMEDOUT)
+ break;
+
+ cmd = c->cmd;
+ c->cmd = NULL;
+ c->avail --;
+ pthread_mutex_unlock(&c->lock);
+ __fuse_process_cmd(f, cmd);
+ pthread_mutex_lock(&c->lock);
+ c->avail ++;
+ }
+
+ c->avail --;
+ pthread_mutex_unlock(&c->lock);
+ return NULL;
+}
+
+static void start_thread(struct thread_common *c)
+{
+ pthread_attr_t attr;
+ pthread_t thrid;
+ sigset_t oldset;
+ sigset_t newset;
+ int res;
+
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+ /* Disallow signal reception in worker threads */
+ sigfillset(&newset);
+ sigprocmask(SIG_SETMASK, &newset, &oldset);
+ res = pthread_create(&thrid, &attr, do_work, c);
+ sigprocmask(SIG_SETMASK, &oldset, NULL);
+ pthread_mutex_lock(&c->lock);
+ if(res != 0) {
+ fprintf(stderr, "Error creating thread: %s\n", strerror(res));
+ exit(1);
+ }
+}
+
+void fuse_loop_mt(struct fuse *f)
+{
+ struct thread_common *c;
+
+ c = (struct thread_common *) malloc(sizeof(struct thread_common));
+ c->f = f;
+ c->cmd = NULL;
+ pthread_cond_init(&c->cond, NULL);
+ pthread_mutex_init(&c->lock, NULL);
+ c->avail = 0;
+
+ while(1) {
+ struct fuse_cmd *cmd = __fuse_read_cmd(f);
+ if(cmd == NULL)
+ exit(1);
+
+ pthread_mutex_lock(&c->lock);
+ c->cmd = cmd;
+ while(c->avail == 0)
+ start_thread(c);
+ pthread_cond_signal(&c->cond);
+ pthread_mutex_unlock(&c->lock);
+ }
+}