Class: ConcurrentSHM::SharedMemory
- Inherits:
-
IO
- Object
- IO
- ConcurrentSHM::SharedMemory
- Defined in:
- ext/concurrent-shm/posix.c,
ext/concurrent-shm/posix.c
Overview
A POSIX shared memory object.
Class Method Summary collapse
-
.delete(name) ⇒ nil
Delete a POSIX shared memory object.
-
.open(name, fmode, mode: :excl) ⇒ SharedMemory
Create or open a POSIX shared memory object.
Instance Method Summary collapse
-
#initialize(fd, name) ⇒ Object
constructor
Create a SharedMemory instance for the specified file descriptor.
-
#size ⇒ Integer
The size of the shared memory space using ‘fstat`.
-
#size=(size) ⇒ Object
Set the size of the shared memory space using ‘ftruncate`.
Constructor Details
#initialize(fd, name) ⇒ Object
Create a ConcurrentSHM::SharedMemory instance for the specified file descriptor.
102 103 104 105 106 107 |
# File 'ext/concurrent-shm/posix.c', line 102
static VALUE shared_memory_initialize(VALUE self, VALUE fd, VALUE name)
{
rb_call_super(1, &fd); // super(fd)
rb_iv_set(self, "@name", name); // @name = name
return self;
}
|
Class Method Details
.delete(name) ⇒ nil
Delete a POSIX shared memory object.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'ext/concurrent-shm/posix.c', line 25
static VALUE shared_memory_delete(VALUE self, VALUE name)
{
UNUSED(self);
if (NIL_P(name)) {
rb_raise(rb_eArgError, "Name must not be nil");
}
char * cname = StringValueCStr(name);
chk_errno(shm_unlink, (cname), "%s", cname);
return Qnil;
}
|
.open(name, fmode, mode: :excl) ⇒ SharedMemory
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'ext/concurrent-shm/posix.c', line 64
static VALUE shared_memory_open(int argc, VALUE * argv, VALUE self)
{
CONST_IDS(id_, exclusive, anonymous, shared, excl, anon);
VALUE name, fmode;
KWARGS(ARGS("2:", name, fmode), 0, mode);
if (NIL_P(name)) {
rb_raise(rb_eArgError, "Name must not be nil");
}
ID id_mode = mode == Qundef ? id_excl : SYM2ID(mode);
char * cname = StringValueCStr(name);
int oflag = O_CREAT | O_RDWR | O_TRUNC | (id_mode == id_shared ? 0 : O_EXCL);
int fd = shm_open(cname, oflag, FIX2INT(fmode));
if (fd < 0) {
int err = errno;
if (err != EMFILE && err != ENFILE && err != ENOMEM) {
rb_syserr_fail_strf(err, "shm_open(%s, %d, %d)", cname, oflag, FIX2INT(fmode));
}
// if ERRNO indicates no memory, GC and try again
rb_gc();
fd = shm_open(cname, oflag, FIX2INT(fmode));
rb_check_syserr_fail_strf(fd < 0, errno, "shm_open(%s, %d, %d)", cname, oflag, FIX2INT(fmode));
}
if (id_mode == id_anon || id_mode == id_anonymous) {
shared_memory_delete(self, name);
}
return rb_funcall(self, rb_intern("new"), 2, INT2FIX(fd), name);
}
|
Instance Method Details
#size ⇒ Integer
The size of the shared memory space using ‘fstat`.
113 114 115 116 117 118 119 |
# File 'ext/concurrent-shm/posix.c', line 113
static VALUE shared_memory_size(VALUE self)
{
struct stat stat;
VALUE fd = rb_funcall(self, rb_intern("fileno"), 0);
chk_err(fstat, (FIX2INT(fd), &stat), "fd=%d", FIX2INT(fd));
return INT2FIX(stat.st_size);
}
|
#size=(size) ⇒ Object
Set the size of the shared memory space using ‘ftruncate`.
124 125 126 127 128 129 |
# File 'ext/concurrent-shm/posix.c', line 124
static VALUE shared_memory_size_eq(VALUE self, VALUE size)
{
VALUE fd = rb_funcall(self, rb_intern("fileno"), 0);
chk_errno(ftruncate, (FIX2INT(fd), FIX2INT(size)), "%d, %d", FIX2INT(fd), FIX2INT(size));
return size;
}
|