Skip to content

Commit

Permalink
Make it easier to swap in a scalable reader-writer mutex.
Browse files Browse the repository at this point in the history
Change-Id: I04837646dfb41c2b545af54312dac63f47473dd6
Reviewed-on: https://code-review.googlesource.com/c/re2/+/58430
Reviewed-by: Paul Wankadia <[email protected]>
  • Loading branch information
junyer committed Feb 24, 2021
1 parent 377eca0 commit f8e389f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions re2/dfa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ class DFA {
typedef std::unordered_set<State*, StateHash, StateEqual> StateSet;

private:
// Make it easier to swap in a scalable reader-writer mutex.
using CacheMutex = Mutex;

enum {
// Indices into start_ for unanchored searches.
// Add kStartAnchored for anchored searches.
Expand Down Expand Up @@ -331,7 +334,7 @@ class DFA {
// while holding cache_mutex_ for writing, to avoid interrupting other
// readers. Any State* pointers are only valid while cache_mutex_
// is held.
Mutex cache_mutex_;
CacheMutex cache_mutex_;
int64_t mem_budget_; // Total memory budget for all States.
int64_t state_budget_; // Amount of memory remaining for new States.
StateSet state_cache_; // All States computed so far.
Expand Down Expand Up @@ -1106,7 +1109,7 @@ DFA::State* DFA::RunStateOnByte(State* state, int c) {

class DFA::RWLocker {
public:
explicit RWLocker(Mutex* mu);
explicit RWLocker(CacheMutex* mu);
~RWLocker();

// If the lock is only held for reading right now,
Expand All @@ -1116,14 +1119,14 @@ class DFA::RWLocker {
void LockForWriting();

private:
Mutex* mu_;
CacheMutex* mu_;
bool writing_;

RWLocker(const RWLocker&) = delete;
RWLocker& operator=(const RWLocker&) = delete;
};

DFA::RWLocker::RWLocker(Mutex* mu) : mu_(mu), writing_(false) {
DFA::RWLocker::RWLocker(CacheMutex* mu) : mu_(mu), writing_(false) {
mu_->ReaderLock();
}

Expand Down

0 comments on commit f8e389f

Please sign in to comment.