Class: Innate::State::Thread
- Inherits:
-
Object
- Object
- Innate::State::Thread
- Defined in:
- lib/innate/state/thread.rb
Overview
In case fibers are not available we fall back to this wrapper.
It will raise errors happening inside the wrapping Thread even if Thread::abort_on_exception is false.
For things that require a mutex in a threaded environment, use STATE#sync, if Fiber is available no mutex will be used.
Constant Summary collapse
- SEMAPHORE =
Mutex.new
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #defer ⇒ Object
- #sync(&block) ⇒ Object
-
#wrap ⇒ Object
Execute given block in a new Thread and rescue any exceptions before they reach Thread::new, so in case Thread::raise_on_exception is false we can still reraise the error outside of the Thread.
Instance Method Details
#[](key) ⇒ Object
16 17 18 |
# File 'lib/innate/state/thread.rb', line 16 def [](key) ::Thread.current[key] end |
#[]=(key, value) ⇒ Object
20 21 22 |
# File 'lib/innate/state/thread.rb', line 20 def []=(key, value) ::Thread.current[key] = value end |
#defer ⇒ Object
41 42 43 44 |
# File 'lib/innate/state/thread.rb', line 41 def defer a = ::Thread.current ::Thread.new{ b = ::Thread.current; a.keys.each{|k| b[k] = a[k] }; yield } end |
#sync(&block) ⇒ Object
37 38 39 |
# File 'lib/innate/state/thread.rb', line 37 def sync(&block) SEMAPHORE.synchronize(&block) end |
#wrap ⇒ Object
Execute given block in a new Thread and rescue any exceptions before they reach Thread::new, so in case Thread::raise_on_exception is false we can still reraise the error outside of the Thread.
This is not meant to be concurrent, we only use Thread as a wrapping context so we can store objects in Thread::current and access them from anywhere within this thread.
31 32 33 34 35 |
# File 'lib/innate/state/thread.rb', line 31 def wrap value = ::Thread.new{ begin; yield; rescue Exception => ex; ex; end }.value raise(value) if Exception === value return value end |