Class: Mutex

Class | Mutex
In: | ext/fastthread/fastthread.c ext/fastthread/fastthread.c
Parent: | Object

Mutex implements a simple semaphore that can be used to coordinate access to shared data from multiple concurrent threads.

Example:

      require 'thread'
      semaphore = Mutex.new

      a = Thread.new {
        semaphore.synchronize {
          # access shared resource
        }
      }

      b = Thread.new {
        semaphore.synchronize {
          # access shared resource
        }
      }

### Methods

exclusive_unlock exclusive_unlock lock lock locked? locked? marshal_load marshal_load synchronize synchronize try_lock try_lock unlock unlock

### Public Instance methods

exclusive_unlock { ... }

If the mutex is locked, unlocks the mutex, wakes one waiting thread, and yields in a critical section.

exclusive_unlock { ... }

If the mutex is locked, unlocks the mutex, wakes one waiting thread, and yields in a critical section.

lock

Attempts to grab the lock and waits if it isn‘t available.

lock

Attempts to grab the lock and waits if it isn‘t available.

locked?

Returns `true` if this lock is currently held by some thread.

[Source]

    /*
     * Document-method: locked?
     * call-seq: locked?
     *
     * Returns +true+ if this lock is currently held by some thread.
     *
     */

    static VALUE
    rb_mutex_locked_p(VALUE self)
    {
        Mutex *mutex;
        Data_Get_Struct(self, Mutex, mutex);
        return MUTEX_LOCKED_P(mutex) ? Qtrue : Qfalse;
    }

locked?

Returns `true` if this lock is currently held by some thread.

[Source]

    /*
     * Document-method: locked?
     * call-seq: locked?
     *
     * Returns +true+ if this lock is currently held by some thread.
     *
     */

    static VALUE
    rb_mutex_locked_p(VALUE self)
    {
        Mutex *mutex;
        Data_Get_Struct(self, Mutex, mutex);
        return MUTEX_LOCKED_P(mutex) ? Qtrue : Qfalse;
    }

marshal_load(p1)

for marshalling mutexes and condvars

[Source]

    /* for marshalling mutexes and condvars */

    static VALUE
    dummy_load(VALUE self, VALUE string)
    {
        return Qnil;
    }

marshal_load(p1)

for marshalling mutexes and condvars

[Source]

    /* for marshalling mutexes and condvars */

    static VALUE
    dummy_load(VALUE self, VALUE string)
    {
        return Qnil;
    }

synchronize { ... }

Obtains a lock, runs the block, and releases the lock when the block completes. See the example under Mutex.

[Source]

    /*
     * Document-method: synchronize
     * call-seq: synchronize { ... }
     *
     * Obtains a lock, runs the block, and releases the lock when the block
     * completes.  See the example under Mutex.
     *
     */

    static VALUE
    rb_mutex_synchronize(VALUE self)
    {
        rb_mutex_lock(self);
        return rb_ensure(rb_yield, Qundef, rb_mutex_unlock, self);
    }

synchronize { ... }

Obtains a lock, runs the block, and releases the lock when the block completes. See the example under Mutex.

[Source]

    /*
     * Document-method: synchronize
     * call-seq: synchronize { ... }
     *
     * Obtains a lock, runs the block, and releases the lock when the block
     * completes.  See the example under Mutex.
     *
     */

    static VALUE
    rb_mutex_synchronize(VALUE self)
    {
        rb_mutex_lock(self);
        return rb_ensure(rb_yield, Qundef, rb_mutex_unlock, self);
    }

try_lock

Attempts to obtain the lock and returns immediately. Returns `true` if the lock was granted.

[Source]

    /*
     * Document-method: try_lock
     * call-seq: try_lock
     *
     * Attempts to obtain the lock and returns immediately. Returns +true+ if the
     * lock was granted.
     *
     */

    static VALUE
    rb_mutex_try_lock(VALUE self)
    {
        Mutex *mutex;

        Data_Get_Struct(self, Mutex, mutex);

        if (MUTEX_LOCKED_P(mutex))
            return Qfalse;

        mutex->owner = rb_thread_current();
        return Qtrue;
    }

try_lock

Attempts to obtain the lock and returns immediately. Returns `true` if the lock was granted.

[Source]

    /*
     * Document-method: try_lock
     * call-seq: try_lock
     *
     * Attempts to obtain the lock and returns immediately. Returns +true+ if the
     * lock was granted.
     *
     */

    static VALUE
    rb_mutex_try_lock(VALUE self)
    {
        Mutex *mutex;

        Data_Get_Struct(self, Mutex, mutex);

        if (MUTEX_LOCKED_P(mutex))
            return Qfalse;

        mutex->owner = rb_thread_current();
        return Qtrue;
    }

unlock()

Releases the lock. Returns `nil` if ref wasn‘t locked.

unlock()

Releases the lock. Returns `nil` if ref wasn‘t locked.

[Validate]