File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -115,6 +115,9 @@ def atomically
115115 rescue Transaction ::AbortError => e
116116 transaction . abort
117117 result = Transaction ::ABORTED
118+ rescue Transaction ::LeaveError => e
119+ transaction . abort
120+ break result
118121 rescue => e
119122 transaction . abort
120123 raise e
@@ -144,6 +147,11 @@ def abort_transaction
144147 raise Transaction ::AbortError . new
145148 end
146149
150+ # Leave a transaction without commiting or aborting - see `Concurrent::atomically`.
151+ def leave_transaction
152+ raise Transaction ::LeaveError . new
153+ end
154+
147155 module_function :atomically , :abort_transaction
148156
149157 private
@@ -155,6 +163,7 @@ class Transaction
155163 ReadLogEntry = Struct . new ( :tvar , :version )
156164
157165 AbortError = Class . new ( StandardError )
166+ LeaveError = Class . new ( StandardError )
158167
159168 def initialize
160169 @read_log = [ ]
Original file line number Diff line number Diff line change @@ -117,13 +117,15 @@ module Concurrent
117117 t . value = 1
118118 a . count_down
119119 b . wait
120+ Concurrent . leave_transaction
120121 end
121122 end
122123
123124 Concurrent ::atomically do
124125 a . wait
125126 expect ( t . value ) . to eq 0
126127 b . count_down
128+ Concurrent . leave_transaction
127129 end
128130 end
129131
@@ -191,4 +193,24 @@ module Concurrent
191193
192194 end
193195
196+ describe '#leave_transaction' do
197+
198+ it 'raises an exception outside an #atomically block' do
199+ expect { Concurrent ::leave_transaction } . to raise_error ( Concurrent ::Transaction ::LeaveError )
200+ end
201+
202+ it 'neither commits nor aborts a transaction' do
203+ t = TVar . new ( 0 )
204+
205+ Concurrent ::atomically do
206+ expect ( t . value ) . to eq 0
207+ t . value = 14
208+ Concurrent ::leave_transaction
209+ end
210+
211+ expect ( t . value ) . to eq 0
212+ end
213+
214+ end
215+
194216end
You can’t perform that action at this time.
0 commit comments