def acquire(self): """Tries to acquire the singleton. Returns: True if there was no previous process, False if this process is a duplicate and should exit. """ if sys.platform == 'win32': # Create a global mutex. Make the mutex so that it disapear automatically # when the process dies. The handle is not inherited so task_runner # doesn't get to keep it alive. # pylint: disable=undefined-variable self.handle = wintypes.windll.kernel32.CreateMutexW( wintypes.c_int(0), wintypes.c_int(-1), wintypes.create_unicode_buffer(self.key)) last_error = wintypes.GetLastError() logging.info('%s = %s ; %s', self.key, self.handle, last_error) if not self.handle: return False # ERROR_ALREADY_EXISTS if last_error == 183: self.release() return bool(self.handle) else: self.handle = open(self.key, 'wb') try: fcntl.flock(self.handle, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError: self.handle.close() self.handle = None return False return True
def __init__(self, mutexName, timeoutMs): mutexName = 'Local\\' + mutexName self._mutex = windll.kernel32.CreateMutexW(wintypes.c_int(0), wintypes.c_int(0), unicode(mutexName)) self._timeoutMs = timeoutMs self._acquired = False assert self._mutex
def __init__(self, mutexName, timeoutMs): mutexName = 'Local\\' + mutexName self._mutex = windll.kernel32.CreateMutexW( wintypes.c_int(0), wintypes.c_int(0), unicode(mutexName)) self._timeoutMs = timeoutMs self._acquired = False assert self._mutex
def __init__(self, space, access): self.space = space self.size = 0 self.pos = 0 self.access = access if _MS_WINDOWS: self.map_handle = wintypes.c_int() self.file_handle = wintypes.c_int() self.tagname = "" elif _POSIX: self.fd = -1 self.closed = False
def __enter__(self): name = 'Local\\%s' % BASE_DIR.replace('\\', '_').replace(':', '_') self.mutex = windll.kernel32.CreateMutexW( wintypes.c_int(0), wintypes.c_int(0), wintypes.create_unicode_buffer(name)) assert self.mutex result = windll.kernel32.WaitForSingleObject( self.mutex, wintypes.c_int(0xFFFFFFFF)) # 0x80 means another process was killed without releasing the mutex, but # that this process has been given ownership. This is fine for our # purposes. assert result in (0, 0x80), ("%s, %s" % (result, windll.kernel32.GetLastError()))
def __enter__(self): name = 'Local\\%s' % BASE_DIR.replace('\\', '_').replace(':', '_') self.mutex = windll.kernel32.CreateMutexW( wintypes.c_int(0), wintypes.c_int(0), wintypes.create_unicode_buffer(name)) assert self.mutex result = windll.kernel32.WaitForSingleObject( self.mutex, wintypes.c_int(0xFFFFFFFF)) # 0x80 means another process was killed without releasing the mutex, but # that this process has been given ownership. This is fine for our # purposes. assert result in (0, 0x80), ( "%s, %s" % (result, windll.kernel32.GetLastError()))
def __enter__(self): # Only lock if we're using the default CC, not when using goma. if self.cc != DEFAULT_CC: return name = 'Local\\c99conv_cl_preprocess_mutex' self.mutex = windll.kernel32.CreateMutexW( wintypes.c_int(0), wintypes.c_int(0), wintypes.create_unicode_buffer(name)) assert self.mutex result = windll.kernel32.WaitForSingleObject( self.mutex, wintypes.c_int(0xFFFFFFFF)) # 0x80 means another process was killed without releasing the mutex, but # that this process has been given ownership. This is fine for our # purposes. assert result in (0, 0x80), ( "%s, %s" % (result, windll.kernel32.GetLastError()))
def acquire(self): WAIT_ABANDONED = 0x00000080 result = windll.kernel32.WaitForSingleObject( self._mutex, wintypes.c_int(self._timeoutMs)) if result != 0 and result != WAIT_ABANDONED: errorString = 'Error! WaitForSingleObject returns {result}, last error {error}'.format( result=result, error=windll.kernel32.GetLastError()) raise ObjectCacheLockException(errorString) self._acquired = True
def acquire(self): """Tries to acquire the singleton. Returns: True if there was no previous process, False if this process is a duplicate and should exit. """ if sys.platform == 'win32': # Create a global mutex. Make the mutex so that it disapear automatically # when the process dies. The handle is not inherited so task_runner # doesn't get to keep it alive. # pylint: disable=undefined-variable self.handle = wintypes.windll.kernel32.CreateMutexW( wintypes.c_int(0), wintypes.c_int(-1), wintypes.create_unicode_buffer(self.key)) last_error = wintypes.GetLastError() logging.info('[singleton] acquire: %s = %s ; %s', self.key, self.handle, last_error) if not self.handle: return False # ERROR_ALREADY_EXISTS if last_error == 183: self.release() return bool(self.handle) else: self.handle = open(self.key, 'a+b') try: fcntl.flock(self.handle, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError: # There's a small race condition where it could report a previous pid. logging.exception('Singleton "%s" is held by "%s"', self.key, self.handle.read()) self.handle.close() self.handle = None return False logging.info('[singleton] acquire: %s = %s', self.key, self.handle) self.handle.seek(0, os.SEEK_SET) self.handle.truncate(0) self.handle.write(str(os.getpid()).encode('utf-8')) return True
def decode_command_line_args(argv): command_line = kernel32.GetCommandLineW() if not command_line: return argv argc = c_int(0) argv_w = shell32.CommandLineToArgvW(command_line, byref(argc)) try: if not argv_w: return argv if argc.value <= 0: return argv start = argc.value - len(argv) ret_args = argv_w[start:argc.value] return ret_args finally: kernel32.LocalFree(argv_w)