10#include "llvm/Config/config.h"
15#if __has_include(<sys/file.h>)
24#if __has_include(<fcntl.h>)
34 return std::error_code();
35 return std::error_code(errno, std::generic_category());
46 if (flock(FD, LOCK_UN) == 0)
47 return std::error_code();
48 return std::error_code(errno, std::generic_category());
61 auto Start = std::chrono::steady_clock::now();
66 return std::error_code();
68 if (
Error == EWOULDBLOCK) {
70 std::this_thread::sleep_for(std::chrono::milliseconds(1));
73 return std::error_code(
Error, std::generic_category());
74 }
while (std::chrono::steady_clock::now() < End);
87 if (EC == std::errc::not_supported)
90#if defined(HAVE_POSIX_FALLOCATE)
91 if (EC == std::errc::invalid_argument && CurrentSize < NewSize && // len > 0
92 NewSize < std::numeric_limits<off_t>::max())
99 "failed to allocate to CAS file: " + EC.message());
101#if defined(HAVE_POSIX_FALLOCATE)
103 if (
int Err = posix_fallocate(FD, CurrentSize, NewSize - CurrentSize))
104 return CreateError(std::error_code(Err, std::generic_category()));
106#elif defined(__APPLE__)
108 FAlloc.fst_flags = F_ALLOCATEALL;
109#if defined(F_ALLOCATEPERSIST) && \
110 defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
111 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 130000
113 FAlloc.fst_flags |= F_ALLOCATEPERSIST;
115 FAlloc.fst_posmode = F_PEOFPOSMODE;
116 FAlloc.fst_offset = 0;
117 FAlloc.fst_length = NewSize - CurrentSize;
118 FAlloc.fst_bytesalloc = 0;
119 if (fcntl(FD, F_PREALLOCATE, &FAlloc))
121 assert(CurrentSize + FAlloc.fst_bytesalloc >= NewSize);
122 return CurrentSize + FAlloc.fst_bytesalloc;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Lightweight error class with error context and mandatory checking.
Tagged union holding either a T or a Error.
std::error_code lockFileThreadSafe(int FD, llvm::sys::fs::LockKind Kind)
Thread-safe alternative to sys::fs::lockFile.
std::error_code unlockFileThreadSafe(int FD)
Thread-safe alternative to sys::fs::unlockFile.
std::error_code tryLockFileThreadSafe(int FD, std::chrono::milliseconds Timeout=std::chrono::milliseconds(0), llvm::sys::fs::LockKind Kind=llvm::sys::fs::LockKind::Exclusive)
Thread-safe alternative to sys::fs::tryLockFile.
Expected< size_t > preallocateFileTail(int FD, size_t CurrentSize, size_t NewSize)
Allocate space for the file FD on disk, if the filesystem supports it.
LLVM_ABI std::error_code lockFile(int FD, LockKind Kind=LockKind::Exclusive)
Lock the file.
LLVM_ABI std::error_code tryLockFile(int FD, std::chrono::milliseconds Timeout=std::chrono::milliseconds(0), LockKind Kind=LockKind::Exclusive)
Try to locks the file during the specified time.
LockKind
An enumeration for the lock kind.
LLVM_ABI std::error_code unlockFile(int FD)
Unlock the file.
This is an optimization pass for GlobalISel generic memory operations.
std::error_code make_error_code(BitcodeError E)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
@ Timeout
Reached timeout while waiting for the owner to release the lock.
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.