-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent setDatabase starvation which leads to no updates of curr…
…ently loaded database
- Loading branch information
Showing
6 changed files
with
29 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,30 @@ | ||
#pragma once | ||
|
||
#include <shared_mutex> | ||
#include <memory> | ||
|
||
#include "silo/database.h" | ||
|
||
namespace silo_api { | ||
|
||
class FixedDatabase { | ||
std::shared_lock<std::shared_mutex> lock; | ||
|
||
public: | ||
FixedDatabase(const silo::Database& database, std::shared_lock<std::shared_mutex>&& mutex); | ||
|
||
const silo::Database& database; | ||
}; | ||
|
||
class UninitializedDatabaseException : public std::runtime_error { | ||
public: | ||
UninitializedDatabaseException() | ||
: std::runtime_error("Database not initialized yet") {} | ||
}; | ||
|
||
class DatabaseMutex { | ||
std::shared_mutex mutex; | ||
silo::Database database; | ||
bool is_initialized = false; | ||
std::shared_ptr<silo::Database> database; | ||
|
||
public: | ||
DatabaseMutex() = default; | ||
DatabaseMutex(const DatabaseMutex& other) = delete; | ||
DatabaseMutex(DatabaseMutex&& other) = delete; | ||
DatabaseMutex& operator=(const DatabaseMutex& other) = delete; | ||
DatabaseMutex& operator=(DatabaseMutex&& other) = delete; | ||
|
||
void setDatabase(silo::Database&& new_database); | ||
|
||
virtual FixedDatabase getDatabase(); | ||
virtual std::shared_ptr<silo::Database> getDatabase(); | ||
}; | ||
} // namespace silo_api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,24 @@ | ||
#include "silo_api/database_mutex.h" | ||
|
||
#include <mutex> | ||
#include <atomic> | ||
#include <utility> | ||
|
||
#include "silo/database.h" | ||
|
||
namespace silo_api { | ||
|
||
silo_api::FixedDatabase::FixedDatabase( | ||
const silo::Database& database, | ||
std::shared_lock<std::shared_mutex>&& mutex | ||
) | ||
: lock(std::move(mutex)), | ||
database(database) {} | ||
|
||
void silo_api::DatabaseMutex::setDatabase(silo::Database&& new_database) { | ||
const std::unique_lock lock(mutex); | ||
database = std::move(new_database); | ||
auto new_database_pointer = std::make_shared<silo::Database>(std::move(new_database)); | ||
|
||
std::atomic_store(&database, new_database_pointer); | ||
is_initialized = true; | ||
} | ||
|
||
silo_api::FixedDatabase silo_api::DatabaseMutex::getDatabase() { | ||
std::shared_ptr<silo::Database> silo_api::DatabaseMutex::getDatabase() { | ||
if (!is_initialized) { | ||
throw silo_api::UninitializedDatabaseException(); | ||
} | ||
std::shared_lock<std::shared_mutex> lock(mutex); | ||
return {database, std::move(lock)}; | ||
return std::atomic_load(&database); | ||
} | ||
|
||
} // namespace silo_api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters