Skip to content

Commit

Permalink
make a KV Store thread-safe, makes access to app profile thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
AO-StreetArt committed Jun 24, 2018
1 parent 4309eb2 commit 1a4a122
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions aossl/core/include/kv_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ THE SOFTWARE.
#include <string>
#include <unordered_map>
#include <iostream>
#include "Poco/RWLock.h"

#include "buffers.h"
#include "kv_store_interface.h"
Expand All @@ -40,12 +41,13 @@ namespace AOSSL {
//! Interface which requires implementation
class KeyValueStore: public KeyValueStoreInterface {
std::unordered_map<std::string, std::string> opts;

Poco::RWLock rw_mutex;
public:
KeyValueStore() {}
virtual ~KeyValueStore() {}
//! Does a key exist?
inline bool opt_exist(std::string key) {
Poco::ScopedReadRWLock lock(rw_mutex);
auto search = opts.find(key);
if (search != opts.end()) {
return true;
Expand All @@ -63,18 +65,25 @@ class KeyValueStore: public KeyValueStoreInterface {

//! Get an option by key
inline void get_opt(std::string key, StringBuffer& val) {
Poco::ScopedReadRWLock lock(rw_mutex);
val.val.assign(opts[key]);
val.success = true;
}

std::unordered_map<std::string, std::string> get_opts() {return opts;}
inline std::unordered_map<std::string, std::string> get_opts() {
Poco::ScopedReadRWLock lock(rw_mutex);
return opts;
}

//! Add an option
void add_opt(const std::string& key, const std::string& value) \
{opts.emplace(key, value);}
{opts.emplace(key, value);}

//! Set an option
void set_opt(std::string& key, std::string& value) {opts[key] = value;}
inline void set_opt(std::string& key, std::string& value) {
Poco::ScopedWriteRWLock lock(rw_mutex);
opts[key] = value;
}
};

} // namespace AOSSL
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export SLC = ar rcs
export CFLAGS = -g -Wall
export STD = -std=c++11
export POCO_LIBS = -lPocoNetSSL -lPocoCrypto -lPocoNet -lPocoUtil -lPocoFoundation
export LIBS = -lssl -lcrypto -luuid $(POCO_LIBS)
export LIBS = -lssl -lcrypto -luuid -lpthread $(POCO_LIBS)
export RM = rm -f

#Install Variables
Expand Down

0 comments on commit 1a4a122

Please sign in to comment.