Class: ProcessShared::Posix::SharedArray
- Inherits:
-
SharedMemory
- Object
- FFI::Pointer
- SharedMemory
- ProcessShared::Posix::SharedArray
- Includes:
- Enumerable
- Defined in:
- lib/process_shared/posix/shared_array.rb
Constant Summary
Constants included from LibC
LibC::MAP_FAILED, LibC::MAP_PRIVATE, LibC::MAP_SHARED, LibC::O_CREAT, LibC::O_EXCL, LibC::O_RDWR, LibC::PROT_EXEC, LibC::PROT_NONE, LibC::PROT_READ, LibC::PROT_WRITE
Instance Attribute Summary
Attributes inherited from SharedMemory
#count, #fd, #size, #type, #type_size
Instance Method Summary collapse
- #[](i) ⇒ Object
- #[]=(i, val) ⇒ Object
- #each ⇒ Object
- #each_with_index ⇒ Object
-
#initialize(type_or_count = 1, count = 1) ⇒ SharedArray
constructor
A fixed-size array in shared memory.
Methods inherited from SharedMemory
Methods included from OpenWithSelf
Methods included from ObjectBuffer
#get_object, #put_object, #read_object, #write_object
Methods included from LibC
Methods included from Errno
Constructor Details
#initialize(type_or_count = 1, count = 1) ⇒ SharedArray
A fixed-size array in shared memory. Processes forked from this one will be able to read and write shared data to the array. Access should be synchronized using a Mutex, ProcessShared::Posix::Semaphore, or other means.
Note that Enumerable methods such as #map, #sort, etc. return new Array objects rather than modifying the shared array.
understood by FFI (e.g. :int, :double)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/process_shared/posix/shared_array.rb', line 19 def initialize(type_or_count = 1, count = 1) super(type_or_count, count) # See https://github.com/ffi/ffi/issues/118 ffi_type = FFI.find_type(self.type) name = if ffi_type.inspect =~ /FFI::Type::Builtin:(\w+)*/ # name will be something like int32 $1.downcase end unless name raise ArgumentError, "could not find FFI::Type for #{self.type}" end getter = "get_#{name}" setter = "put_#{name}" # singleton class sclass = class << self; self; end unless sclass.method_defined?(getter) raise ArgumentError, "no element getter for #{self.type} (#{getter})" end unless sclass.method_defined?(setter) raise ArgumentError, "no element setter for #{self.type} (#{setter})" end sclass.send(:alias_method, :get_type, getter) sclass.send(:alias_method, :put_type, setter) end |
Instance Method Details
#[](i) ⇒ Object
62 63 64 |
# File 'lib/process_shared/posix/shared_array.rb', line 62 def [](i) get_type(i * self.type_size) end |
#[]=(i, val) ⇒ Object
66 67 68 |
# File 'lib/process_shared/posix/shared_array.rb', line 66 def []=(i, val) put_type(i * self.type_size, val) end |
#each ⇒ Object
52 53 54 55 56 |
# File 'lib/process_shared/posix/shared_array.rb', line 52 def each # NOTE: using @count because Enumerable defines its own count # method... @count.times { |i| yield self[i] } end |
#each_with_index ⇒ Object
58 59 60 |
# File 'lib/process_shared/posix/shared_array.rb', line 58 def each_with_index @count.times { |i| yield self[i], i } end |