Class: Moneta::SynchronizePrimitive

### Home Classes Methods

### In Files

- lib/moneta/synchronize.rb

### Parent

Object

### Methods

- #enter
- #leave
- #lock
- #locked?
- #synchronize
- #try_enter
- #try_lock
- #unlock

### Files

- LICENSE

### Class/Module Index

Quicksearch

- Moneta
- Moneta::Adapters
- Moneta::Adapters::ActiveRecord
- Moneta::Adapters::Cassandra
- Moneta::Adapters::Client
- Moneta::Adapters::Cookie
- Moneta::Adapters::Couch
- Moneta::Adapters::DBM
- Moneta::Adapters::DataMapper
- Moneta::Adapters::DataMapper::Store
- Moneta::Adapters::Daybreak
- Moneta::Adapters::File
- Moneta::Adapters::Fog
- Moneta::Adapters::GDBM
- Moneta::Adapters::HBase
- Moneta::Adapters::KyotoCabinet
- Moneta::Adapters::LMDB
- Moneta::Adapters::LRUHash
- Moneta::Adapters::LRUHash::Entry
- Moneta::Adapters::LevelDB
- Moneta::Adapters::LocalMemCache
- Moneta::Adapters::MemcachedDalli
- Moneta::Adapters::MemcachedNative
- Moneta::Adapters::Memory
- Moneta::Adapters::Mongo
- Moneta::Adapters::Null
- Moneta::Adapters::PStore
- Moneta::Adapters::Redis
- Moneta::Adapters::RestClient
- Moneta::Adapters::Riak
- Moneta::Adapters::SDBM
- Moneta::Adapters::Sequel
- Moneta::Adapters::Sqlite
- Moneta::Adapters::TDB
- Moneta::Adapters::TokyoCabinet
- Moneta::Adapters::TokyoTyrant
- Moneta::Adapters::YAML
- Moneta::Builder
- Moneta::Cache
- Moneta::Cache::DSL
- Moneta::CreateSupport
- Moneta::Defaults
- Moneta::Defaults::ClassMethods
- Moneta::Expires
- Moneta::ExpiresSupport
- Moneta::HashAdapter
- Moneta::IncrementSupport
- Moneta::Lock
- Moneta::Logger
- Moneta::Logger::Format
- Moneta::Mutex
- Moneta::OptionMerger
- Moneta::OptionSupport
- Moneta::Pool
- Moneta::Proxy
- Moneta::Semaphore
- Moneta::Server
- Moneta::Shared
- Moneta::Stack
- Moneta::Stack::DSL
- Moneta::SynchronizePrimitive
- Moneta::Transformer
- Moneta::Transformer::Helper
- Moneta::Utils
- Moneta::WeakCreate
- Moneta::WeakIncrement
- Moneta::Wrapper
- Rack
- Rack::Cache
- Rack::Cache::EntityStore
- Rack::Cache::EntityStore::MONETA
- Rack::Cache::EntityStore::Moneta
- Rack::Cache::MetaStore
- Rack::Cache::MetaStore::MONETA
- Rack::Cache::MetaStore::Moneta
- Rack::Cache::MonetaResolver
- Rack::MonetaCookies
- Rack::MonetaRest
- Rack::MonetaStore
- Rack::Session
- Rack::Session::Moneta
- ActionDispatch
- ActionDispatch::Session
- ActionDispatch::Session::MonetaStore
- ActiveSupport
- ActiveSupport::Cache
- ActiveSupport::Cache::MonetaStore

No matching classes.

# Moneta::SynchronizePrimitive

Base class for {Mutex} and {Semaphore} @api private

### Public Instance Methods

enter(timeout = nil, wait = 0.01) click to toggle source

Enter critical section (blocking)

@api public @param [Number] timeout Maximum time to wait @param [Number] wait Sleep time between tries to acquire lock @return [Boolean] true if the lock was aquired

    # File lib/moneta/synchronize.rb, line 33
    def enter(timeout = nil, wait = 0.01)
      time_at_timeout = Time.now + timeout if timeout
      while !timeout || Time.now < time_at_timeout
        return true if try_enter
        sleep(wait)
      end
      false
    end

Also aliased as: lock
leave() click to toggle source

Leave critical section

@api public

    # File lib/moneta/synchronize.rb, line 46
    def leave
      raise 'Not locked' unless @locked
      leave_primitive
      @locked = false
      nil
    end

Also aliased as: unlock
lock(timeout = nil, wait = 0.01) click to toggle source
Alias for: enter
locked?() click to toggle source

Is the lock acquired?

@api public

    # File lib/moneta/synchronize.rb, line 57
    def locked?
      @locked
    end

synchronize() click to toggle source

Synchronize block

@api public @yieldparam Synchronized block @return [Object] result of block

    # File lib/moneta/synchronize.rb, line 10
    def synchronize
      enter
      yield
    ensure
      leave
    end

try_enter() click to toggle source

Try to enter critical section (nonblocking)

@api public @return [Boolean] true if the lock was acquired

    # File lib/moneta/synchronize.rb, line 21
    def try_enter
      raise 'Already locked' if @locked
      enter_primitive ? @locked = true : false
    end

Also aliased as: try_lock
try_lock() click to toggle source
Alias for: try_enter
unlock() click to toggle source
Alias for: leave

[Validate]

Generated with the Darkfish Rdoc Generator 2.