Class: Winipc::Semaphore

Inherits:
Object
  • Object
show all
Defined in:
lib/winipc.rb,
ext/winipc/winipc.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(name, initial:, maximum:, scope: :local, security: :owner) ⇒ Object



300
301
302
303
304
305
306
# File 'lib/winipc.rb', line 300

def self.create(name, initial:, maximum:, scope: :local, security: :owner)
  unless maximum.is_a?(Integer) && maximum > 0 && initial.is_a?(Integer) && initial >= 0 && initial <= maximum
    raise ArgumentError, "require 0 <= initial <= maximum and maximum > 0"
  end

  _create(K_SEM, Winipc.obj_path(name, scope), initial, maximum, security)
end

.open(name, scope: :local) ⇒ Object



308
309
310
# File 'lib/winipc.rb', line 308

def self.open(name, scope: :local)
  _open(K_SEM, Winipc.obj_path(name, scope))
end

Instance Method Details

#_release(vcount) ⇒ Object



1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
# File 'ext/winipc/winipc.c', line 1135

static VALUE
sem_release(VALUE self, VALUE vcount)
{
    sync_t *s = sync_live(self);
    LONG count = NUM2LONG(vcount), prev = 0;
    if (!ReleaseSemaphore(s->h, count, &prev)) {
        DWORD e = GetLastError();
        if (e == ERROR_TOO_MANY_POSTS) raise_code(eWouldExceedMax, "ReleaseSemaphore", e);
        raise_gle("ReleaseSemaphore", e);
    }
    return LONG2NUM(prev);
}

#acquire(timeout: nil) ⇒ Object

Acquire one unit. Returns true, or false on timeout.



313
314
315
# File 'lib/winipc.rb', line 313

def acquire(timeout: nil)
  Winipc.run_blocking { _wait(Winipc.ms_for(timeout)) } == :ok
end

#release(count = 1) ⇒ Object

Release count units; returns the previous count.



322
323
324
# File 'lib/winipc.rb', line 322

def release(count = 1)
  _release(count)
end

#synchronize(timeout: nil) ⇒ Object

Acquire one unit for the duration of the block, then release it. (Use #acquire / #release directly for multi-unit patterns.)

Raises:



328
329
330
331
332
333
334
335
336
# File 'lib/winipc.rb', line 328

def synchronize(timeout: nil)
  raise TimeoutError, "winipc: semaphore not acquired within timeout" unless acquire(timeout: timeout)

  begin
    yield
  ensure
    _release(1)
  end
end

#try_acquireObject



317
318
319
# File 'lib/winipc.rb', line 317

def try_acquire
  acquire(timeout: 0)
end