Class: ConcurrentSHM::Mutex

Inherits:
Object
  • Object
show all
Defined in:
ext/concurrent-shm/posix.c,
ext/concurrent-shm/posix.c

Overview

A POSIX-threads mutex, with the ‘pshared` attribute set to `PTHREAD_PROCESS_SHARED`, allocated in a shared memory space.

Instance Method Summary collapse

Constructor Details

#initialize(region) ⇒ Object

Initializes a shared mutex in the specified memory region.

Parameters:

  • region (Region)

    the memory region

Raises:

  • (RangeError)

    if the region is too small or not aligned to a 16-byte boundary



579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'ext/concurrent-shm/posix.c', line 579

static VALUE mutex_initialize(VALUE self, VALUE region)
{
    mutex_t * mu = value_as_mutex(self);
    set_region(mu, 16, region);
    __set_finalizer(self, mu);

    if (__shared_retain(&mu->shared->refcount) == 1) {
        chk_err(pthread_mutexattr_init, (&mu->shared->attr), "");
        chk_err(pthread_mutexattr_setpshared, (&mu->shared->attr, PTHREAD_PROCESS_SHARED), "");
        chk_err(pthread_mutex_init, (&mu->shared->mu, &mu->shared->attr), "");
    }
    return self;
}

Instance Method Details

#locknil

Locks the mutex, blocking until the mutex becomes available, if necessary.

Returns:

  • (nil)

Raises:

  • (Errno::EDEADLK)

    if the current thread already owns the mutex

  • (Errno::EAGAIN)

    if the mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded



616
617
618
619
620
621
622
623
624
625
626
627
# File 'ext/concurrent-shm/posix.c', line 616

static VALUE mutex_lock(VALUE self)
{
    if (mutex_try_lock(self) == Qtrue) {
        return Qnil;
    }

    mutex_t * mu = value_as_mutex(self);
    int err = (int)(uintptr_t)rb_thread_call_without_gvl((void * (*)(void *))pthread_mutex_lock, &mu->shared->mu, NULL, NULL);

    rb_check_syserr_fail_strf(err, err, "pthread_mutex_lock()");
    return Qnil;
}

#try_lockBoolean

Attempts to lock the mutex.

Returns:

  • (Boolean)

    ‘true` if the lock was acquired

Raises:

  • (Errno::EAGAIN)

    if the mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded



597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'ext/concurrent-shm/posix.c', line 597

static VALUE mutex_try_lock(VALUE self)
{
    mutex_t * mu = value_as_mutex(self);
    int err = pthread_mutex_trylock(&mu->shared->mu);
    switch (err) {
    case 0:
        return Qtrue;
    case EBUSY:
        return Qfalse;
    default:
        rb_syserr_fail_strf(err, "mutex_try_lock");
    }
}

#unlockObject

Unlocks the mutex.

Raises:

  • (Errno::EPERM)

    if the current thread does not own the mutex

  • (Errno::EAGAIN)

    if the mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded



633
634
635
636
637
638
# File 'ext/concurrent-shm/posix.c', line 633

static VALUE mutex_unlock(VALUE self)
{
    mutex_t * mu = value_as_mutex(self);
    chk_err(pthread_mutex_unlock, (&mu->shared->mu), "");
    return Qnil;
}