Class: FSDB::Modex
- Inherits:
-
Object
- Object
- FSDB::Modex
- Defined in:
- lib/fsdb/modex.rb,
lib/fsdb/faster-modex.rb
Overview
Modex is a modal exclusion semaphore, like in syncronizer.rb. The two modes are shared (SH) and exclusive (EX). Modex is not nestable.
Defined Under Namespace
Modules: ForkSafely
Constant Summary collapse
- SH =
:SH
- EX =
:EX
Instance Method Summary collapse
-
#initialize ⇒ Modex
constructor
A new instance of Modex.
-
#lock(mode) ⇒ Object
the block is executed in the exclusive context.
-
#remove_dead ⇒ Object
:nodoc:.
- #synchronize(mode, do_when_first = nil, do_when_last = nil, arg = nil) ⇒ Object
- #try_lock(mode) ⇒ Object
-
#unlock ⇒ Object
the block is executed in the exclusive context.
Constructor Details
#initialize ⇒ Modex
Returns a new instance of Modex.
35 36 37 38 39 40 |
# File 'lib/fsdb/modex.rb', line 35 def initialize @waiting = [] @locked = [] @mode = nil @first = true end |
Instance Method Details
#lock(mode) ⇒ Object
the block is executed in the exclusive context
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fsdb/modex.rb', line 59 def lock mode Thread.exclusive do thread = Thread.current raise ThreadError if @locked.include?(thread) if @mode == mode and mode == SH and @waiting.empty? # strict queue @locked << thread elsif not @mode @mode = mode @locked << thread else @waiting << thread << mode Thread.stop Thread.critical = true end yield if block_given? # if @mode != mode # raise "@mode == #{@mode} but mode == #{mode}" # end # # if @mode == EX and @locked.size > 1 # raise "@mode == EX but @locked.size == #{@locked.size}" # end self end end |
#remove_dead ⇒ Object
:nodoc:
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/fsdb/modex.rb', line 136 def remove_dead # :nodoc: Thread.exclusive do waiting = @waiting; @waiting = [] until waiting.empty? t = waiting.shift; m = waiting.shift @waiting << t << m if t.alive? end @locked = @locked.select {|t| t.alive?} wake_next_waiter if @locked.empty? end end |
#synchronize(mode, do_when_first = nil, do_when_last = nil, arg = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/fsdb/modex.rb', line 102 def synchronize mode, do_when_first = nil, do_when_last = nil, arg = nil lock mode do if @first @first = false if do_when_first if mode == SH @mode = EX end Thread.nonexclusive { do_when_first[arg] } if mode == SH @mode = SH wake_waiting_sharers end end end end yield ensure unlock do if @locked.size == 1 if do_when_last @mode = EX Thread.nonexclusive { do_when_last[arg] } end @first = true end end end |
#try_lock(mode) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/fsdb/modex.rb', line 42 def try_lock mode Thread.exclusive do thread = Thread.current raise ThreadError if @locked.include?(thread) if @mode == mode and mode == SH and @waiting.empty? # strict queue @locked << thread true elsif not @mode @mode = mode @locked << thread true end end end |