#include "perl.h"
#include "XSUB.h"
+#include <fuse.h>
+
+/* Determine if threads support should be included */
#ifdef USE_ITHREADS
# ifdef I_PTHREAD
-/* perl implements threads with pthread. So, we use the pthread API for
- * handling thread-local storage. */
-# include <pthread.h>
-PerlInterpreter *master_interp = NULL;
-static inline void create_perl_context() {
- if(master_interp) {
- PerlInterpreter *me = PERL_GET_CONTEXT;
- if(!me) {
- PERL_SET_CONTEXT(master_interp);
- me = perl_clone(master_interp, CLONEf_CLONE_HOST);
- }
- }
-}
-# define FUSE_CONTEXT_PRE create_perl_context(); { dSP
-# define FUSE_CONTEXT_POST }
# define FUSE_USE_ITHREADS
# else
-# error "Sorry, I don't know how to handle ithreads on this architecture."
+# warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
# endif
- #define N_CALLBACKS 25
+#endif
+
+/* Global Data */
+
+#define MY_CXT_KEY "Fuse::_guts" XS_VERSION
++#define N_CALLBACKS 29
+
+typedef struct {
+ SV *callback[N_CALLBACKS];
+ HV *handles;
+ tTHX self;
+ int threaded;
+ perl_mutex mutex;
+} my_cxt_t;
+START_MY_CXT;
+
+#ifdef FUSE_USE_ITHREADS
+tTHX master_interp = NULL;
+
+#define CLONE_INTERP(parent) S_clone_interp(parent)
+tTHX S_clone_interp(tTHX parent) {
+ dMY_CXT_INTERP(parent);
+ if(MY_CXT.threaded) {
+ MUTEX_LOCK(&MY_CXT.mutex);
+ PERL_SET_CONTEXT(parent);
+ dTHX;
+#if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
+ tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
+#else
+ tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
+ ptr_table_free(PL_ptr_table);
+ PL_ptr_table = NULL;
+#endif
+ MUTEX_UNLOCK(&MY_CXT.mutex);
+ return child;
+ }
+ return NULL;
+}
+
+# define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
+# define FUSE_CONTEXT_POST }
#else
-# define FUSE_CONTEXT_PRE dSP
+# define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
# define FUSE_CONTEXT_POST
#endif
-#include <fuse.h>
#undef DEBUGf
#if 0
return rv;
}
- #if 0
- /*
- * This doesn't yet work... we alwas get ENOSYS when trying to use readdir().
- * Well, of course, getdir() is fine as well.
- */
- int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil, off_t off, struct fuse_file_info *fi) {
- #endif
+ int _PLfuse_opendir(const char *file, struct fuse_file_info *info) {
+ croak("opendir NOT IMPLEMENTED");
+ }
+ int _PLfuse_releasedir(const char *file, struct fuse_file_info *info) {
+ croak("releasedir NOT IMPLEMENTED");
+ }
+ int _PLfuse_fsyncdir(const char *file, struct fuse_file_info *info) {
+ croak("fsyncdir NOT IMPLEMENTED");
+ }
+
+ int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil, off_t off, struct fuse_file_info *fi) {
+ int prv, rv, offset;
+ SV *entry;
+ FUSE_CONTEXT_PRE;
+ DEBUGf("readdir begin\n");
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ XPUSHs(sv_2mortal(newSVpv(file,0)));
+ XPUSHs(sv_2mortal(newSViv(off)));
+ PUTBACK;
- prv = call_sv(_PLfuse_callbacks[26],G_ARRAY);
++ prv = call_sv(MY_CXT.callback[26],G_ARRAY);
+ SPAGAIN;
+ if(3 == prv) {
+ rv = POPi;
+ offset = POPi;
+ entry = POPs;
+ if(SvOK(entry))
+ dirfil(dirh,SvPV_nolen(entry),NULL,offset);
+ } else {
+ fprintf(stderr,"readdir() handler didn't return 2 values!\n");
+ rv = -ENOSYS;
+ }
+ FREETMPS;
+ LEAVE;
+ PUTBACK;
+ DEBUGf("readdir end: %i\n",rv);
+ FUSE_CONTEXT_POST;
+ return rv;
+ }
+
int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
int prv, rv;
FUSE_CONTEXT_PRE;
PREINIT:
struct fuse_operations fops =
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
- NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
+ NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
- int i, fd, debug, threaded;
+ int i, debug;
char *mountpoint;
char *mountopts;
struct fuse_args margs = FUSE_ARGS_INIT(0, NULL);
struct fuse_args fargs = FUSE_ARGS_INIT(0, NULL);
+ struct fuse_chan *fc;
+ dMY_CXT;
INIT:
- if(items != 29) {
+ if(items != 4+N_CALLBACKS) {
fprintf(stderr,"Perl<->C inconsistency or internal error\n");
XSRETURN_UNDEF;
}