Try to locks the file during the specified time.

Synopsis

Declared in <llvm/Support/FileSystem.h>

std::error_code
tryLockFile(
    int FD,
    std::chrono::milliseconds Timeout = std::chrono::milliseconds(0),
    LockKind Kind = LockKind::Exclusive);

Description

This function implements advisory locking on entire file. If it returns errc::success, the file is locked by the calling process. Until the process unlocks the file by calling unlockFile, all attempts to lock the same file will fail/block. The process that locked the file may assume that none of other processes read or write this file, provided that all processes lock the file prior to accessing its content.

Note

Care should be taken when using this function in a multithreaded context, as it may not prevent other threads in the same process from obtaining a lock on the same file, even if they are using a different file descriptor.

Return Value

errc::success if lock is successfully obtained, errc::no_lock_available if the file cannot be locked, or platform‐specific error_code otherwise.

Parameters

Name

Description

FD

The descriptor representing the file to lock.

Timeout

Time in milliseconds that the process should wait before reporting lock failure. Zero value means try to get lock only once.

Kind

The kind of the lock used (exclusive/shared).

Created with MrDocs