Class: Eldritch::Group

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeGroup

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

#abortObject

Aborts the other async calls/blocks in the group

Warning: This call will directly kill underlying threads. This isn’t very safe.

See Also:



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

#interruptObject

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.

See Also:



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_allObject



35
36
37
# File 'lib/eldritch/group.rb', line 35

def join_all
  @tasks.each &:join
end

#othersArray<Task>

Returns the other async calls/blocks in the group.

Returns:

  • (Array<Task>)

    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

Yields:



31
32
33
# File 'lib/eldritch/group.rb', line 31

def synchronize(&block)
  @mutex.synchronize { block.call }
end