Class: Win32::Mutex
- Inherits:
-
Ipc
- Object
- Ipc
- Win32::Mutex
- Defined in:
- lib/win32/mutex.rb
Overview
The Mutex class encapsulates Windows mutex objects.
Defined Under Namespace
Classes: SecurityAttributes
Constant Summary collapse
- VERSION =
The version of the win32-mutex library
'0.4.3'
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The name of the mutex object.
Class Method Summary collapse
-
.open(name, inherit = true, &block) ⇒ Object
Open an existing Mutex by
name
.
Instance Method Summary collapse
-
#inheritable? ⇒ Boolean
Returns whether or not the object was opened such that a process created by the CreateProcess() function (a Windows API function) can inherit the handle.
-
#initial_owner? ⇒ Boolean
Returns whether or not the calling thread has initial ownership of the mutex object.
-
#initialize(initial_owner = false, name = nil, inherit = true) ⇒ Mutex
constructor
Creates and returns new Mutex object.
-
#release ⇒ Object
Releases ownership of the mutex.
Constructor Details
#initialize(initial_owner = false, name = nil, inherit = true) ⇒ Mutex
Creates and returns new Mutex object. If name
is omitted, the Mutex object is created without a name, i.e. it’s anonymous.
If the initial_owner
value is true and the caller created the mutex, the calling thread obtains initial ownership of the mutex object. Otherwise, the calling thread does not obtain ownership of the mutex. This value is false by default.
If name
is provided and it already exists, then it is opened instead, and the initial_count
and max_count
parameters are ignored.
The inherit
attribute determines whether or not the mutex can be inherited by child processes.
55 56 57 58 59 60 61 62 63 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 |
# File 'lib/win32/mutex.rb', line 55 def initialize(initial_owner=false, name=nil, inherit=true) @initial_owner = initial_owner @name = name @inherit = inherit if inherit sec = SecurityAttributes.new sec[:nLength] = SecurityAttributes.size sec[:bInheritHandle] = true else sec = nil end if name && name.encoding.to_s != 'UTF-16LE' name = name + 0.chr name.encode!('UTF-16LE') end handle = CreateMutexW(sec, initial_owner, name) if handle == 0 || handle == INVALID_HANDLE_VALUE raise SystemCallError.new("CreateMutex", FFI.errno) end super(handle) if block_given? begin yield self ensure close # From superclass end end end |
Instance Attribute Details
#name ⇒ Object (readonly)
The name of the mutex object.
38 39 40 |
# File 'lib/win32/mutex.rb', line 38 def name @name end |
Class Method Details
.open(name, inherit = true, &block) ⇒ Object
Open an existing Mutex by name
. The inherit
argument sets whether or not the object was opened such that a process created by the CreateProcess() function (a Windows API function) can inherit the handle. The default is true.
This method is essentially identical to Mutex.new, except that the option for initial_owner
cannot be set (since it is already set). Also, this method will raise a Mutex::Error if the mutex doesn’t already exist.
If you want “open or create” semantics, then use Mutex.new.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/win32/mutex.rb', line 102 def self.open(name, inherit=true, &block) if name.encoding.to_s != 'UTF-16LE' name = name + 0.chr name.encode!('UTF-16LE') end begin # The OpenMutex() call here is strictly to force an error if the user # tries to open a mutex that doesn't already exist. handle = OpenMutexW(MUTEX_ALL_ACCESS, inherit, name) if handle == 0 || handle == INVALID_HANDLE_VALUE raise SystemCallError.new("OpenMutex", FFI.errno) end ensure CloseHandle(handle) if handle && handle > 0 end self.new(false, name, inherit, &block) end |
Instance Method Details
#inheritable? ⇒ Boolean
Returns whether or not the object was opened such that a process created by the CreateProcess() function (a Windows API function) can inherit the handle. The default is true.
142 143 144 |
# File 'lib/win32/mutex.rb', line 142 def inheritable? @inherit end |
#initial_owner? ⇒ Boolean
Returns whether or not the calling thread has initial ownership of the mutex object.
134 135 136 |
# File 'lib/win32/mutex.rb', line 134 def initial_owner? @initial_owner end |
#release ⇒ Object
Releases ownership of the mutex.
125 126 127 128 129 |
# File 'lib/win32/mutex.rb', line 125 def release unless ReleaseMutex(@handle) raise SystemCallError.new("ReleaseMutex", FFI.errno) end end |