LLFIO  v2.00
llfio_v2_xxx::algorithm::shared_fs_mutex::lock_files Class Reference

Many entity exclusive compatibility file system based lock. More...

#include "lock_files.hpp"

Inheritance diagram for llfio_v2_xxx::algorithm::shared_fs_mutex::lock_files:
llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex

Public Types

using entity_type = shared_fs_mutex::entity_type
 The type of an entity id.
 
using entities_type = shared_fs_mutex::entities_type
 The type of a sequence of entities.
 

Public Member Functions

 lock_files (const lock_files &)=delete
 No copy construction.
 
lock_filesoperator= (const lock_files &)=delete
 No copy assignment.
 
 lock_files (lock_files &&o) noexcept
 Move constructor.
 
lock_filesoperator= (lock_files &&o) noexcept
 Move assign.
 
const path_handlepath () const noexcept
 Return the path to the directory being used for this lock.
 
virtual void unlock (entities_type, unsigned long long) noexcept final
 Unlock a previously locked sequence of entities.
 
entity_type entity_from_buffer (const char *buffer, size_t bytes, bool exclusive=true) noexcept
 Generates an entity id from a sequence of bytes.
 
template<typename T >
entity_type entity_from_string (const std::basic_string< T > &str, bool exclusive=true) noexcept
 Generates an entity id from a string.
 
entity_type random_entity (bool exclusive=true) noexcept
 Generates a cryptographically random entity id.
 
void fill_random_entities (span< entity_type > seq, bool exclusive=true) noexcept
 Fills a sequence of entity ids with cryptographic randomness. Much faster than calling random_entity() individually.
 
result< entities_guardlock (entities_type entities, deadline d=deadline(), bool spin_not_sleep=false) noexcept
 Lock all of a sequence of entities for exclusive or shared access.
 
result< entities_guardlock (entity_type entity, deadline d=deadline(), bool spin_not_sleep=false) noexcept
 Lock a single entity for exclusive or shared access.
 
result< entities_guardtry_lock (entities_type entities) noexcept
 Try to lock all of a sequence of entities for exclusive or shared access.
 
result< entities_guardtry_lock (entity_type entity) noexcept
 Try to lock a single entity for exclusive or shared access.
 

Static Public Member Functions

static result< lock_filesfs_mutex_lock_files (const path_handle &lockdir) noexcept
 Initialises a shared filing system mutex using the directory at lockdir which MUST stay valid for the duration of this lock.
 

Protected Member Functions

virtual result< void > _lock (entities_guard &out, deadline d, bool spin_not_sleep) noexcept final
 

Detailed Description

Many entity exclusive compatibility file system based lock.

This is a very simple many entity shared mutex likely to work almost anywhere without surprises. It works by trying to exclusively create a file called the hex of the entity id. If it fails to exclusively create any file, it deletes all previously created files, randomises the order and tries locking them again until success. The only real reason to use this implementation is its excellent compatibility with almost everything, most users will want byte_ranges instead.

  • Compatible with all networked file systems.
  • Linear complexity to number of concurrent users.
  • Exponential complexity to number of contended entities being concurrently locked.
  • Requests for shared locks are treated as if for exclusive locks.

Caveats:

  • No ability to sleep until a lock becomes free, so CPUs are spun at 100%.
  • On POSIX only sudden process exit with locks held will deadlock all other users by leaving stale files around.
  • Costs a file descriptor per entity locked.
  • Sudden power loss during use will deadlock first user after reboot, again due to stale files.
  • Currently this implementation does not permit more than one lock() per instance as the lock information is stored as member data. Creating multiple instances referring to the same path works fine. This could be fixed easily, but it would require a memory allocation per lock and user demand that this is actually a problem in practice.
  • Leaves many 16 character long hexadecimal named files in the supplied directory which may confuse users. Tip: create a hidden lockfile directory.

Fixing the stale lock file problem could be quite trivial - simply byte range lock the first byte in the lock file to detect when a lock file is stale. However in this situation using the byte_ranges algorithm would be far superior, so implementing stale lock file clean up is left up to the user.


The documentation for this class was generated from the following file: