### Home Classes Methods

### In Files

- lib/needle/thread.rb

### Parent

Mutex

### Methods

- ::new
- #lock
- #self_locked?
- #try_lock
- #unlock

### Files

- README

### Class/Module Index

Quicksearch

- Needle
- Needle::Bug
- Needle::Container
- Needle::DefinitionContext
- Needle::IncludeExclude
- Needle::Interceptor
- Needle::Interceptor::DynamicInterceptor
- Needle::InterceptorChainBuilder
- Needle::InterceptorChainBuilder::InterceptedServiceProxy
- Needle::InterceptorChainBuilder::InterceptorChainElement
- Needle::InterceptorChainBuilder::ProxyObjectChainElement
- Needle::InterceptorConfigurationError
- Needle::Lifecycle
- Needle::Lifecycle::Deferred
- Needle::Lifecycle::Initialize
- Needle::Lifecycle::Multiton
- Needle::Lifecycle::Proxy
- Needle::Lifecycle::Singleton
- Needle::Lifecycle::Threaded
- Needle::LogFactory
- Needle::Logger
- Needle::LoggingInterceptor
- Needle::NeedleError
- Needle::Pipeline
- Needle::Pipeline::Collection
- Needle::Pipeline::Collection::BlockElement
- Needle::Pipeline::Element
- Needle::Pipeline::InterceptorElement
- Needle::QueryableMutex
- Needle::Registry
- Needle::ServiceNotFound
- Needle::ServicePoint
- Needle::Version

No matching classes.

# Needle::QueryableMutex

A subclass of Mutex that allows clients to discover which thread has the mutex locked. This is necessary for (among other things) discovering cycles in the dependency graph of services.

### Public Class Methods

new() click to toggle source

Create a new unlocked QueryableMutex.

    # File lib/needle/thread.rb, line 27
    def initialize
      super
      @locking_thread = nil
    end

### Public Instance Methods

lock() click to toggle source

Checks to see if the current thread has the mutex locked, and if it does, raises an exception. Otherwise, locks the mutex and sets the locking thread to Thread.current.

    # File lib/needle/thread.rb, line 35
    def lock
      if @locking_thread == Thread.current
        raise ThreadError,
          "an attempt was made to reacquire an existing lock " +
          "(this could happen if you have a circular dependency on a service)"
      end

      while (Thread.critical = true; @locked)
        @waiting.push Thread.current
        Thread.stop
      end
      @locked = true
      @locking_thread = Thread.current
      Thread.critical = false
      self
    end

self_locked?() click to toggle source

Return `true` if the current thread has locked this mutex.

    # File lib/needle/thread.rb, line 87
    def self_locked?
      @locking_thread == Thread.current
    end

try_lock() click to toggle source

Attempts to acquire the lock. Returns `true` if the lock was acquired, and `false` if the mutex was already locked.

    # File lib/needle/thread.rb, line 54
    def try_lock
      result = false
      Thread.critical = true
      unless @locked
        @locked = true
        result = true
        @locking_thread = Thread.current
      end
      Thread.critical = false
      result
    end

unlock() click to toggle source

Unlocks the mutex and sets the locking thread to `nil`.

    # File lib/needle/thread.rb, line 67
    def unlock
      return unless @locked
      Thread.critical = true
      @locked = false
      begin
        t = @waiting.shift
        t.wakeup if t
      rescue ThreadError
        retry
      end
      @locking_thread = nil
      Thread.critical = false
      begin
        t.run if t
      rescue ThreadError
      end
      self
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.