Skip to content

Commit

Permalink
Cleanup and fix V3SharedMutex
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Glebocki committed Jan 30, 2024
1 parent 1ab0ed3 commit 22f1eb1
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/V3Mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ using V3RecursiveMutex = V3MutexImp<std::recursive_mutex>;

#if defined(VL_SHARED_MUTEX_IMPLEMENTATION_PTHREAD)

// TODO(mglb): Wrap std::shared_timed_mutex and ignore timing methods
// https://en.cppreference.com/w/cpp/thread/shared_timed_mutex

// PThreads-based implementation of shared mutex
class VL_CAPABILITY("shared mutex") V3SharedMutex final {
pthread_rwlock_t m_mutex;
const char* m_lastLockFile = nullptr;
int m_lastLockLine = 0;
bool m_lastLockExclusive = false;

public:
Expand All @@ -161,38 +162,28 @@ class VL_CAPABILITY("shared mutex") V3SharedMutex final {

// Exclusive locking

void lock(const char* file = __builtin_FILE(), int line = __builtin_LINE())
VL_ACQUIRE() VL_MT_SAFE {
void lock() VL_ACQUIRE() VL_MT_SAFE {
pthread_rwlock_wrlock(&m_mutex);
m_lastLockFile = file;
m_lastLockLine = line;
m_lastLockExclusive = true;
}

// TODO(mglb): bool try_lock();

void unlock() VL_RELEASE() VL_MT_SAFE {
pthread_rwlock_unlock(&m_mutex);
m_lastLockFile = nullptr;
m_lastLockLine = 0;
}

// Shared locking

void lock_shared(const char* file = __builtin_FILE(), int line = __builtin_LINE())
VL_ACQUIRE_SHARED() VL_MT_SAFE {
void lock_shared() VL_ACQUIRE_SHARED() VL_MT_SAFE {
pthread_rwlock_rdlock(&m_mutex);
m_lastLockFile = file;
m_lastLockLine = line;
m_lastLockExclusive = false;
}

// TODO(mglb): bool try_lock_shared();

void unlock_shared() VL_RELEASE_SHARED() VL_MT_SAFE {
pthread_rwlock_unlock(&m_mutex);
m_lastLockFile = nullptr;
m_lastLockLine = 0;
}

void assumeLocked() VL_ASSERT_CAPABILITY(this) VL_MT_SAFE {}
Expand Down Expand Up @@ -269,6 +260,10 @@ class VL_CAPABILITY("shared mutex") V3SharedMutex final {
if (m_sharedCount == 0) { m_noSharedLocksCond.notify_one(); }
}
}

void assumeLocked() VL_ASSERT_CAPABILITY(this) VL_MT_SAFE {}

void assumeUnlocked() VL_ASSERT_CAPABILITY(!this) VL_MT_SAFE {}
};

#endif
Expand Down

0 comments on commit 22f1eb1

Please sign in to comment.