Class: ConcurrentSHM::Channel::Unbuffered::Fixed

Inherits:
ConcurrentSHM::Channel::Unbuffered show all
Defined in:
lib/concurrent-shm/channel.rb

Overview

An unbuffered channel with fixed-width packets.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ConcurrentSHM::Channel

#close

Class Method Details

.new(shm, width:, offset: 0, autosize: true) ⇒ Object

Allocates an unbuffered, fixed-width channel with the specified width.

Raises:

  • if the width is less than 1

See Also:



44
45
46
47
48
49
50
51
52
# File 'lib/concurrent-shm/channel.rb', line 44

def self.new(shm, width:, offset: 0, autosize: true)
  raise RangeError, "Width must be > 0" unless width > 0

  alloc(shm, 2 + width, offset: offset, autosize: autosize) do |body|
    @state = body[0].as_intptr
    @closed = body[1].as_intptr
    @data = body[2..]
  end
end

Instance Method Details

#recvString

Receive a fixed-width packet over the channel. Blocks if there are no senders.

Returns:

  • the packet



69
70
71
72
73
# File 'lib/concurrent-shm/channel.rb', line 69

def recv
  do_recv do
    @data.read
  end
end

#send(data) ⇒ nil

Send a fixed-width packet over the channel. Blocks if there are no receivers.

Parameters:

  • the packet

Returns:

Raises:

  • if the channel is closed

  • if the packet size exceeds the channel width



59
60
61
62
63
64
65
# File 'lib/concurrent-shm/channel.rb', line 59

def send(data)
  raise RangeError, "Data is wider than channel" if data.size > @data.size

  do_send do
    @data.write(data)
  end
end