LLFIO v2.00
Loading...
Searching...
No Matches
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.

Constructor & Destructor Documentation

◆ lock_files()

llfio_v2_xxx::algorithm::shared_fs_mutex::lock_files::lock_files ( lock_files &&  o)
inlinenoexcept

Move constructor.

96: _path(o._path), _hs(std::move(o._hs)) {}

Member Function Documentation

◆ _lock()

virtual result< void > llfio_v2_xxx::algorithm::shared_fs_mutex::lock_files::_lock ( entities_guard out,
deadline  d,
bool  spin_not_sleep 
)
inlinefinalprotectedvirtualnoexcept

Implements llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex.

122 {
123 LLFIO_LOG_FUNCTION_CALL(this);
124 std::chrono::steady_clock::time_point began_steady;
125 std::chrono::system_clock::time_point end_utc;
126 if(d)
127 {
128 if((d).steady)
129 {
130 began_steady = std::chrono::steady_clock::now();
131 }
132 else
133 {
134 end_utc = (d).to_time_point();
135 }
136 }
137 size_t n;
138 // Create a set of paths to files to exclusively create
139 std::vector<std::string> entity_paths(out.entities.size());
140 for(n = 0; n < out.entities.size(); n++)
141 {
142 auto v = out.entities[n].value;
143 entity_paths[n] = QUICKCPPLIB_NAMESPACE::algorithm::string::to_hex_string(span<char>(reinterpret_cast<char *>(&v), 8));
144 }
145 _hs.resize(out.entities.size());
146 do
147 {
148 auto was_contended = static_cast<size_t>(-1);
149 {
150 auto undo = make_scope_exit([&]() noexcept {
151 // 0 to (n-1) need to be closed
152 if(n > 0)
153 {
154 --n;
155 // Now 0 to n needs to be closed
156 for(; n > 0; n--)
157 {
158 (void) _hs[n].close(); // delete on close semantics deletes the file
159 }
160 (void) _hs[0].close();
161 }
162 });
163 for(n = 0; n < out.entities.size(); n++)
164 {
165 auto ret = file_handle::file(_path, entity_paths[n], file_handle::mode::write, file_handle::creation::only_if_not_exist, file_handle::caching::temporary, file_handle::flag::unlink_on_first_close);
166 if(ret.has_error())
167 {
168 const auto &ec = ret.error();
169 if(ec != errc::resource_unavailable_try_again && ec != errc::file_exists)
170 {
171 return std::move(ret).error();
172 }
173 // Collided with another locker
174 was_contended = n;
175 break;
176 }
177 _hs[n] = std::move(ret.value());
178 }
179 if(n == out.entities.size())
180 {
181 undo.release();
182 }
183 }
184 if(n != out.entities.size())
185 {
186 if(d)
187 {
188 if((d).steady)
189 {
190 if(std::chrono::steady_clock::now() >= (began_steady + std::chrono::nanoseconds((d).nsecs)))
191 {
192 return errc::timed_out;
193 }
194 }
195 else
196 {
197 if(std::chrono::system_clock::now() >= end_utc)
198 {
199 return errc::timed_out;
200 }
201 }
202 }
203 // Move was_contended to front and randomise rest of out.entities
204 std::swap(out.entities[was_contended], out.entities[0]);
205 auto front = out.entities.begin();
206 ++front;
207 QUICKCPPLIB_NAMESPACE::algorithm::small_prng::random_shuffle(front, out.entities.end());
208 // Sleep for a very short time
209 if(!spin_not_sleep)
210 {
211 std::this_thread::yield();
212 }
213 }
214 } while(n < out.entities.size());
215 return success();
216 }
static result< file_handle > file(const path_handle &base, path_view_type path, mode _mode=mode::read, creation _creation=creation::open_existing, caching _caching=caching::all, flag flags=flag::none) noexcept
result< void > close(handle &self) noexcept
Immediately close the native handle type managed by this handle.
Definition handle.hpp:679

◆ entity_from_buffer()

entity_type llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex::entity_from_buffer ( const char *  buffer,
size_t  bytes,
bool  exclusive = true 
)
inlinenoexceptinherited

Generates an entity id from a sequence of bytes.

116 {
117 uint128 hash = QUICKCPPLIB_NAMESPACE::algorithm::hash::fast_hash::hash(buffer, bytes);
118 return {hash.as_longlongs[0] ^ hash.as_longlongs[1], exclusive};
119 }
QUICKCPPLIB_NAMESPACE::integers128::uint128 uint128
Unsigned 128 bit integer.
Definition base.hpp:45
@ exclusive
Exclude those requesting any kind of lock on the same inode.

