Skip to content

Commit

Permalink
Made SysV override on boot-up
Browse files Browse the repository at this point in the history
  • Loading branch information
kyewei committed Nov 20, 2015
1 parent 9d117e2 commit 89075a2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 52 deletions.
39 changes: 7 additions & 32 deletions lib/semian/sysv_shared_memory.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
module Semian
module SysVSharedMemory #:nodoc:
@type_size = {}
def self.sizeof(type)
size = (@type_size[type.to_sym] ||= (respond_to?(:_sizeof) ? _sizeof(type.to_sym) : 0))
raise TypeError.new("#{type} is not a valid C type") if size <= 0
size
end

def self.included(base)
def base.do_with_sync(*names)
names.each do |name|
Expand All @@ -30,38 +23,20 @@ def shmid
-1
end

def synchronize(&block)
if respond_to?(:_synchronize) && @using_shared_memory
return _synchronize(&block)
else
yield if block_given?
end
def synchronize
yield if block_given?
end

alias_method :transaction, :synchronize

def destroy
if respond_to?(:_destroy) && @using_shared_memory
_destroy
else
super
end
super
end

private

def shared?
@using_shared_memory
end

def acquire_memory_object(name, data_layout, permissions)
return @using_shared_memory = false unless Semian.semaphores_enabled? && respond_to?(:_acquire)

byte_size = data_layout.inject(0) { |sum, type| sum + ::Semian::SysVSharedMemory.sizeof(type) }
raise TypeError.new("Given data layout is 0 bytes: #{data_layout.inspect}") if byte_size <= 0
# Calls C layer to acquire/create a memory block, calling #bind_initialize_memory_callback in the process, see below
_acquire(name, byte_size, permissions)
@using_shared_memory = true
def acquire_memory_object(*)
# Concrete classes must call this method before accessing shared memory
# If SysV is enabled, a C method overrides this stub and returns true if acquiring succeeds
false
end

def bind_initialize_memory_callback
Expand Down
28 changes: 8 additions & 20 deletions lib/semian/sysv_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,25 @@ class State < Semian::Simple::State #:nodoc:
include SysVSharedMemory
extend Forwardable

def_delegators :@integer, :semid, :shmid, :synchronize, :transaction,
:shared?, :acquire_memory_object, :bind_initialize_memory_callback
private :shared?, :acquire_memory_object, :bind_initialize_memory_callback
SYM_TO_NUM = {closed: 0, open: 1, half_open: 2}.freeze
NUM_TO_SYM = SYM_TO_NUM.invert.freeze

def_delegators :@integer, :semid, :shmid, :synchronize, :acquire_memory_object,
:bind_initialize_memory_callback, :destroy
private :acquire_memory_object, :bind_initialize_memory_callback

def initialize(name:, permissions:)
@integer = Semian::SysV::Integer.new(name: name, permissions: permissions)
initialize_lookup
end

def destroy
super
@integer.destroy
end

def value
@num_to_sym.fetch(@integer.value) { raise ArgumentError }
NUM_TO_SYM.fetch(@integer.value) { raise ArgumentError }
end

private

def value=(sym)
@integer.value = @sym_to_num.fetch(sym) { raise ArgumentError }
end

def initialize_lookup
# Assume symbol_list[0] is mapped to 0
# Cannot just use #object_id since #object_id for symbols is different in every run
# For now, implement a C-style enum type backed by integers

@sym_to_num = {closed: 0, open: 1, half_open: 2}
@num_to_sym = @sym_to_num.invert
@integer.value = SYM_TO_NUM.fetch(sym) { raise ArgumentError }
end
end
end
Expand Down

0 comments on commit 89075a2

Please sign in to comment.