Class: Origen::Generator::PatternSequence

Inherits:
Object
  • Object
show all
Defined in:
lib/origen/generator/pattern_sequence.rb

Overview

Manages a single pattern sequence, i.e. an instance of PatternSequence is created for every Pattern.sequence do … end block

Instance Method Summary collapse

Constructor Details

#initialize(name, block) ⇒ PatternSequence

Returns a new instance of PatternSequence



8
9
10
11
12
13
14
15
16
17
# File 'lib/origen/generator/pattern_sequence.rb', line 8

def initialize(name, block)
  @number_of_threads = 1
  @name = name
  @running_thread_ids = { main: true }
  # The contents of the main Pattern.sequence block will be executed as a thread and treated
  # like any other parallel block
  thread = PatternThread.new(:main, self, block, true)
  threads << thread
  active_threads << thread
end

Instance Method Details

#run(pattern_name) ⇒ Object Also known as: call

Execute the given pattern



20
21
22
23
24
# File 'lib/origen/generator/pattern_sequence.rb', line 20

def run(pattern_name)
  pattern = Origen.generator.pattern_finder.find(pattern_name.to_s, {})
  pattern = pattern[:pattern] if pattern.is_a?(Hash)
  load pattern
end

#thread(id = nil, &block) ⇒ Object Also known as: in_parallel

Execute the given block in a new concurrent thread



28
29
30
31
32
33
34
35
# File 'lib/origen/generator/pattern_sequence.rb', line 28

def thread(id = nil, &block)
  @number_of_threads += 1
  id ||= "thread#{@number_of_threads}".to_sym
  # Just stage the request for now, it will be started at the end of the current execute loop
  @parallel_blocks_waiting_to_start ||= []
  @parallel_blocks_waiting_to_start << [id, block]
  @running_thread_ids[id] = true
end

#wait_for_threads(*ids) ⇒ Object Also known as: wait_for_thread



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/origen/generator/pattern_sequence.rb', line 38

def wait_for_threads(*ids)
  completed = false
  blocked = false
  ids = ids.map(&:to_sym)
  all = ids.empty? || ids.include?(:all)
  until completed
    if all
      limit = current_thread.id == :main ? 1 : 2
      if @running_thread_ids.size > limit
        current_thread.waiting_for_thread(blocked)
        blocked = true
      else
        current_thread.record_active if blocked
        completed = true
      end
    else
      if ids.any? { |id| @running_thread_ids[id] }
        current_thread.waiting_for_thread(blocked)
        blocked = true
      else
        current_thread.record_active if blocked
        completed = true
      end
    end
  end
end