# class Chef::ReservedNames::Win32::Mutex

### Attributes

handle[R]
name[R]

### Public Class Methods

new(name) click to toggle source

    # File lib/chef/win32/mutex.rb, line 26
    def initialize(name)
      @name = name
      create_system_mutex
    end

### Public Instance Methods

release() click to toggle source

Releaes the mutex

    # File lib/chef/win32/mutex.rb, line 71
          def release
            # http://msdn.microsoft.com/en-us/library/windows/desktop/ms685066(v=vs.85).aspx
            # Note that release method needs to be called more than once
            # if mutex is acquired more than once.
            unless ReleaseMutex(handle)
              # Don't fail things in here if we can't release the mutex.
              # Because it will be automatically released when the owner
              # of the process goes away and this class is only being used
              # to synchronize chef-clients runs on a node.
              Chef::Log.error("Can not release mutex '#{name}'. This might cause issues \
    if the mutex is attempted to be acquired by other threads.")
              Chef::ReservedNames::Win32::Error.raise!
            end
          end

test() click to toggle source

Attempts to grab the mutex. Returns true if the mutex is grabbed or if it’s already owned; false otherwise.

    # File lib/chef/win32/mutex.rb, line 38
    def test
      WaitForSingleObject(handle, 0) == WAIT_OBJECT_0
    end

wait() click to toggle source

Attempts to grab the mutex and waits until it is acquired.

    # File lib/chef/win32/mutex.rb, line 44
    def wait
      loop do
        wait_result = WaitForSingleObject(handle, 1000)
        case wait_result
        when WAIT_TIMEOUT
          # We are periodically waking up in order to give ruby a
          # chance to process any signal it got while we were
          # sleeping. This condition shouldn't contain any logic
          # other than sleeping.
          sleep 0.1
        when WAIT_ABANDONED
          # Previous owner of the mutex died before it can release the
          # mutex. Log a warning and continue.
          Chef::Log.debug "Existing owner of the mutex exited prematurely."
          break
        when WAIT_OBJECT_0
          # Mutex is successfully acquired.
          break
        else
          Chef::Log.error("Failed to acquire system mutex '#{name}'. Return code: #{wait_result}")
          Chef::ReservedNames::Win32::Error.raise!
        end
      end
    end