Skip to content

Commit

Permalink
Add support for Windows phmap::srwlock
Browse files Browse the repository at this point in the history
  • Loading branch information
greg7mdp committed Nov 19, 2023
1 parent d51c761 commit 9248dcf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
11 changes: 1 addition & 10 deletions examples/lazy_emplace_l.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@
#include <vector>
#include <ppl.h>

class srwlock {
SRWLOCK _lock;

public:
srwlock() { InitializeSRWLock(&_lock); }
void lock() { AcquireSRWLockExclusive(&_lock); }
void unlock() { ReleaseSRWLockExclusive(&_lock); }
};

using Map = phmap::parallel_flat_hash_map<std::string, int, phmap::priv::hash_default_hash<std::string>,
phmap::priv::hash_default_eq<std::string>,
std::allocator<std::pair<const std::string, int>>, 8, srwlock>;
std::allocator<std::pair<const std::string, int>>, 8, phmap::srwlock>;

class Dict
{
Expand Down
37 changes: 37 additions & 0 deletions parallel_hashmap/phmap_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -5000,6 +5000,7 @@ class LockableImpl<phmap::NullMutex>: public phmap::NullMutex

// --------------------------------------------------------------------------
// Abseil Mutex support (read and write lock support)
// use: `phmap::AbslMutex` instead of `std::mutex`
// --------------------------------------------------------------------------
#ifdef ABSL_SYNCHRONIZATION_MUTEX_H_

Expand Down Expand Up @@ -5030,6 +5031,42 @@ class LockableImpl<phmap::NullMutex>: public phmap::NullMutex

#endif

// --------------------------------------------------------------------------
// Microsoft SRWLOCK support (read and write lock support)
// use: `phmap::srwlock` instead of `std::mutex`
// --------------------------------------------------------------------------
#if defined(_MSVC_LANG) && defined(SRWLOCK_INIT)

class srwlock {
SRWLOCK _lock;
public:
srwlock() { InitializeSRWLock(&_lock); }
void lock() { AcquireSRWLockExclusive(&_lock); }
void unlock() { ReleaseSRWLockExclusive(&_lock); }
bool try_lock() { return !!TryAcquireSRWLockExclusive(&_lock); }
void lock_shared() { AcquireSRWLockShared(&_lock); }
void unlock_shared() { ReleaseSRWLockShared(&_lock); }
bool try_lock_shared() { return !!TryAcquireSRWLockShared(&_lock); }
};


template<>
class LockableImpl<srwlock> : public srwlock
{
public:
using mutex_type = srwlock;
using Base = LockableBaseImpl<srwlock>;
using SharedLock = typename Base::ReadLock;
using ReadWriteLock = typename Base::ReadWriteLock;
using UpgradeLock = typename Base::WriteLock;
using UniqueLock = typename Base::WriteLock;
using SharedLocks = typename Base::ReadLocks;
using UniqueLocks = typename Base::WriteLocks;
using UpgradeToUnique = typename Base::DoNothing; // we already have unique ownership
};

#endif

// --------------------------------------------------------------------------
// Boost shared_mutex support (read and write lock support)
// --------------------------------------------------------------------------
Expand Down

0 comments on commit 9248dcf

Please sign in to comment.