class Concurrent::Transaction
Constants
- ABORTED
- AbortError
- LeaveError
- ReadLogEntry
Public Class Methods
Source
# File lib/concurrent-ruby/concurrent/tvar.rb, line 251 def self.current Thread.current[:current_tvar_transaction] end
Source
# File lib/concurrent-ruby/concurrent/tvar.rb, line 255 def self.current=(transaction) Thread.current[:current_tvar_transaction] = transaction end
Source
# File lib/concurrent-ruby/concurrent/tvar.rb, line 172 def initialize @read_log = [] @write_log = {} end
Public Instance Methods
Source
# File lib/concurrent-ruby/concurrent/tvar.rb, line 220 def commit return false unless valid? @write_log.each_pair do |tvar, value| tvar.unsafe_value = value tvar.unsafe_increment_version end unlock true end
Source
# File lib/concurrent-ruby/concurrent/tvar.rb, line 177 def read(tvar) Concurrent::abort_transaction unless valid? if @write_log.has_key? tvar @write_log[tvar] else @read_log.push(ReadLogEntry.new(tvar, tvar.unsafe_version)) tvar.unsafe_value end end
Source
# File lib/concurrent-ruby/concurrent/tvar.rb, line 245 def unlock @write_log.each_key do |tvar| tvar.unsafe_lock.unlock end end
Source
# File lib/concurrent-ruby/concurrent/tvar.rb, line 233 def valid? @read_log.each do |log_entry| unless @write_log.has_key? log_entry.tvar if log_entry.tvar.unsafe_version > log_entry.version return false end end end true end
Source
# File lib/concurrent-ruby/concurrent/tvar.rb, line 188 def write(tvar, value) # Have we already written to this TVar? if @write_log.has_key? tvar # Record the value written @write_log[tvar] = value else # Try to lock the TVar unless tvar.unsafe_lock.try_lock # Someone else is writing to this TVar - abort Concurrent::abort_transaction end # Record the value written @write_log[tvar] = value # If we previously read from it, check the version hasn't changed @read_log.each do |log_entry| if log_entry.tvar == tvar and tvar.unsafe_version > log_entry.version Concurrent::abort_transaction end end end end