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, pre_block = nil) ⇒ PatternSequence

Returns a new instance of PatternSequence.



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

def initialize(name, block, pre_block = nil)
  @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, pre_block)
  threads << thread
  active_threads << thread
  PatSeq.send(:current_sequence=, self)
  @sync_ups = {}
end

Instance Method Details

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

Execute the given pattern



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/origen/generator/pattern_sequence.rb', line 22

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



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

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_to_complete(*ids) ⇒ Object Also known as: wait_for_thread, wait_for_threads, wait_for_thread_to_complete



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

def wait_for_threads_to_complete(*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