Skip to content

Commit

Permalink
lock/mutex added to blackboard
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzs13 committed Jun 18, 2024
1 parent ade2a3f commit 15ac294
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 31 deletions.
29 changes: 19 additions & 10 deletions yasmin/include/yasmin/blackboard/blackboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define YASMIN_BLACKBOARD_HPP

#include <map>
#include <mutex>
#include <string>

#include "yasmin/blackboard/blackboard_value.hpp"
Expand All @@ -28,24 +29,18 @@ namespace blackboard {
class Blackboard {

private:
std::recursive_mutex mutex;
std::map<std::string, BlackboardValueInterface *> values;

public:
Blackboard();
Blackboard(const Blackboard &other);
~Blackboard();

template <class T> void set(std::string name, T value) {
if (!this->contains(name)) {
BlackboardValue<T> *b_value = new BlackboardValue<T>(value);
this->values.insert({name, b_value});
template <class T> T get(std::string name) {

} else {
((BlackboardValue<T> *)this->values.at(name))->set(value);
}
}
std::lock_guard<std::recursive_mutex> lk(this->mutex);

template <class T> T get(std::string name) {
if (!this->contains(name)) {
throw "Element " + name + " does not exist in the blackboard";
}
Expand All @@ -54,8 +49,22 @@ class Blackboard {
return b_value->get();
}

bool contains(std::string name);
template <class T> void set(std::string name, T value) {

std::lock_guard<std::recursive_mutex> lk(this->mutex);

if (!this->contains(name)) {
BlackboardValue<T> *b_value = new BlackboardValue<T>(value);
this->values.insert({name, b_value});

} else {
((BlackboardValue<T> *)this->values.at(name))->set(value);
}
}

void remove(std::string name);
bool contains(std::string name);
int size();
std::string to_string();
};

Expand Down
14 changes: 14 additions & 0 deletions yasmin/src/yasmin/blackboard/blackboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,26 @@ Blackboard::~Blackboard() {
}
}

void Blackboard::remove(std::string name) {
std::lock_guard<std::recursive_mutex> lk(this->mutex);
delete this->values.at(name);
this->values.erase(name);
}

bool Blackboard::contains(std::string name) {
std::lock_guard<std::recursive_mutex> lk(this->mutex);
return (this->values.find(name) != this->values.end());
}

int Blackboard::size() {
std::lock_guard<std::recursive_mutex> lk(this->mutex);
return this->values.size();
}

std::string Blackboard::to_string() {

std::lock_guard<std::recursive_mutex> lk(this->mutex);

std::string result = "Blackboard\n";

for (const auto &ele : this->values) {
Expand Down
19 changes: 9 additions & 10 deletions yasmin/tests/python/test_blackboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@
class TestBlackboard(unittest.TestCase):

def setUp(self):

self.blackboard = Blackboard()

def test_blackboard_get(self):
self.blackboard.__setitem__("foo", "foo")
self.assertEqual("foo", self.blackboard.__getitem__("foo"))
self.blackboard["foo"] = "foo"
self.assertEqual("foo", self.blackboard["foo"])

def test_blackboard_delete(self):
self.blackboard.__setitem__("foo", "foo")
self.blackboard.__delitem__("foo")
self.assertFalse(self.blackboard.__contains__("foo"))
self.blackboard["foo"] = "foo"
del self.blackboard["foo"]
self.assertFalse("foo" in self.blackboard)

def test_blackboard_contains(self):
self.blackboard.__setitem__("foo", "foo")
self.assertTrue(self.blackboard.__contains__("foo"))
self.blackboard["foo"] = "foo"
self.assertTrue("foo" in self.blackboard)

def test_blackboard_len(self):
self.blackboard.__setitem__("foo", "foo")
self.assertEqual(1, self.blackboard.__len__())
self.blackboard["foo"] = "foo"
self.assertEqual(1, len(self.blackboard))
28 changes: 19 additions & 9 deletions yasmin/yasmin/blackboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,38 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.


from typing import Any
from typing import Any, Dict
from threading import Lock


class Blackboard(object):
def __init__(self, init=None) -> None:

def __init__(self, init: Dict[str, Any] = None) -> None:
self.__lock = Lock()
self._data = {}
if init is not None:
self.__dict__.update(init)
self._data.update(init)

def __getitem__(self, key) -> Any:
return self.__dict__[key]
with self.__lock:
return self._data[key]

def __setitem__(self, key, value) -> None:
self.__dict__[key] = value
with self.__lock:
self._data[key] = value

def __delitem__(self, key) -> None:
del self.__dict__[key]
with self.__lock:
del self._data[key]

def __contains__(self, key) -> bool:
return key in self.__dict__
with self.__lock:
return key in self._data

def __len__(self) -> int:
return len(self.__dict__)
with self.__lock:
return len(self._data)

def __repr__(self) -> str:
return repr(self.__dict__)
with self.__lock:
return repr(self._data)
4 changes: 2 additions & 2 deletions yasmin_ros/tests/python/test_yasmin_ros.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from threading import Thread

from yasmin_ros import ActionState, ServiceState, MonitorState
from yasmin_ros.basic_outcomes import SUCCEED, CANCEL, ABORT
from yasmin_ros.basic_outcomes import SUCCEED, CANCEL, ABORT, TIMEOUT

from example_interfaces.action import Fibonacci
from example_interfaces.srv import AddTwoInts
Expand Down Expand Up @@ -195,4 +195,4 @@ def monitor_handler(blackboard, msg):
state = MonitorState(String, "test1", [SUCCEED],
monitor_handler=monitor_handler,
timeout=2)
self.assertEqual(CANCEL, state())
self.assertEqual(TIMEOUT, state())

0 comments on commit 15ac294

Please sign in to comment.