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

Safe many entity shared/exclusive file system based lock. More...

#include "safe_byte_ranges.hpp"

Inheritance diagram for afio_v2_xxx::algorithm::shared_fs_mutex::safe_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

 safe_byte_ranges (const safe_byte_ranges &)=delete
 No copy construction.
safe_byte_rangesoperator= (const safe_byte_ranges &)=delete
 No copy assignment.
 safe_byte_ranges (safe_byte_ranges &&o) noexcept
 Move constructor.
safe_byte_rangesoperator= (safe_byte_ranges &&o) noexcept
 Move assign.
virtual void unlock (entities_type entities, unsigned long long hint) 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< safe_byte_rangesfs_mutex_safe_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

Safe many entity shared/exclusive file system based lock.

See also

POSIX requires that byte range locks have insane semantics, namely that:

  • Any fd closed on an inode must release all byte range locks on that inode for all other fds.
  • Threads replace each other's locks.

This makes the use of byte range locks in modern code highly problematic.

This implementation of shared_fs_mutex exclusively uses byte range locks on an inode for exclusion with other processes, but on POSIX only also layers on top:

  • Keeps a process-wide store of reference counted fd's per lock in the process, thus preventing byte range locks ever being dropped unexpectedly.
  • A process-local thread aware layer, allowing byte range locks to be held by a thread and excluding other threads as well as processes.
  • A thread may add an additional exclusive or shared lock on top of its existing exclusive or shared lock, but unlocks always unlock the exclusive lock first. This choice of behaviour is to match Microsoft Windows' behaviour to aid writing portable code.

On Microsoft Windows safe_byte_ranges is typedefed to byte_ranges, as the Windows byte range locking API already works as described above.


  • 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.
  • 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.


  • 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.
  • Unavoidably these locks will be a good bit slower than byte_ranges.

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