Class: Eldritch::Group
- Inherits:
-
Object
- Object
- Eldritch::Group
- Defined in:
- lib/eldritch/group.rb
Overview
Represents a group of tasks or async calls/block. It is used to act upon all the tasks in the group.
Instance Method Summary collapse
- #<<(task) ⇒ Object
-
#abort ⇒ Object
Aborts the other async calls/blocks in the group.
-
#initialize ⇒ Group
constructor
A new instance of Group.
-
#interrupt ⇒ Object
Interrupts the other async calls/blocks in the group.
- #join_all ⇒ Object
-
#others ⇒ Array<Task>
The other async calls/blocks in the group.
-
#synchronize { ... } ⇒ Object
Yields the block in mutual exclusion with all the async calls/tasks.
Constructor Details
#initialize ⇒ Group
Returns a new instance of Group.
8 9 10 11 12 |
# File 'lib/eldritch/group.rb', line 8 def initialize @tasks = [] @mutex = ReentrantMutex.new @accept = true end |
Instance Method Details
#<<(task) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/eldritch/group.rb', line 19 def <<(task) @mutex.synchronize do if @accept @tasks << task task.start end end end |
#abort ⇒ Object
Aborts the other async calls/blocks in the group
Warning: This call will directly kill underlying threads. This isn’t very safe.
44 45 46 47 48 49 |
# File 'lib/eldritch/group.rb', line 44 def abort @mutex.synchronize do @accept = false others.each &:abort end end |
#interrupt ⇒ Object
Interrupts the other async calls/blocks in the group
Interruptions are done using exceptions that can be caught by the async calls/blocks to perform cleanup.
56 57 58 59 60 61 |
# File 'lib/eldritch/group.rb', line 56 def interrupt @mutex.synchronize do @accept = false others.each &:interrupt end end |
#join_all ⇒ Object
35 36 37 |
# File 'lib/eldritch/group.rb', line 35 def join_all @tasks.each &:join end |
#others ⇒ Array<Task>
Returns the other async calls/blocks in the group.
15 16 17 |
# File 'lib/eldritch/group.rb', line 15 def others @tasks - [Thread.current.eldritch_task] end |
#synchronize { ... } ⇒ Object
Yields the block in mutual exclusion with all the async calls/tasks
31 32 33 |
# File 'lib/eldritch/group.rb', line 31 def synchronize(&block) @mutex.synchronize { block.call } end |