lib/ffi/libfuse/adapter/context.rb in ffi-libfuse-0.0.1.rctest12 vs lib/ffi/libfuse/adapter/context.rb in ffi-libfuse-0.1.0.rc20220550
- old
+ new
@@ -3,34 +3,36 @@
require_relative '../fuse_context'
module FFI
module Libfuse
module Adapter
- # Wrapper module to inject {FuseContext} as first arg to each callback method (except :destroy)
- #
- # {ThreadLocalContext} may be a less intrusive means to make the context available to callbacks
+ # Injects a wrapper via #{FuseCallbacks#fuse_wrappers} make the current {FuseContext} object available to
+ # callbacks (except :destroy) via thread local variable :fuse_context
module Context
# @!visibility private
def fuse_wrappers(*wrappers)
wrappers.unshift(
{
- wrapper: proc { |_fm, *args, **_, &b| self.class.context_callback(*args, &b) },
+ wrapper: proc { |_fm, *args, **_, &b| Context.thread_local_context(*args, &b) },
excludes: %i[destroy]
}
)
return wrappers unless defined?(super)
super(*wrappers)
end
module_function
- # @yieldparam [FuseContext] ctx
- # @yieldparam [Array] *args
- def context_callback(*args)
- ctx = FuseContext.get
- ctx = nil if ctx.null?
- yield ctx, *args
+ # Capture {FuseContext} in thread local variable
+ def fuse_context
+ Thread.current[:fuse_context] ||= FuseContext.get
+ end
+
+ def thread_local_context(*args)
+ yield(*args)
+ ensure
+ Thread.current[:fuse_context] = nil
end
end
end
end
end