AFIO  v2.00 late alpha
afio_v2_xxx::algorithm::shared_fs_mutex::byte_ranges Class Reference

Many entity shared/exclusive file system based lock. More...

#include "byte_ranges.hpp"

Inheritance diagram for afio_v2_xxx::algorithm::shared_fs_mutex::byte_ranges:

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

 byte_ranges (const byte_ranges &)=delete
 No copy construction.
byte_rangesoperator= (const byte_ranges &)=delete
 No copy assignment.
 byte_ranges (byte_ranges &&o) noexcept
 Move constructor.
byte_rangesoperator= (byte_ranges &&o) noexcept
 Move assign.
const file_handlehandle () const noexcept
 Return the handle to file being used for this lock.
virtual void unlock (entities_type entities, 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< byte_rangesfs_mutex_byte_ranges (const path_handle &base, path_view lockfile) noexcept
 Initialises a shared filing system mutex using the file at lockfile.

Protected Member Functions

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

Detailed Description

Many entity shared/exclusive file system based lock.

This is a simple many entity shared mutex. It works by locking in the same file the byte at the offset of the entity id. If it fails to lock a byte, it backs out all preceding locks, randomises the order and tries locking them again until success. Needless to say this algorithm puts a lot of strain on your byte range locking implementation, some NFS implementations have been known to fail to cope.

Most users will want to use safe_byte_ranges instead of this class directly.
  • Compatible with networked file systems, though be cautious with older NFS.
  • Linear complexity to number of concurrent users.
  • Exponential complexity to number of entities being concurrently locked, though some OSs provide linear complexity so long as total concurrent waiting processes is CPU core count or less.
  • Does a reasonable job of trying to sleep the thread if any of the entities are locked.
  • Sudden process exit with lock held is recovered from.
  • Sudden power loss during use is recovered from.
  • Safe for multithreaded usage of the same instance.


  • When entities being locked is more than one, the algorithm places the contending lock at the front of the list during the randomisation after lock failure so we can sleep the thread until it becomes free. However, under heavy churn the thread will generally spin, consuming 100% CPU.
  • Byte range locks need to work properly on your system. Misconfiguring NFS or Samba to cause byte range locks to not work right will produce bad outcomes.
  • If your OS doesn't have sane byte range locks (OS X, BSD, older Linuxes) and multiple objects in your process use the same lock file, misoperation will occur. Use lock_files or share a single instance of this class per lock file in this case.
  • If you are on POSIX and the same process relocks an entity a second time, it will release everything on the first unlock. On Windows, the first unlock releases the exclusive lock and the second unlock will release the shared lock.

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