Class: MPV::MultiQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/mpv_ipc/multi_queue.rb

Overview

Holds state to handle request/response lifecycle for mpv’s ipc protocol.

Defined Under Namespace

Classes: Reader

Instance Method Summary collapse

Constructor Details

#initialize(timeout = nil) ⇒ MultiQueue

Creates a new MPV::MultiQueue instance.

Parameters:

  • timeout (Numeric) (defaults to: nil)

    default timeout for pops in seconds



43
44
45
46
47
48
# File 'lib/mpv_ipc/multi_queue.rb', line 43

def initialize(timeout=nil)
  @timeout = timeout
  @table = Hash.new
  @mutex = Mutex.new
  @resource = ConditionVariable.new
end

Instance Method Details

#open(id) {|Reader| ... } ⇒ Object

Opens a new queue for a certain unique id.

Parameters:

  • id (Integer)

    the unique queue id

Yields:

  • (Reader)

    if given, yields the queue reader then closes it and returns the block’s result.

Returns:

  • (Object)

    new queue reader, or the block’s result if given



55
56
57
58
59
60
# File 'lib/mpv_ipc/multi_queue.rb', line 55

def open(id, &block)
  reader = Reader.new(self, id)
  block ? block.call(reader) : reader
ensure
  reader.close if block and reader
end

#push(id, value) ⇒ Boolean

Pushes a value to a certain queue.

Parameters:

  • id (Integer)

    the unique queue id

  • value (Object)

    the value

Returns:

  • (Boolean)

    whether the push was successful



66
67
68
69
70
71
72
73
74
# File 'lib/mpv_ipc/multi_queue.rb', line 66

def push(id, value)
  @mutex.synchronize do
    if queue = @table[id]
      queue.push(value)
      @resource.broadcast
    end
    !!queue
  end
end