Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SharedSync class #10868

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libutil/posix-source-accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ bool PosixSourceAccessor::pathExists(const CanonPath & path)

std::optional<struct stat> PosixSourceAccessor::cachedLstat(const CanonPath & path)
{
static Sync<std::unordered_map<Path, std::optional<struct stat>>> _cache;
static SharedSync<std::unordered_map<Path, std::optional<struct stat>>> _cache;

// Note: we convert std::filesystem::path to Path because the
// former is not hashable on libc++.
Path absPath = makeAbsPath(path).string();

{
auto cache(_cache.lock());
auto cache(_cache.read());
auto i = cache->find(absPath);
if (i != cache->end()) return i->second;
}
Expand Down
53 changes: 40 additions & 13 deletions src/libutil/sync.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cstdlib>
#include <mutex>
#include <shared_mutex>
#include <condition_variable>
#include <cassert>

Expand All @@ -24,32 +25,31 @@ namespace nix {
* Here, "data" is automatically unlocked when "data_" goes out of
* scope.
*/
template<class T, class M = std::mutex>
class Sync
template<class T, class M, class WL, class RL>
class SyncBase
{
private:
M mutex;
T data;

public:

Sync() { }
Sync(const T & data) : data(data) { }
Sync(T && data) noexcept : data(std::move(data)) { }
SyncBase() { }
SyncBase(const T & data) : data(data) { }
SyncBase(T && data) noexcept : data(std::move(data)) { }

template<class L>
class Lock
{
private:
Sync * s;
std::unique_lock<M> lk;
friend Sync;
Lock(Sync * s) : s(s), lk(s->mutex) { }
protected:
SyncBase * s;
L lk;
friend SyncBase;
Lock(SyncBase * s) : s(s), lk(s->mutex) { }
public:
Lock(Lock && l) : s(l.s) { abort(); }
Lock(const Lock & l) = delete;
~Lock() { }
T * operator -> () { return &s->data; }
T & operator * () { return s->data; }

void wait(std::condition_variable & cv)
{
Expand Down Expand Up @@ -83,7 +83,34 @@ public:
}
};

Lock lock() { return Lock(this); }
struct WriteLock : Lock<WL>
{
T * operator -> () { return &WriteLock::s->data; }
T & operator * () { return WriteLock::s->data; }
};

/**
* Acquire write (exclusive) access to the inner value.
*/
WriteLock lock() { return WriteLock(this); }

struct ReadLock : Lock<RL>
{
const T * operator -> () { return &ReadLock::s->data; }
const T & operator * () { return ReadLock::s->data; }
};

/**
* Acquire read access to the inner value. When using
* `std::shared_mutex`, this will use a shared lock.
*/
ReadLock read() const { return ReadLock(const_cast<SyncBase *>(this)); }
};

template<class T>
using Sync = SyncBase<T, std::mutex, std::unique_lock<std::mutex>, std::unique_lock<std::mutex>>;

template<class T>
using SharedSync = SyncBase<T, std::shared_mutex, std::unique_lock<std::shared_mutex>, std::shared_lock<std::shared_mutex>>;

}
Loading