Class FileLock

Inheritance Relationships

Base Type

Class Documentation

class FileLock : public DesignPattern::Creation<FileLock, FileLockError>

Posix file lock C++ wrapping class Following RAII, the lock is acquired on creation and released on destruction. Releasing the locks works even if the process crashes with a segfault or using SIGKILL. ‘lslocks’ can be used to display all system-wide locks (see man page)

iox::posix::FileLock::create(nameOfmyLock)
    .and_then([] { std::cout << "We aquired the lock!" << std::endl; })
    .or_else([](auto& error) {
        if (error == FileLockError::LOCKED_BY_OTHER_PROCESS)
        {
            std::cout << "Some other process is running and holds the lock!" << std::endl;
        }
    });

Public Types

using FileName_t = cxx::string<FILENAME_LENGTH>
using PathName_t = cxx::string<platform::IOX_MAX_PATH_LENGTH>

Public Functions

FileLock(const FileLock&) = delete
FileLock &operator=(const FileLock&) = delete
FileLock(FileLock &&rhs) noexcept
FileLock &operator=(FileLock &&rhs) noexcept
~FileLock() noexcept

Public Static Attributes

static constexpr int32_t ERROR_CODE = -1
static constexpr int32_t INVALID_FD = -1
static constexpr const char LOCK_FILE_SUFFIX[] = ".lock"
static constexpr uint64_t FILENAME_LENGTH = platform::IOX_MAX_FILENAME_LENGTH - sizeof(platform::IOX_LOCK_FILE_PATH_PREFIX) / sizeof(char) - sizeof(LOCK_FILE_SUFFIX) / sizeof(char)

Friends

friend class DesignPattern::Creation< FileLock, FileLockError >