Sha256: f1f80cdaf4a63c2f560af22a53dfb240b19c4846c74b99d0d59906fb9f7b8338

Contents?: true

Size: 400 Bytes

Versions: 16

Compression:

Stored size: 400 Bytes

Contents

# frozen_string_literal: true

export :Mutex

# Implements mutex lock for synchronizing access to a shared resource
class Mutex
  def initialize
    @waiting_fibers = []
  end

  def synchronize
    fiber = Fiber.current
    @waiting_fibers << fiber
    suspend if @waiting_fibers.size > 1
    yield
  ensure
    @waiting_fibers.delete(fiber)
    @waiting_fibers.first&.schedule
    snooze
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
polyphony-0.36 lib/polyphony/core/sync.rb
polyphony-0.34 lib/polyphony/core/sync.rb
polyphony-0.33 lib/polyphony/core/sync.rb
polyphony-0.32 lib/polyphony/core/sync.rb
polyphony-0.31 lib/polyphony/core/sync.rb
polyphony-0.30 lib/polyphony/core/sync.rb
polyphony-0.29 lib/polyphony/core/sync.rb
polyphony-0.28 lib/polyphony/core/sync.rb
polyphony-0.27 lib/polyphony/core/sync.rb
polyphony-0.26 lib/polyphony/core/sync.rb
polyphony-0.25 lib/polyphony/core/sync.rb
polyphony-0.24 lib/polyphony/core/sync.rb
polyphony-0.23 lib/polyphony/core/sync.rb
polyphony-0.22 lib/polyphony/core/sync.rb
polyphony-0.21 lib/polyphony/core/sync.rb
polyphony-0.20 lib/polyphony/core/sync.rb