aboutsummaryrefslogtreecommitdiffstats
path: root/lib/fuse_mt.c
diff options
context:
space:
mode:
authorMiklos Szeredi <miklos@szeredi.hu>2001-11-16 17:46:45 +0000
committerMiklos Szeredi <miklos@szeredi.hu>2001-11-16 17:46:45 +0000
commitf830a7f84dda931307785dd9653c3627c9cc6386 (patch)
tree333cb16ef36317812ee41013e6f99872bce439c3 /lib/fuse_mt.c
parent680a69a8be8b7dd9bf1beae4e48e927d0fb1cd7f (diff)
downloadlibfuse-f830a7f84dda931307785dd9653c3627c9cc6386.tar.gz
multithreading works in Python
Diffstat (limited to 'lib/fuse_mt.c')
-rw-r--r--lib/fuse_mt.c92
1 files changed, 23 insertions, 69 deletions
diff --git a/lib/fuse_mt.c b/lib/fuse_mt.c
index 6afc3bc..19cc33c 100644
--- a/lib/fuse_mt.c
+++ b/lib/fuse_mt.c
@@ -11,108 +11,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <errno.h>
#include <pthread.h>
-#include <semaphore.h>
#include <signal.h>
-#include <sys/time.h>
-struct thread_common {
+struct fuse_thr_data {
struct fuse *f;
+ void *data;
+ fuse_processor_t proc;
struct fuse_cmd *cmd;
- pthread_mutex_t lock;
- pthread_cond_t cond;
- sem_t started;
- int avail;
};
static void *do_work(void *data)
{
- struct thread_common *c = (struct thread_common *) data;
-
- pthread_mutex_lock(&c->lock);
- sem_post(&c->started);
- 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(c->f, cmd);
- pthread_mutex_lock(&c->lock);
- c->avail ++;
- }
-
- c->avail --;
- pthread_mutex_unlock(&c->lock);
+ struct fuse_thr_data *d = (struct fuse_thr_data *) data;
+ d->proc(d->f, d->cmd, d->data);
+ free(d);
return NULL;
}
-static void start_thread(struct thread_common *c)
+static void start_thread(struct fuse_thr_data *d)
{
- pthread_attr_t attr;
pthread_t thrid;
sigset_t oldset;
sigset_t newset;
int res;
- pthread_mutex_unlock(&c->lock);
-
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-
/* Disallow signal reception in worker threads */
sigfillset(&newset);
pthread_sigmask(SIG_SETMASK, &newset, &oldset);
- res = pthread_create(&thrid, &attr, do_work, c);
+ res = pthread_create(&thrid, NULL, do_work, d);
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
if(res != 0) {
fprintf(stderr, "Error creating thread: %s\n", strerror(res));
exit(1);
}
-
- sem_wait(&c->started);
- pthread_mutex_lock(&c->lock);
+ pthread_detach(thrid);
}
-void fuse_loop_mt(struct fuse *f)
+void __fuse_loop_mt(struct fuse *f, fuse_processor_t proc, void *data)
{
- 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);
- sem_init(&c->started, 0, 0);
- c->avail = 0;
-
while(1) {
+ struct fuse_thr_data *d;
struct fuse_cmd *cmd = __fuse_read_cmd(f);
if(cmd == NULL)
exit(1);
- pthread_mutex_lock(&c->lock);
- while(c->avail == 0)
- start_thread(c);
- c->cmd = cmd;
- pthread_cond_signal(&c->cond);
- pthread_mutex_unlock(&c->lock);
+ d = malloc(sizeof(struct fuse_thr_data));
+ d->proc = proc;
+ d->f = f;
+ d->cmd = cmd;
+ d->data = data;
+
+ start_thread(d);
}
}
+
+void fuse_loop_mt(struct fuse *f)
+{
+ __fuse_loop_mt(f, (fuse_processor_t) __fuse_process_cmd, NULL);
+}