From 7a87f2cea251b7035c364fb180bc0d9fec318aa6 Mon Sep 17 00:00:00 2001 From: Raymond Hammarling Date: Thu, 6 Aug 2015 19:24:38 +0200 Subject: [PATCH] abstract threads, mutexes --- abstractions.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ abstractions.h | 37 +++++++++++++++++++++++++++++++++++ binding.gyp | 2 +- fuse-bindings.cc | 40 +++++++++++++------------------------- 4 files changed, 101 insertions(+), 28 deletions(-) create mode 100644 abstractions.cc diff --git a/abstractions.cc b/abstractions.cc new file mode 100644 index 0000000..8312474 --- /dev/null +++ b/abstractions.cc @@ -0,0 +1,50 @@ +#include "abstractions.h" + +#include +#include + +#ifdef __APPLE__ + +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +void thread_create (thread_t* thread, thread_fn fn, void* data) { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_create(thread, &attr, fn, data); +} + +void thread_join (thread_t thread) { + pthread_join(thread, NULL); +} + +void fusermount (char *path) { + char *argv[] = {(char *) "umount", path, NULL}; + + pid_t cpid = vfork(); + if (cpid > 0) waitpid(cpid, NULL, 0); + else execvp(argv[0], argv); +} + +#else + +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +void thread_create (thread_t* thread, thread_fn fn, void* data) { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_create(thread, &attr, fn, data); +} + +void thread_join (thread_t thread) { + pthread_join(thread, NULL); +} + +void fusermount (char *path) { + char *argv[] = {(char *) "fusermount", (char *) "-q", (char *) "-u", path, NULL}; + + pid_t cpid = vfork(); + if (cpid > 0) waitpid(cpid, NULL, 0); + else execvp(argv[0], argv); +} + +#endif diff --git a/abstractions.h b/abstractions.h index eb5ea63..64ff076 100644 --- a/abstractions.h +++ b/abstractions.h @@ -1,7 +1,12 @@ +#include + +typedef void*(*thread_fn)(void*); + #ifdef __APPLE__ // OS X #include +#include typedef dispatch_semaphore_t bindings_sem_t; @@ -18,6 +23,21 @@ NAN_INLINE static void semaphore_signal (dispatch_semaphore_t *sem) { dispatch_semaphore_signal(*sem); } +extern pthread_mutex_t mutex; + +NAN_INLINE static void mutex_lock (pthread_mutex_t *mutex) { + pthread_mutex_lock(mutex); +} + +NAN_INLINE static void mutex_unlock (pthread_mutex_t *mutex) { + pthread_mutex_unlock(mutex); +} + +typedef pthread_t thread_t; + +void thread_create (thread_t*, thread_fn, void*); +void thread_join (thread_t); + #else // Linux and whatnot @@ -37,4 +57,21 @@ NAN_INLINE static void semaphore_signal (sem_t *sem) { sem_post(sem); } +extern pthread_mutex_t mutex; + +NAN_INLINE static void mutex_lock (pthread_mutex_t *mutex) { + pthread_mutex_lock(mutex); +} + +NAN_INLINE static void mutex_unlock (pthread_mutex_t *mutex) { + pthread_mutex_unlock(mutex); +} + +typedef pthread_t thread_t; + +void thread_create (thread_t*, thread_fn, void*); +void thread_join (thread_t); + +void fusermount (char*); + #endif diff --git a/binding.gyp b/binding.gyp index 222dac9..4f1ed49 100644 --- a/binding.gyp +++ b/binding.gyp @@ -2,7 +2,7 @@ "targets": [ { "target_name": "fuse_bindings", - "sources": [ "fuse-bindings.cc" ], + "sources": [ "fuse-bindings.cc", "abstractions.cc" ], "include_dirs": [ " #include #include + #include #include #include #include -#include #include #include -#include -#ifdef __APPLE__ -#include -#endif #include "abstractions.h" @@ -57,7 +53,6 @@ enum bindings_ops_t { OP_DESTROY }; -static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static Persistent buffer_constructor; static NanCallback *callback_constructor; static struct stat empty_stat; @@ -74,7 +69,7 @@ struct bindings_t { // fuse data char mnt[1024]; char mntopts[1024]; - pthread_t thread; + thread_t thread; bindings_sem_t semaphore; uv_async_t async; @@ -146,23 +141,16 @@ static bindings_t *bindings_find_mounted (char *path) { } static void bindings_fusermount (char *path) { -#ifdef __APPLE__ - char *argv[] = {(char *) "umount", path, NULL}; -#else - char *argv[] = {(char *) "fusermount", (char *) "-q", (char *) "-u", path, NULL}; -#endif - pid_t cpid = vfork(); - if (cpid > 0) waitpid(cpid, NULL, 0); - else execvp(argv[0], argv); + fusermount(path); } static void bindings_unmount (char *path) { - pthread_mutex_lock(&mutex); + mutex_lock(&mutex); bindings_t *b = bindings_find_mounted(path); if (b != NULL) b->gc = 1; bindings_fusermount(path); - if (b != NULL) pthread_join(b->thread, NULL); - pthread_mutex_unlock(&mutex); + if (b != NULL) thread_join(b->thread); + mutex_unlock(&mutex); } @@ -607,9 +595,9 @@ static void bindings_free (bindings_t *b) { } static void bindings_on_close (uv_handle_t *handle) { - pthread_mutex_lock(&mutex); + mutex_lock(&mutex); bindings_free((bindings_t *) handle->data); - pthread_mutex_unlock(&mutex); + mutex_unlock(&mutex); } static void *bindings_thread (void *data) { @@ -1089,15 +1077,15 @@ NAN_METHOD(Mount) { if (!args[0]->IsString()) return NanThrowError("mnt must be a string"); - pthread_mutex_lock(&mutex); + mutex_lock(&mutex); int index = bindings_alloc(); - pthread_mutex_unlock(&mutex); + mutex_unlock(&mutex); if (index == -1) return NanThrowError("You cannot mount more than 1024 filesystem in one process"); - pthread_mutex_lock(&mutex); + mutex_lock(&mutex); bindings_t *b = bindings_mounted[index]; - pthread_mutex_unlock(&mutex); + mutex_unlock(&mutex); memset(&empty_stat, 0, sizeof(empty_stat)); memset(b, 0, sizeof(bindings_t)); @@ -1158,9 +1146,7 @@ NAN_METHOD(Mount) { uv_async_init(uv_default_loop(), &(b->async), (uv_async_cb) bindings_dispatch); b->async.data = b; - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_create(&(b->thread), &attr, bindings_thread, b); + thread_create(&(b->thread), bindings_thread, b); NanReturnUndefined(); }