Class: Trip
- Inherits:
-
Object
- Object
- Trip
- Defined in:
- lib/trip.rb,
lib/trip/version.rb
Defined Under Namespace
Classes: Event
Constant Summary collapse
- NotStartedError =
Class.new(RuntimeError)
- NotFinishedError =
Class.new(RuntimeError)
- RUN_STATE =
'run'- SLEEP_STATE =
'sleep'- END_STATE =
[nil, false]
- CALL_E =
['call', 'c-call']
- RETURN_E =
['return', 'c-return']
- PAUSE_P =
Proc.new { |event| CALL_E.include?(event.name) or RETURN_E.include?(event.name) }
- VERSION =
'0.1.1'
Instance Method Summary collapse
-
#finished? ⇒ Boolean
@return [Boolean] returns true when a tracer thread has finished.
-
#initialize(&block) ⇒ Trip
constructor
@param [Proc] &block a block of code.
-
#pause?(callable = nil, &block) ⇒ Proc
Returns a Proc.
-
#resume ⇒ Object
resume the tracer.
-
#running? ⇒ Boolean
@return [Boolean] returns true when a tracer thread is running.
-
#sleeping? ⇒ Boolean
@return [Boolean] returns true when a tracer thread is sleeping.
-
#start ⇒ Trip::Event?
start the tracer.
-
#started? ⇒ Boolean
@return [Boolean] returns true when a trace has started.
-
#stop ⇒ nil
stop the tracer.
Constructor Details
#initialize(&block) ⇒ Trip
@param [Proc] &block
a block of code
@return [Trip]
returns an instance of Trip
25 26 27 28 29 30 31 32 33 |
# File 'lib/trip.rb', line 25 def initialize(&block) if block.equal?(nil) raise ArgumentError, 'no block given' end @thread = nil @block = block @queue = nil @pause = PAUSE_P end |
Instance Method Details
#finished? ⇒ Boolean
@return [Boolean]
returns true when a tracer thread has finished
73 74 75 |
# File 'lib/trip.rb', line 73 def finished? @thread and END_STATE.include?(@thread.status) end |
#pause?(callable = nil, &block) ⇒ Proc
Returns a Proc
45 46 47 48 49 50 51 |
# File 'lib/trip.rb', line 45 def pause?(callable = nil, &block) pause = callable || block if pause.equal?(nil) raise ArgumentError, 'no block given' end @pause = pause end |
#resume ⇒ Object
resume the tracer
@raise [Trip::NotStartedError]
when the start method has not been called yet
@return [Trip::Event, nil]
returns an event or nil
94 95 96 97 98 99 100 101 102 |
# File 'lib/trip.rb', line 94 def resume unless started? raise NotStartedError, 'trace not started' end if sleeping? @thread.wakeup @queue.deq end end |
#running? ⇒ Boolean
@return [Boolean]
returns true when a tracer thread is running
65 66 67 |
# File 'lib/trip.rb', line 65 def running? @thread and @thread.status == RUN_STATE end |
#sleeping? ⇒ Boolean
@return [Boolean]
returns true when a tracer thread is sleeping
81 82 83 |
# File 'lib/trip.rb', line 81 def sleeping? @thread and @thread.status == SLEEP_STATE end |
#start ⇒ Trip::Event?
start the tracer
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/trip.rb', line 113 def start if started? and !finished? raise NotFinishedError, 'trace not finished' end @queue = Queue.new @thread = Thread.new do Thread.current.set_trace_func method(:on_event).to_proc @block.call Thread.current.set_trace_func(nil) @queue.enq(nil) end @queue.deq end |
#started? ⇒ Boolean
@return [Boolean]
returns true when a trace has started
57 58 59 |
# File 'lib/trip.rb', line 57 def started? @thread != nil end |
#stop ⇒ nil
stop the tracer
133 134 135 136 137 138 139 140 |
# File 'lib/trip.rb', line 133 def stop if @thread @thread.set_trace_func(nil) @thread.exit @thread.join nil end end |