Class: ConcurrentSHM::Channel::Buffered::Fixed
- Inherits:
-
ConcurrentSHM::Channel::Buffered
- Object
- ConcurrentSHM::Channel
- ConcurrentSHM::Channel::Buffered
- ConcurrentSHM::Channel::Buffered::Fixed
- Defined in:
- lib/concurrent-shm/channel.rb
Overview
A buffered channel with fixed-width packets.
Class Method Summary collapse
-
.new(shm, depth:, width:, offset: 0, autosize: true) ⇒ Object
Allocates a buffered, fixed-width channel with the specified width.
Instance Method Summary collapse
-
#recv ⇒ String
Receive a fixed-width packet over the channel.
-
#send(data) ⇒ nil
Send a fixed-width packet over the channel.
Methods inherited from ConcurrentSHM::Channel
Class Method Details
.new(shm, depth:, width:, offset: 0, autosize: true) ⇒ Object
Allocates a buffered, fixed-width channel with the specified width.
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
#recv ⇒ String
Receive a fixed-width packet over the channel. Blocks if the buffer is empty.
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.
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 |