Class: Dramatis::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/dramatis/runtime.rb,
lib/dramatis/runtime/gate.rb,
lib/dramatis/runtime/task.rb,
lib/dramatis/runtime/actor.rb,
lib/dramatis/runtime/timer.rb,
lib/dramatis/runtime/scheduler.rb,
lib/dramatis/runtime/actor/main.rb,
lib/dramatis/runtime/thread_pool.rb

Overview

Dramatis::Runtime is the top level class managing the running of the various pieces of the dramatis runtime. Typically programs don’t need to deal with the runtime directly, though some functions are useful, particularly for debugging and testing.

Defined Under Namespace

Classes: Actor, Gate, Scheduler, Task, ThreadPool, Timer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currentObject

call-seq: current -> current_runtime_object

Returns a reference to the current Dramatis::Runtime object.



17
18
19
# File 'lib/dramatis/runtime.rb', line 17

def self.current
  @@current ||= self.new
end

.resetObject

call-seq: reset -> nil

Resets the current runtime instance. Note that this method hard resets counters and ignores exceptions which is generally a bad idea. It is typical only used in unit test and spec “after” methods to keep failing tests from cascading.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dramatis/runtime.rb', line 29

def self.reset
  # this swallows exceptions: it's assumed to be used to clean up
  # a failed test so there's no connection between tests
  begin
    Dramatis::Runtime.current.quiesce
  rescue Exception => e
  end
  Dramatis::Runtime::Scheduler.reset    
  Dramatis::Runtime::Actor::Main.reset    
  @@current = nil
end

Instance Method Details

#at_exitObject

:nodoc:



138
139
140
# File 'lib/dramatis/runtime.rb', line 138

def at_exit #:nodoc:
  Dramatis::Runtime::Actor::Main.current.finalize
end

#backtraceObject

:nodoc:



111
112
113
114
115
116
117
# File 'lib/dramatis/runtime.rb', line 111

def backtrace #:nodoc:
  begin
    raise "backtrace"
  rescue ::Exception => e
    pp e.backtrace
  end
end

#clear_exceptionsObject

call-seq: clear_exceptions -> nil

Clears the list of uncaught exceptions. Used in unit tests and specs to clear expected exceptions. If exceptions are raised and not cleared, they will be raised at the end of the program via a Dramatis::Error::Uncaught.



95
96
97
98
99
100
# File 'lib/dramatis/runtime.rb', line 95

def clear_exceptions
  @mutex.synchronize do
    # warn "runtime clearing exceptions"
    @exceptions.clear
  end
end

#exception(exception) ⇒ Object

:nodoc:



102
103
104
105
106
107
108
109
# File 'lib/dramatis/runtime.rb', line 102

def exception exception #:nodoc:
  @mutex.synchronize do
    @exceptions << exception
    warn "runtime recording exception: #{exception} [#{@exceptions.length}]" if warnings?
    # backtrace
    # pp exception.backtrace
  end
end

#exceptionsObject

call-seq: exceptions -> array_of_exceptions

Returns the list of exceptions that were not caught by an actor.



79
80
81
82
83
84
85
# File 'lib/dramatis/runtime.rb', line 79

def exceptions 
  result = nil
  @mutex.synchronize do
    result = @exceptions.dup
  end
  result
end

#maybe_raise_exceptions(quiescing) ⇒ Object

:nodoc:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dramatis/runtime.rb', line 57

def maybe_raise_exceptions quiescing #:nodoc:
  @mutex.synchronize do
    if !@exceptions.empty?
      # warn "no maybe about it"
      if !quiescing and warnings?
        warn "the following #{@exceptions.length} exception(s) were raised and not caught"
        @exceptions.each do |exception|
          # warn "#{exception}"
          # pp exception.backtrace
        end
      end
      raise Dramatis::Error::Uncaught.new( @exceptions )
    end
    @exceptions.clear
  end
end

#quiesceObject

call-seq: quiesce -> nil

Causes the runtime to suspend the current thread until there are no more tasks that can be executed. If no tasks remain, returns normally. If tasks remain but are gated off, Dramatis::Deadlock is raised.

As a side effect, this method releases the current actor to process messages but does not change the task gate.



52
53
54
55
# File 'lib/dramatis/runtime.rb', line 52

def quiesce
  Dramatis::Runtime::Scheduler.current.quiesce
  maybe_raise_exceptions true
end

#warnings=(value) ⇒ Object

call-seq: warnings = boolean -> boolean

Enables or disables printing warnings, e.g., when uncaught exceptions are detected. Returns the value passed.



125
126
127
# File 'lib/dramatis/runtime.rb', line 125

def warnings= value
  @warnings = value
end

#warnings?Boolean

call-seq: warnings? -> boolean

Returns true if warnings are enabled.

Returns:

  • (Boolean)


134
135
136
# File 'lib/dramatis/runtime.rb', line 134

def warnings?
  @warnings
end