Class: ConcurrentSHM::Channel::Buffered::Fixed

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

Overview

A buffered channel with fixed-width packets.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ConcurrentSHM::Channel

#close

Class Method Details

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

Allocates a buffered, fixed-width channel with the specified width.

Raises:

  • (RangeError)

    if width is less than 1 or depth is less than 1 or greater than 2<sup>32</sup>-1

See Also:



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/concurrent-shm/channel.rb', line 301

def self.new(shm, depth:, width:, offset: 0, autosize: true)
  raise RangeError, "Width must be > 0" unless width > 0
  raise RangeError, "Depth must be > 0" unless depth > 0
  dbytes = bytes_for(depth) { raise RangeError, "Depth must be less than 2^32" }

  alloc(shm, dbytes*2 + 1 + depth*width, offset: offset, autosize: autosize) do |body|
    @depth, @width = depth,
    @read = body[0, dbytes].as_uintptr
    @written = body[dbytes, dbytes].as_uintptr
    @closed = body[dbytes*2].as_intptr

    data = body[dbytes*2+1..]
    @data = (0...@depth).map { |i| data[i*width, width] }
  end
end

Instance Method Details

#recvString

Receive a fixed-width packet over the channel. Blocks if the buffer is empty.

Returns:

  • (String)

    the packet



332
333
334
335
336
# File 'lib/concurrent-shm/channel.rb', line 332

def recv
  do_recv do |i|
    @data[i].read
  end
end

#send(data) ⇒ nil

Send a fixed-width packet over the channel. Blocks if the buffer is full.

Parameters:

  • data (String)

    the packet

Returns:

  • (nil)

Raises:

  • (ClosedWriteError)

    if the channel is closed

  • (RangeError)

    if the packet size exceeds the channel width



322
323
324
325
326
327
328
# File 'lib/concurrent-shm/channel.rb', line 322

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

  do_send do |i|
    @data[i].write(data)
  end
end