# Class: Concurrent::Transaction

Inherits:
  Object
  - Object
  - Concurrent::Transaction
  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

- .current ⇒ undocumented
- .current=(transaction) ⇒ undocumented

## Instance Method Summary collapse

- #abort ⇒ undocumented
- #commit ⇒ undocumented
- #initialize ⇒ Transaction constructor

  A new instance of Transaction.

- #open(tvar) ⇒ undocumented
- #read(tvar) ⇒ undocumented
- #unlock ⇒ undocumented
- #write(tvar, value) ⇒ undocumented

## Constructor Details

### #initialize ⇒ `Transaction`

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

### .current ⇒ `undocumented`

    211
    212
    213

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

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

### .current=(transaction) ⇒ `undocumented`

    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

### #abort ⇒ `undocumented`

    191
    192
    193

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

    def abort
      unlock
    end

### #commit ⇒ `undocumented`

    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) ⇒ `undocumented`

    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) ⇒ `undocumented`

    165
    166
    167
    168

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

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

### #unlock ⇒ `undocumented`

    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) ⇒ `undocumented`

    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