Partial MonitorMixin implementation.
This commit is contained in:
parent
80284301a7
commit
5aba20cdf7
|
@ -29,4 +29,5 @@ require 'process_shared/binary_semaphore'
|
|||
require 'process_shared/mutex'
|
||||
require 'process_shared/condition_variable'
|
||||
require 'process_shared/monitor'
|
||||
require 'process_shared/monitor_mixin'
|
||||
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
require 'process_shared'
|
||||
|
||||
module ProcessShared
|
||||
module MonitorMixin
|
||||
def self.extended(obj)
|
||||
obj.send :mon_initialize
|
||||
end
|
||||
|
||||
def mon_enter
|
||||
@mon_monitor.lock
|
||||
end
|
||||
|
||||
def mon_exit
|
||||
@mon_monitor.unlock
|
||||
end
|
||||
|
||||
def mon_synchronize
|
||||
mon_enter
|
||||
begin
|
||||
yield
|
||||
ensure
|
||||
mon_exit
|
||||
end
|
||||
end
|
||||
alias_method :synchronize, :mon_synchronize
|
||||
|
||||
def mon_try_enter
|
||||
raise NotImplementedError, 'not implemented'
|
||||
end
|
||||
alias_method :try_mon_enter, :mon_try_enter
|
||||
|
||||
def new_cond
|
||||
raise NotImplementedError, 'not implemented'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mon_initialize
|
||||
@mon_monitor = Monitor.new
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,33 @@
|
|||
require 'spec_helper'
|
||||
require 'process_shared'
|
||||
|
||||
module ProcessShared
|
||||
describe MonitorMixin do
|
||||
|
||||
before :each do
|
||||
@obj = Object.new
|
||||
@obj.extend(MonitorMixin)
|
||||
end
|
||||
|
||||
it 'raises exception when unlocked by other process' do
|
||||
pid = Kernel.fork do
|
||||
@obj.mon_enter
|
||||
sleep 0.2
|
||||
@obj.mon_exit
|
||||
Kernel.exit!
|
||||
end
|
||||
|
||||
sleep 0.1
|
||||
proc { @obj.mon_exit }.must_raise(ProcessError)
|
||||
|
||||
::Process.wait(pid)
|
||||
end
|
||||
|
||||
it 'raises nothing with nested lock' do
|
||||
@obj.mon_enter
|
||||
@obj.mon_enter
|
||||
@obj.mon_exit
|
||||
@obj.mon_exit
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue