Replaced the mutex in FileCache with an RWLock.

This commit is contained in:
Relintai 2022-02-04 22:12:08 +01:00
parent 0514505cef
commit ae81caf9a1
2 changed files with 12 additions and 8 deletions

View File

@ -17,9 +17,13 @@ bool FileCache::wwwroot_has_file(const String &file_path) {
} }
void FileCache::wwwroot_refresh_cache() { void FileCache::wwwroot_refresh_cache() {
_lock.write_lock();
registered_files.clear(); registered_files.clear();
wwwroot_evaluate_dir(wwwroot.c_str()); wwwroot_evaluate_dir(wwwroot.c_str());
_lock.write_unlock();
} }
void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist) { void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist) {
@ -62,9 +66,9 @@ void FileCache::wwwroot_evaluate_dir(const char *path, const bool should_exist)
bool FileCache::get_cached_body(const String &path, String *body) { bool FileCache::get_cached_body(const String &path, String *body) {
//TODO ERROR MACRO body == null //TODO ERROR MACRO body == null
//this shouldn't need mutexes _lock.read_lock();
CacheEntry *e = cache_map[path]; CacheEntry *e = cache_map[path];
_lock.read_unlock();
if (!e) { if (!e) {
return false; return false;
@ -84,7 +88,7 @@ bool FileCache::get_cached_body(const String &path, String *body) {
} }
void FileCache::set_cached_body(const String &path, const String &body) { void FileCache::set_cached_body(const String &path, const String &body) {
cache_mutex.lock(); _lock.write_lock();
CacheEntry *e = cache_map[path]; CacheEntry *e = cache_map[path];
@ -98,11 +102,11 @@ void FileCache::set_cached_body(const String &path, const String &body) {
e->timestamp = current_timestamp; e->timestamp = current_timestamp;
e->body = body; e->body = body;
cache_mutex.unlock(); _lock.write_unlock();
} }
void FileCache::clear() { void FileCache::clear() {
cache_mutex.lock(); _lock.write_lock();
registered_files.clear(); registered_files.clear();
@ -116,7 +120,7 @@ void FileCache::clear() {
cache_map.clear(); cache_map.clear();
cache_mutex.unlock(); _lock.write_unlock();
} }
FileCache::FileCache(bool singleton) { FileCache::FileCache(bool singleton) {

View File

@ -6,7 +6,7 @@
#include <set> #include <set>
#include <map> #include <map>
#include <chrono> #include <chrono>
#include <mutex> #include "core/threading/rw_lock.h"
class FileCache { class FileCache {
public: public:
@ -44,7 +44,7 @@ protected:
} }
}; };
std::mutex cache_mutex; RWLock _lock;
std::map<String, CacheEntry *> cache_map; std::map<String, CacheEntry *> cache_map;
private: private: