Module: Minx

Defined in:
lib/minx.rb,
lib/minx/channel.rb,
lib/minx/process.rb

Defined Under Namespace

Classes: Channel, Process

Class Method Summary collapse

Class Method Details

.channelChannel

Create a new channel.

Returns:



20
21
22
# File 'lib/minx.rb', line 20

def self.channel
  Channel.new
end

.join(*processes) ⇒ nil

Wait for the processes to yield execution.

Returns:

  • (nil)


34
35
36
37
38
39
40
# File 'lib/minx.rb', line 34

def self.join(*processes)
  processes.each do |process|
    process.resume
  end

  return nil
end

.select(*choices) ⇒ Object

Select from a list of channels.

The channels will be enumerated in order; the first one carrying a message will be picked, and the message will be returned.

If none of the channels are readable, the calling process will yield until a channel is written to, unless :skip => true is passed as an option, in which case the call will just return nil.

Examples:

Non-blocking select

Minx.select(chan1, chan2, :skip => true)

Parameters:

  • choices (Channel)

    the channels to be selected among

Returns:

  • the first message received from any of the channels



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

def self.select(*choices)
  options = choices.last.is_a?(Hash) ? choices.pop : {}

  # If a choice is readable, just receive from that one.
  choices.each do |choice|
    return choice.receive if choice.readable?
  end

  # Return immediately if :skip => true
  return if options[:skip]

  # ... otherwise, wait for a channel to become readable.
  choices.each do |choice|
    choice.receive(:async => true)
  end
  Minx.yield
end

.spawn(&block) ⇒ Process

Spawn a new process.

Returns:



13
14
15
# File 'lib/minx.rb', line 13

def self.spawn(&block)
  Process.new(&block).spawn
end

.yield(*args) ⇒ Object

Yield control to another process.

The calling process will be resumed at a later point.



27
28
29
# File 'lib/minx.rb', line 27

def self.yield(*args)
  Fiber.yield(*args)
end