-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change to State from Enum, updated to match merged PR, removed Mutex …
…code
- Loading branch information
Showing
16 changed files
with
218 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,6 @@ def destroy | |
@value = 0 | ||
end | ||
end | ||
|
||
surround_with_mutex :value, :value=, :increment | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'forwardable' | ||
|
||
module Semian | ||
module Simple | ||
class State < SharedMemoryObject #:nodoc: | ||
def initialize | ||
reset | ||
end | ||
|
||
attr_reader :value | ||
|
||
def open? | ||
value == :open | ||
end | ||
|
||
def closed? | ||
value == :closed | ||
end | ||
|
||
def half_open? | ||
value == :half_open | ||
end | ||
|
||
def open | ||
@value = :open | ||
end | ||
|
||
def close | ||
@value = :closed | ||
end | ||
|
||
def half_open | ||
@value = :half_open | ||
end | ||
|
||
def reset | ||
close | ||
end | ||
|
||
def destroy | ||
reset | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Semian | ||
module SysV | ||
class State < Semian::Simple::State #:nodoc: | ||
extend Forwardable | ||
|
||
def_delegators :@integer, :semid, :shmid, :execute_atomically, :transaction, | ||
:shared?, :acquire_memory_object, :bind_init_fn | ||
private :shared?, :acquire_memory_object, :bind_init_fn | ||
|
||
def initialize(name:, permissions:) | ||
@integer = Semian::SysV::Integer.new(name: name, permissions: permissions) | ||
initialize_lookup([:closed, :open, :half_open]) | ||
end | ||
|
||
def open | ||
self.value = :open | ||
end | ||
|
||
def close | ||
self.value = :closed | ||
end | ||
|
||
def half_open | ||
self.value = :half_open | ||
end | ||
|
||
def reset | ||
close | ||
end | ||
|
||
def destroy | ||
reset | ||
@integer.destroy | ||
end | ||
|
||
def value | ||
@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(symbol_list) | ||
# 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 = Hash[symbol_list.each_with_index.to_a] | ||
@num_to_sym = @sym_to_num.invert | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'test_helper' | ||
|
||
class TestSimpleState < MiniTest::Unit::TestCase | ||
CLASS = ::Semian::Simple::State | ||
|
||
def setup | ||
@state = CLASS.new | ||
end | ||
|
||
def teardown | ||
@state.destroy | ||
end | ||
|
||
module StateTestCases | ||
def test_start_closed? | ||
assert @state.closed? | ||
end | ||
|
||
def test_open | ||
@state.open | ||
assert @state.open? | ||
assert_equal @state.value, :open | ||
end | ||
|
||
def test_close | ||
@state.close | ||
assert @state.closed? | ||
assert_equal @state.value, :closed | ||
end | ||
|
||
def test_half_open | ||
@state.half_open | ||
assert @state.half_open? | ||
assert_equal @state.value, :half_open | ||
end | ||
|
||
def test_reset | ||
@state.reset | ||
assert @state.closed? | ||
assert_equal @state.value, :closed | ||
end | ||
end | ||
|
||
include StateTestCases | ||
end |
Oops, something went wrong.