Skip to content

Commit 674065c

Browse files
committed
TVar: Concurrent.leave_transaction, used to fix isolation specs but useful on its own.
1 parent 40eacdf commit 674065c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/concurrent/tvar.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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 = []

spec/concurrent/tvar_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
194216
end

0 commit comments

Comments
 (0)