Class: ConcurrentSHM::SharedMemory

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

Overview

A POSIX shared memory object.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fd, name) ⇒ Object

Create a ConcurrentSHM::SharedMemory instance for the specified file descriptor.

Parameters:

  • fd (Integer)

    the file descriptor

  • name (String)

    the name



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.

Parameters:

  • name (String)

    the name of the object

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    if the name is nil or is not a string

  • (Errno::EACCES)

    if permission to delete the object is denied

  • (Errno::ENOENT)

    if the named object does not exist



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

Create or open a POSIX shared memory object. The default flags are ‘O_CREATE`, `O_RDWR`, and `O_TRUNC`.

If the mode is ‘:excl`, `:exclusive`, `:anon`, or `:anonymous`, the object is opened exclusively, with the `O_EXCL` flag in addition to the defaults.

If the mode is ‘:anon` or `:anonymous`, the object is deleted immediately after being opened. Opened and deleted shared memory objects can be used until the object is closed.

If the mode is ‘:shared`, the object is opened with the default flags.

Parameters:

  • name (String)

    the name of the object

  • fmode (Integer)

    the file mode of the object

  • mode (Symbol) (defaults to: :excl)

    the manner in which to create or open the object.

Returns:

Raises:

  • (ArgumentError)

    if the name is nil or is not a string

  • (Errno::EACCES)

    if permission to create or open the object is denied

  • (Errno::EEXIST)

    if ‘exclusive` is `true` and the object specified by `name` already exists

  • (Errno::EINVAL)

    if the name is invalid

  • (Errno::EMFILE)

    if the per-process limit on the number of open file descriptors has been reached

  • (Errno::ENAMETOOLONG)

    if the name is too long

  • (Errno::ENFILE)

    if the system-wide limit on the total number of open files has been reached



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

#sizeInteger

The size of the shared memory space using ‘fstat`.

Returns:

  • (Integer)

Raises:

  • (Errno::EBADF)

    if the receivers ‘fileno` is not a valid file descriptor



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`.

Parameters:

  • size (Integer)

    the new size of the space



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;
}