diff --git a/lib/libuv/reactor.rb b/lib/libuv/reactor.rb index 6c175bd..237af0d 100644 --- a/lib/libuv/reactor.rb +++ b/lib/libuv/reactor.rb @@ -9,6 +9,7 @@ class Reactor CRITICAL = ::Mutex.new + THREAD_POOL = ::Concurrent::FixedThreadPool.new(8) module ClassMethods @@ -425,9 +426,19 @@ def on_program_interrupt(&callback) # @param callback [Proc] the callback to be called in the thread pool # @return [::Libuv::Work] # @raise [ArgumentError] if block is not given - def work(&callback) - assert_block(callback) - Work.new(@reactor, &callback) + def work + ref + d = defer + THREAD_POOL.post do + begin + d.resolve(yield) + rescue Exception => e + d.reject(e) + end + end + promise = d.promise + promise.finally { unref } + promise end # Lookup a hostname