summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonty Taylor <mordred@inaugust.com>2019-02-10 19:25:30 +0000
committerMonty Taylor <mordred@inaugust.com>2019-02-10 19:25:30 +0000
commit8dc7bfcf16407950cff46f727c77d00dcca3961c (patch)
treec197a419e54042a25741c399fb2e06f79ec684d8
parentc9245f61ec6b08c9f45ae62001aa5ada1eebf463 (diff)
Add mutex to gets and puts
gets and puts both mutate the underling structures. Put in a mutex and use a guard to make sure we're not stepping on ourselves. This almost certainly could be smarter.
-rw-r--r--zuul-preview/main.cc7
1 files changed, 6 insertions, 1 deletions
diff --git a/zuul-preview/main.cc b/zuul-preview/main.cc
index 18f4381..c96ed7a 100644
--- a/zuul-preview/main.cc
+++ b/zuul-preview/main.cc
@@ -47,15 +47,19 @@ class Cache {
47 // The maximum size of the cache. 47 // The maximum size of the cache.
48 const uint32_t size; 48 const uint32_t size;
49 49
50 // Mutex protecting the map and list.
51 mutex cache_mutex;
52
50public: 53public:
51 Cache(uint s) 54 Cache(uint s)
52 : queue {}, map {}, size{s} 55 : queue {}, map {}, size{s}, cache_mutex{}
53 { } 56 { }
54 57
55 // Lookup the hostname in the cache and return the URL if present. 58 // Lookup the hostname in the cache and return the URL if present.
56 // If the entry is present, it is moved to the head of the queue. 59 // If the entry is present, it is moved to the head of the queue.
57 optional<const string> get(const string &key) 60 optional<const string> get(const string &key)
58 { 61 {
62 lock_guard<mutex> guard{cache_mutex};
59 auto location = map.find(key); 63 auto location = map.find(key);
60 if (location == map.end()) 64 if (location == map.end())
61 return {}; 65 return {};
@@ -71,6 +75,7 @@ public:
71 // recently used entry. 75 // recently used entry.
72 void put(const string &key, const string &value) 76 void put(const string &key, const string &value)
73 { 77 {
78 lock_guard<mutex> guard{cache_mutex};
74 auto location = map.find(key); 79 auto location = map.find(key);
75 if (location != map.end()) 80 if (location != map.end())
76 return; 81 return;