Class: Winipc::Mutex

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, scope: :local, security: :owner) ⇒ Object



242
243
244
# File 'lib/winipc.rb', line 242

def self.create(name, scope: :local, security: :owner)
  _create(K_MUTEX, Winipc.obj_path(name, scope), nil, nil, security)
end

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



246
247
248
# File 'lib/winipc.rb', line 246

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

Instance Method Details

#_unlockObject



1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
# File 'ext/winipc/winipc.c', line 1107

static VALUE
mutex_unlock(VALUE self)
{
    sync_t *s = sync_live(self);
    if (!ReleaseMutex(s->h)) {
        DWORD e = GetLastError();
        if (e == ERROR_NOT_OWNER) raise_code(eNotOwner, "ReleaseMutex", e);
        raise_gle("ReleaseMutex", e);
    }
    return self;
}

#abandoned?Boolean

Returns:

  • (Boolean)


1149
# File 'ext/winipc/winipc.c', line 1149

static VALUE sync_abandoned_p(VALUE self) { return sync_get(self)->abandoned ? Qtrue : Qfalse; }

#lock(timeout: nil) ⇒ Object

NOTE: a Windows mutex is owned by the acquiring THREAD, so Mutex waits are NOT offloaded to a worker (that would acquire on the wrong thread). They release the GVL but, under a fiber scheduler, block the loop for the wait. Use Event/Semaphore for fiber-cooperative coordination.



254
255
256
# File 'lib/winipc.rb', line 254

def lock(timeout: nil)
  _wait(Winipc.ms_for(timeout)) != :timeout # :ok or :abandoned both acquire
end

#synchronize(timeout: nil) ⇒ Object

Raises:



267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/winipc.rb', line 267

def synchronize(timeout: nil)
  result = _wait(Winipc.ms_for(timeout))
  raise TimeoutError, "winipc: mutex not acquired within timeout" if result == :timeout

  begin
    if result == :abandoned
      raise Abandoned, "winipc: mutex was abandoned by a dead owner; shared state may be inconsistent"
    end

    yield
  ensure
    _unlock
  end
end

#try_lockObject



258
259
260
# File 'lib/winipc.rb', line 258

def try_lock
  lock(timeout: 0)
end

#unlockObject



262
263
264
265
# File 'lib/winipc.rb', line 262

def unlock
  _unlock
  self
end