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
25
26
27
28
29
30
31
32
33
34
# File 'lib/origen/generator/pattern_sequence.rb', line 20

def run(pattern_name)
  name = Pathname.new(pattern_name.to_s).basename
  ss "START OF PATTERN: #{name}"
  # Give the app a chance to handle pattern dispatch
  skip = false
  Origen.app.listeners_for(:before_pattern_lookup).each do |listener|
    skip ||= !listener.before_pattern_lookup(pattern_name.to_s)
  end
  unless skip
    pattern = Origen.generator.pattern_finder.find(pattern_name.to_s, {})
    pattern = pattern[:pattern] if pattern.is_a?(Hash)
    load pattern
  end
  ss "END OF PATTERN: #{name}"
end

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

Execute the given block in a new concurrent thread



38
39
40
41
42
43
44
45
# File 'lib/origen/generator/pattern_sequence.rb', line 38

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/origen/generator/pattern_sequence.rb', line 48

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