class Origen::Generator::PatternSequencer
Provides APIs to enable applications to support concurrency
Public Class Methods
active?()
click to toggle source
Returns true if a pattern sequence is currently open/active
# File lib/origen/generator/pattern_sequencer.rb, line 64 def active? !!@active end
add_thread(str)
click to toggle source
Prepends the given string with “[<current thread ID>] ” unless it already contains it
# File lib/origen/generator/pattern_sequencer.rb, line 76 def add_thread(str) if active? && thread id = "[#{thread.id}] " str.prepend(id) unless str =~ /#{id}/ end str end
reserve(id) { || ... }
click to toggle source
Once a lock is acquired on a serialize block with the given ID, it won’t be released to other parallel threads until the end of this block
# File lib/origen/generator/pattern_sequencer.rb, line 41 def reserve(id) if active? if thread.reservations[id] thread.reservations[id][:count] += 1 else thread.reservations[id] = { count: 1, semaphore: nil } end yield if thread.reservations[id][:count] == 1 # May not be set if the application reserved the resource but never hit it if s = thread.reservations[id][:semaphore] s.release end thread.reservations[id] = nil else thread.reservations[id][:count] -= 1 end else yield end end
serialize(id = nil) { || ... }
click to toggle source
# File lib/origen/generator/pattern_sequencer.rb, line 7 def serialize(id = nil) if active? s = nil id ||= caller[0] @semaphores ||= {} @semaphores[id] ||= Concurrent::Semaphore.new(1) s = @semaphores[id] completed = false blocked = false until completed # If already acquired or available if (thread.reservations[id] && thread.reservations[id][:semaphore]) || s.try_acquire thread.record_active if blocked yield completed = true else thread.waiting_for_serialize(id, blocked) blocked = true end end # If the thread has reserved access to this serialized resource then don't release it now, but # store a reference to the semaphore and it will be released at the end of the reserve block if thread.reservations[id] thread.reservations[id][:semaphore] = s else s.release end else yield end end
sync_up(*ids)
click to toggle source
# File lib/origen/generator/pattern_sequencer.rb, line 93 def sync_up(*ids) if @current_sequence @current_sequence.send(:sync_up, caller[0], *ids) end end
thread()
click to toggle source
Returns the PatternThread
object for the current thread
# File lib/origen/generator/pattern_sequencer.rb, line 71 def thread @thread.value end
wait_for_threads_to_complete(*ids)
click to toggle source
Wait for the given threads to complete. If no IDs given it will wait for all currently running threads (except for the one who called this) to complete.
# File lib/origen/generator/pattern_sequencer.rb, line 86 def wait_for_threads_to_complete(*ids) @current_sequence.wait_for_threads_to_complete(*ids) end
Private Class Methods
active=(val)
click to toggle source
# File lib/origen/generator/pattern_sequencer.rb, line 105 def active=(val) @active = val end
current_sequence=(seq)
click to toggle source
# File lib/origen/generator/pattern_sequencer.rb, line 101 def current_sequence=(seq) @current_sequence = seq end
thread=(t)
click to toggle source
# File lib/origen/generator/pattern_sequencer.rb, line 109 def thread=(t) @thread ||= Concurrent::ThreadLocalVar.new(nil) @thread.value = t end