Class: Concurrent::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/concurrent-ruby/concurrent/tvar.rb

Defined Under Namespace

Classes: OpenEntry

Constant Summary collapse

ABORTED =
::Object.new
AbortError =
Class.new(StandardError)
LeaveError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransaction

Returns a new instance of Transaction.



161
162
163
# File 'lib/concurrent-ruby/concurrent/tvar.rb', line 161

def initialize
  @open_tvars = {}
end

Class Method Details

.currentObject



211
212
213
# File 'lib/concurrent-ruby/concurrent/tvar.rb', line 211

def self.current
  Thread.current[:current_tvar_transaction]
end

.current=(transaction) ⇒ Object



215
216
217
# File 'lib/concurrent-ruby/concurrent/tvar.rb', line 215

def self.current=(transaction)
  Thread.current[:current_tvar_transaction] = transaction
end

Instance Method Details

#abortObject



191
192
193
# File 'lib/concurrent-ruby/concurrent/tvar.rb', line 191

def abort
  unlock
end

#commitObject



195
196
197
198
199
200
201
202
203
# File 'lib/concurrent-ruby/concurrent/tvar.rb', line 195

def commit
  @open_tvars.each do |tvar, entry|
    if entry.modified
      tvar.unsafe_value = entry.value
    end
  end

  unlock
end

#open(tvar) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/concurrent-ruby/concurrent/tvar.rb', line 176

def open(tvar)
  entry = @open_tvars[tvar]

  unless entry
    unless tvar.unsafe_lock.try_lock
      Concurrent::abort_transaction
    end

    entry = OpenEntry.new(tvar.unsafe_value, false)
    @open_tvars[tvar] = entry
  end

  entry
end

#read(tvar) ⇒ Object



165
166
167
168
# File 'lib/concurrent-ruby/concurrent/tvar.rb', line 165

def read(tvar)
  entry = open(tvar)
  entry.value
end

#unlockObject



205
206
207
208
209
# File 'lib/concurrent-ruby/concurrent/tvar.rb', line 205

def unlock
  @open_tvars.each_key do |tvar|
    tvar.unsafe_lock.unlock
  end
end

#write(tvar, value) ⇒ Object



170
171
172
173
174
# File 'lib/concurrent-ruby/concurrent/tvar.rb', line 170

def write(tvar, value)
  entry = open(tvar)
  entry.modified = true
  entry.value = value
end