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
115
115
rescue Transaction ::AbortError => e
116
116
transaction . abort
117
117
result = Transaction ::ABORTED
118
+ rescue Transaction ::LeaveError => e
119
+ transaction . abort
120
+ break result
118
121
rescue => e
119
122
transaction . abort
120
123
raise e
@@ -144,6 +147,11 @@ def abort_transaction
144
147
raise Transaction ::AbortError . new
145
148
end
146
149
150
+ # Leave a transaction without commiting or aborting - see `Concurrent::atomically`.
151
+ def leave_transaction
152
+ raise Transaction ::LeaveError . new
153
+ end
154
+
147
155
module_function :atomically , :abort_transaction
148
156
149
157
private
@@ -155,6 +163,7 @@ class Transaction
155
163
ReadLogEntry = Struct . new ( :tvar , :version )
156
164
157
165
AbortError = Class . new ( StandardError )
166
+ LeaveError = Class . new ( StandardError )
158
167
159
168
def initialize
160
169
@read_log = [ ]
Original file line number Diff line number Diff line change @@ -117,13 +117,15 @@ module Concurrent
117
117
t . value = 1
118
118
a . count_down
119
119
b . wait
120
+ Concurrent . leave_transaction
120
121
end
121
122
end
122
123
123
124
Concurrent ::atomically do
124
125
a . wait
125
126
expect ( t . value ) . to eq 0
126
127
b . count_down
128
+ Concurrent . leave_transaction
127
129
end
128
130
end
129
131
@@ -191,4 +193,24 @@ module Concurrent
191
193
192
194
end
193
195
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
+
194
216
end
You can’t perform that action at this time.
0 commit comments