Source/Common/CrossProcessMutex.h
// Acquires the mutex. If 'wait' is true and mutex is acquired by someone else then
// function waits until mutex is released
// Returns false if !wait and lock cannot be acquired, or in case of a system error that prevents us from acquiring the lock.
Acquires the mutex. If 'wait' is true and mutex is acquired by someone else then
function waits until mutex is released
Returns false if !wait and lock cannot be acquired, or in case of a system error that prevents us from acquiring the lock.
Acquires the mutex . If ' wait ' is true and mutex is acquired by someone else then function waits until mutex is released Returns false if !wait and lock cannot be acquired or in case of a system error that prevents us from acquiring the lock .
bool Acquire(bool wait)
    {
        assert(m_fd == -1);
        for (;;)
        {
            int fd = open(m_fileName.c_str(), O_WRONLY | O_CREAT, 0666);
            if (fd < 0)
                RuntimeError("Acquire: Failed to open lock file %s: %s.", m_fileName.c_str(), strerror(errno));
            memset(&m_lock, 0, sizeof(m_lock));
            m_lock.l_type = F_WRLCK;
            setupTimeout(1);
            int r = fcntl(fd, wait ? F_SETLKW : F_SETLK, &m_lock);
            if (errno == EINTR)
            {
                sleep(1);
                close(fd);
                continue;
            }
            if (r != 0)
            {
                close(fd);
                return false;
            }
            struct stat before, after;
            fstat(fd, &before);
            if (stat(m_fileName.c_str(), &after) != 0 || before.st_ino != after.st_ino)
            {
                close(fd);
                continue;
            }
            else
            {
                m_fd = fd;
                return true;
            }
        }
    }
bool Acquire ( bool wait ) { assert ( m_fd == -1 ) ; for ( ; ; ) { int fd = open ( m_fileName . c_str ( ) , O_WRONLY | O_CREAT , 0666 ) ; if ( fd < 0 ) RuntimeError ( " " , m_fileName . c_str ( ) , strerror ( errno ) ) ; memset ( & m_lock , 0 , sizeof ( m_lock ) ) ; m_lock . l_type = F_WRLCK ; setupTimeout ( 1 ) ; int r = fcntl ( fd , wait ? F_SETLKW : F_SETLK , & m_lock ) ; if ( errno == EINTR ) { sleep ( 1 ) ; close ( fd ) ; continue ; } if ( r != 0 ) { close ( fd ) ; return false ; } struct stat before , after ; fstat ( fd , & before ) ; if ( stat ( m_fileName . c_str ( ) , & after ) != 0 || before . st_ino != after . st_ino ) { close ( fd ) ; continue ; } else { m_fd = fd ; return true ; } } }
Acquires the mutex.
Acquires the mutex .
// opening a lock file // locking it with the fcntl API // BUG: fcntl call with F_SETLKW doesn't always reliably detect when lock is released // As a workaround, using alarm() for interupting fcntl if it waits more than 1 second // retrying in the case of signal or timeout // acquire failed // we own the exclusive lock on file descriptor, but we need to double-check // that the lock file wasn't deleted and/or re-created; // checking this by comparing inode numbers // we have a race with 'unlink' call in Release() // our lock is held to the previous instance of the file; // this is not a problem, we just need to retry locking the new file // lock acquired successfully