Class: Litescheduler

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/litescheduler.rb,
lib/litescheduler/version.rb

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLitescheduler

Returns a new instance of Litescheduler.



11
12
13
14
15
16
17
18
19
# File 'lib/litescheduler.rb', line 11

def initialize
  @environment = detect_environment
  @scheduler = detect_scheduler
  @max_contexts = detect_max_contexts
  @storage = detect_storage
  @context = detect_context
  # a single mutex per process (is that ok?)
  @mutex = Thread::Mutex.new
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/litescheduler.rb', line 9

def context
  @context
end

#environmentObject (readonly)

Returns the value of attribute environment.



9
10
11
# File 'lib/litescheduler.rb', line 9

def environment
  @environment
end

#max_contextsObject (readonly)

Returns the value of attribute max_contexts.



9
10
11
# File 'lib/litescheduler.rb', line 9

def max_contexts
  @max_contexts
end

#mutexObject (readonly)

Returns the value of attribute mutex.



9
10
11
# File 'lib/litescheduler.rb', line 9

def mutex
  @mutex
end

#schedulerObject (readonly)

Returns the value of attribute scheduler.



9
10
11
# File 'lib/litescheduler.rb', line 9

def scheduler
  @scheduler
end

#storageObject (readonly)

Returns the value of attribute storage.



9
10
11
# File 'lib/litescheduler.rb', line 9

def storage
  @storage
end

Instance Method Details

#spawn(&block) ⇒ Object

spawn a new execution context



22
23
24
25
26
27
28
29
30
31
# File 'lib/litescheduler.rb', line 22

def spawn(&block)
  case @scheduler
  when :fiber then Fiber.schedule(&block)
  when :polyphony then spin(&block)
  when :iodine then Thread.new(&block)
  when :threaded then Thread.new(&block)
  else
    raise StandardError.new("Unknown scheduler: `#{@scheduler}`")
  end
end

#switchObject

switch the execution context to allow others to run



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/litescheduler.rb', line 34

def switch
  if @scheduler == :fiber
    Fiber.scheduler.yield
    true
  elsif @scheduler == :polyphony
    Fiber.current.schedule
    Thread.current.switch_fiber
    true
  else
    # Thread.pass
    false
  end
end

#synchronize(&block) ⇒ Object

bold assumption, we will only synchronize threaded code this is a no-op for fibers



50
51
52
53
54
55
56
# File 'lib/litescheduler.rb', line 50

def synchronize(&block)
  # do nothing, just run the block as is
  return yield if @scheduler == :fiber
  return yield if @scheduler == :polyphony

  @mutex.synchronize(&block)
end