◆ entity_from_string()

template<typename T >
entity_type llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex::entity_from_string ( const std::basic_string< T > &  str,
bool  exclusive = true 
)
inlinenoexceptinherited

Generates an entity id from a string.

122 {
123 uint128 hash = QUICKCPPLIB_NAMESPACE::algorithm::hash::fast_hash::hash(str);
124 return {hash.as_longlongs[0] ^ hash.as_longlongs[1], exclusive};
125 }

◆ fill_random_entities()

void llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex::fill_random_entities ( span< entity_type seq,
bool  exclusive = true 
)
inlinenoexceptinherited

Fills a sequence of entity ids with cryptographic randomness. Much faster than calling random_entity() individually.

135 {
136 utils::random_fill(reinterpret_cast<char *>(seq.data()), seq.size() * sizeof(entity_type));
137 for(auto &i : seq)
138 {
139 i.exclusive = exclusive; // NOLINT
140 }
141 }
void random_fill(char *buffer, size_t bytes) noexcept
Fills the buffer supplied with cryptographically strong randomness. Uses the OS kernel API.

◆ fs_mutex_lock_files()

static result< lock_files > llfio_v2_xxx::algorithm::shared_fs_mutex::lock_files::fs_mutex_lock_files ( const path_handle lockdir)
inlinestaticnoexcept

Initialises a shared filing system mutex using the directory at lockdir which MUST stay valid for the duration of this lock.

112 {
113 LLFIO_LOG_FUNCTION_CALL(0);
114 return lock_files(lockdir);
115 }

◆ lock() [1/2]

result< entities_guard > llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex::lock ( entities_type  entities,
deadline  d = deadline(),
bool  spin_not_sleep = false 
)
inlinenoexceptinherited

Lock all of a sequence of entities for exclusive or shared access.

222 {
223 entities_guard ret(this, entities);
224 OUTCOME_TRYV(_lock(ret, d, spin_not_sleep));
225 return {std::move(ret)};
226 }

◆ lock() [2/2]

result< entities_guard > llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex::lock ( entity_type  entity,
deadline  d = deadline(),
bool  spin_not_sleep = false 
)
inlinenoexceptinherited

Lock a single entity for exclusive or shared access.

229 {
230 entities_guard ret(this, entity);
231 OUTCOME_TRYV(_lock(ret, d, spin_not_sleep));
232 return {std::move(ret)};
233 }

◆ operator=()

lock_files & llfio_v2_xxx::algorithm::shared_fs_mutex::lock_files::operator= ( lock_files &&  o)
inlinenoexcept

Move assign.

99 {
100 if(this == &o)
101 {
102 return *this;
103 }
104 this->~lock_files();
105 new(this) lock_files(std::move(o));
106 return *this;
107 }

◆ path()

const path_handle & llfio_v2_xxx::algorithm::shared_fs_mutex::lock_files::path ( ) const
inlinenoexcept

Return the path to the directory being used for this lock.

118{ return _path; }

◆ random_entity()

entity_type llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex::random_entity ( bool  exclusive = true)
inlinenoexceptinherited

Generates a cryptographically random entity id.

128 {
130 utils::random_fill(reinterpret_cast<char *>(&v), sizeof(v));
131 return {v, exclusive};
132 }
handle::extent_type value_type
The type backing the value.
Definition base.hpp:63

◆ try_lock() [1/2]

result< entities_guard > llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex::try_lock ( entities_type  entities)
inlinenoexceptinherited

Try to lock all of a sequence of entities for exclusive or shared access.

235{ return lock(entities, deadline(std::chrono::seconds(0))); }
result< entities_guard > lock(entities_type entities, deadline d=deadline(), bool spin_not_sleep=false) noexcept
Lock all of a sequence of entities for exclusive or shared access.
Definition base.hpp:221

◆ try_lock() [2/2]

result< entities_guard > llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex::try_lock ( entity_type  entity)
inlinenoexceptinherited

Try to lock a single entity for exclusive or shared access.

237{ return lock(entity, deadline(std::chrono::seconds(0))); }

◆ unlock()

virtual void llfio_v2_xxx::algorithm::shared_fs_mutex::lock_files::unlock ( entities_type  entities,
unsigned long long  hint 
)
inlinefinalvirtualnoexcept

Unlock a previously locked sequence of entities.

Implements llfio_v2_xxx::algorithm::shared_fs_mutex::shared_fs_mutex.

220 {
221 LLFIO_LOG_FUNCTION_CALL(this);
222 for(auto &i : _hs)
223 {
224 (void) i.close(); // delete on close semantics deletes the file
225 }
226 }

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