Giter VIP home page Giter VIP logo

file-lock's Introduction

File Lock

A multithreaded multiprocessed file locking mechanism compatible with std::unique_lock and std::shared_lock. A caveat to this FileLock system is that :

  • Multithreaded file sharing have to be done on the same FileLock object.

Requirements

Since this process uses C++ modules, the following are the requirements :

cmake >= 3.28
clang >= 17 or equivalent gcc (with modules support p1689)
ninja >= 1.11.1

If your system does not satisfy the requirement, you can use the docker container to build python bindings if that is what you need. The python bindings have been tested to work under ubuntu22.04

Constructing a File Lock

To construct a file

#include <filesystem>
#include <unique_lock>
#include <shared_lock>
import file_lock;

auto file_path = std::filesystem::path {R"(some path to your file)"};
auto lock = file_lock::BasicMutex { file_path };
lock.lock(); // Locks the file for writing (blocking)
lock.unlock(); // Unlocks the file
/*
  Try to lock the file (nonblocking), returns true if the file is successfully locked
  otherwise returns false.
*/
lock.try_lock(); 

// Shared version
lock.lock_shared();
lock.unlock_shared();
lock.try_lock_shared();

// Also works with unique_lock and shared_lock RAII based locks
std::unique_lock unique_lock { lock };
std::shared_lock shared_lock { lock };

Running Tests

In the build folder, run the following to test

ninja multiprocessing_file_lock_tests
./multiprocessing_file_lock_tests

Currently there is a bug in the test that causes the child process to not exit. If you encounter such a condition during testing. Please just restart the test and see if it works.

Linking via CMake

target_link_libraries(<target> multiprocessing_fileLock)

Compiling for a Python Build

CXX=$(<path to your clang>) cmake -B build -DCMAKE_BUILD_TYPE=Release -GNinja
cd build
ninja multiprocessing_file_lock_python

The above commands will produce a file_lock.so file which you can import into a python application.

Python API Documentation

Assume that file_lock.so contains the following python definition with strict type definitions.

class FileMutex:
    def __init__(self, file_path : str):
        ...
      
    def lock() -> None:
        ...
    def unlock() -> None:
        ...
    def try_lock() -> bool:
        ...
    def lock_shared() -> None:
        ...
    def unlock_shared() -> None:
        ...
    def try_unlock_shared() -> bool:
        ...
    def file_path() -> str:
        ...

file-lock's People

Contributors

jowillianto avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

file-lock's Issues

Garbage Collection

  • MutexStore needs to be garbage collection as its size can grow infinitely in a long running application. Hence, it is important to limit the size of this mutex store to ensure that this store works in the long run

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.