Module: Denko::Behaviors::Threaded

Included in:
AnalogIO::Output, Poller, DigitalIO::Output
Defined in:
lib/denko/behaviors/threaded.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#interrupts_enabledObject (readonly)

Returns the value of attribute interrupts_enabled.



4
5
6
# File 'lib/denko/behaviors/threaded.rb', line 4

def interrupts_enabled
  @interrupts_enabled
end

#threadObject (readonly)

Returns the value of attribute thread.



4
5
6
# File 'lib/denko/behaviors/threaded.rb', line 4

def thread
  @thread
end

Class Method Details

.included(base) ⇒ Object



20
21
22
# File 'lib/denko/behaviors/threaded.rb', line 20

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#enable_interruptsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/denko/behaviors/threaded.rb', line 48

def enable_interrupts
  unless Denko.mruby?
    interrupts = self.class.class_eval('@@interrupts') rescue []
    interrupts.each do |method_name|
      standard_method = self.method(method_name)

      singleton_class.send(:define_method, method_name) do |*args|
        stop_thread unless (Thread.current == @thread)
        standard_method.call(*args)
      end
    end
  end

  @interrupts_enabled = true
end

#mruby_thread_checkObject



14
15
16
17
18
# File 'lib/denko/behaviors/threaded.rb', line 14

def mruby_thread_check
  if Denko.mruby?
    raise NotImplementedError, "threads unavailable in mruby "
  end
end

#stopObject



43
44
45
46
# File 'lib/denko/behaviors/threaded.rb', line 43

def stop
  stop_thread unless Denko.mruby?
  begin; super; rescue NoMethodError; end
end

#stop_threadObject



38
39
40
41
# File 'lib/denko/behaviors/threaded.rb', line 38

def stop_thread
  mruby_thread_check
  @thread.kill if @thread
end

#threaded(&block) ⇒ Object



24
25
26
27
28
29
# File 'lib/denko/behaviors/threaded.rb', line 24

def threaded(&block)
  mruby_thread_check
  stop_thread
  enable_interrupts unless interrupts_enabled
  @thread = Thread.new(&block)
end

#threaded_loop(&block) ⇒ Object



31
32
33
34
35
36
# File 'lib/denko/behaviors/threaded.rb', line 31

def threaded_loop(&block)
  mruby_thread_check
  threaded do
    loop(&block)
  end
end