Skip to content

Commit

Permalink
Rebased, small changes had to be done
Browse files Browse the repository at this point in the history
  • Loading branch information
kyewei committed Nov 10, 2015
1 parent c18f642 commit 9957a65
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if Semian.sysv_semaphores_supported?
ext.ext_dir = 'ext/semian'
ext.lib_dir = 'lib/semian'
end
task :build => :compile
task build: :compile
else
task :build do
end
Expand Down
2 changes: 1 addition & 1 deletion ext/semian/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
have_func 'rb_thread_call_without_gvl'

$CFLAGS = "-D_GNU_SOURCE -Werror -Wall -std=c99 "
if ENV.has_key?('DEBUG')
if ENV.key?('DEBUG')
$CFLAGS << "-O0 -g"
else
$CFLAGS << "-O3"
Expand Down
15 changes: 11 additions & 4 deletions lib/semian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,21 @@ def resources
module ReentrantMutex
attr_reader :monitor

def call_with_mutex
@monitor ||= Monitor.new
@monitor.synchronize do
yield if block_given?
end
end

def self.included(base)
def base.surround_with_mutex(*names)
names.each do |name|
m = instance_method(name)
new_name = "#{name}_inner".freeze
alias_method new_name, name
define_method(name) do |*args, &block|
@monitor ||= Monitor.new
@monitor.synchronize do
m.bind(self).call(*args, &block)
call_with_mutex do
method(new_name).call(*args, &block)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/semian/atomic_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AtomicEnum < SharedMemoryObject #:nodoc:
:shared?, :destroy, :acquire_memory_object, :bind_init_fn
private :shared?, :acquire_memory_object, :bind_init_fn

def initialize(symbol_list, **options)
def initialize(symbol_list)
@integer = Semian::AtomicInteger.new
initialize_lookup(symbol_list)
end
Expand Down
3 changes: 1 addition & 2 deletions lib/semian/atomic_integer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module Semian
class AtomicInteger < SharedMemoryObject #:nodoc:
include ::Semian::ReentrantMutex
attr_accessor :value

def initialize(**options)
def initialize
@value = 0
end

Expand Down
9 changes: 7 additions & 2 deletions lib/semian/shared_memory_object.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Semian
class SharedMemoryObject #:nodoc:
include ReentrantMutex

@type_size = {}
def self.sizeof(type)
size = (@type_size[type.to_sym] ||= (respond_to?(:_sizeof) ? _sizeof(type.to_sym) : 0))
Expand All @@ -16,8 +18,11 @@ def shmid
end

def execute_atomically(&proc)
return _execute_atomically(&proc) if respond_to?(:_execute_atomically) && @using_shared_memory
yield if block_given?
if respond_to?(:_execute_atomically) && @using_shared_memory
return _execute_atomically(&proc)
else
call_with_mutex(&proc)
end
end

alias_method :transaction, :execute_atomically
Expand Down
3 changes: 1 addition & 2 deletions lib/semian/sliding_window.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module Semian
class SlidingWindow < SharedMemoryObject #:nodoc:
extend Forwardable
include ::Semian::ReentrantMutex

def_delegators :@window, :size, :pop, :shift, :first, :last
attr_reader :max_size

def initialize(max_size, **options)
def initialize(max_size)
@max_size = max_size
@window = []
end
Expand Down
2 changes: 1 addition & 1 deletion lib/semian/sysv_atomic_enum.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Semian
class SysVAtomicEnum < AtomicEnum #:nodoc:
def initialize(symbol_list, name:, permissions:)
def initialize(symbol_list, name:, permissions:)
@integer = Semian::SysVAtomicInteger.new(name: name, permissions: permissions)
initialize_lookup(symbol_list)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/semian/sysv_atomic_integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Semian
class SysVAtomicInteger < AtomicInteger #:nodoc:
def initialize(name:, permissions:)
data_layout = [:int]
super unless acquire_memory_object(name, data_layout, permissions)
super() unless acquire_memory_object(name, data_layout, permissions)
end
end
end
2 changes: 1 addition & 1 deletion lib/semian/sysv_sliding_window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Semian
class SysVSlidingWindow < SlidingWindow #:nodoc:
def initialize(max_size, name:, permissions:)
data_layout = [:int, :int].concat(Array.new(max_size, :long))
super unless acquire_memory_object(name, data_layout, permissions)
super(max_size) unless acquire_memory_object(name, data_layout, permissions)
end
end
end
5 changes: 1 addition & 4 deletions test/atomic_enum_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ class TestAtomicEnum < MiniTest::Unit::TestCase
CLASS = ::Semian::AtomicEnum

def setup
@enum = CLASS.new([:one, :two, :three],
name: 'TestAtomicEnum',
permissions: 0660)
@enum = CLASS.new([:one, :two, :three])
end

def teardown
Expand Down Expand Up @@ -47,4 +45,3 @@ def test_will_throw_error_when_invalid_symbol_given

include AtomicEnumTestCases
end

2 changes: 1 addition & 1 deletion test/atomic_integer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class TestAtomicInteger < MiniTest::Unit::TestCase
CLASS = ::Semian::AtomicInteger

def setup
@integer = CLASS.new(name: 'TestAtomicInteger', permissions: 0660)
@integer = CLASS.new
@integer.value = 0
end

Expand Down
4 changes: 1 addition & 3 deletions test/sliding_window_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ class TestSlidingWindow < MiniTest::Unit::TestCase
CLASS = ::Semian::SlidingWindow

def setup
@sliding_window = CLASS.new(6,
name: 'TestSlidingWindow',
permissions: 0660)
@sliding_window = CLASS.new(6)
@sliding_window.clear
end

Expand Down
5 changes: 2 additions & 3 deletions test/sysv_atomic_enum_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def test_memory_is_shared
@enum.value = :three

enum_2 = CLASS.new([:one, :two, :three],
name: 'TestAtomicEnum',
permissions: 0660)
name: 'TestAtomicEnum',
permissions: 0660)
assert_equal :three, enum_2.value
end

end

0 comments on commit 9957a65

Please sign in to comment.