Class: ConcurrentSHM::Mutex
- Inherits:
-
Object
- Object
- ConcurrentSHM::Mutex
- 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
-
#initialize(region) ⇒ Object
constructor
Initializes a shared mutex in the specified memory region.
-
#lock ⇒ nil
Locks the mutex, blocking until the mutex becomes available, if necessary.
-
#try_lock ⇒ Boolean
Attempts to lock the mutex.
-
#unlock ⇒ Object
Unlocks the mutex.
Constructor Details
#initialize(region) ⇒ Object
Initializes a shared mutex in the specified memory region.
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
#lock ⇒ nil
Locks the mutex, blocking until the mutex becomes available, if necessary.
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_lock ⇒ Boolean
Attempts to lock the mutex.
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");
}
}
|
#unlock ⇒ Object
Unlocks the mutex.
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;
}
|