Skip to content

Commit c0558d4

Browse files
committed
blackboard: update getKeys and add mutex to scripting
1 parent 1769591 commit c0558d4

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

include/behaviortree_cpp/blackboard.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#ifndef BLACKBOARD_H
2-
#define BLACKBOARD_H
1+
#pragma once
32

43
#include <iostream>
54
#include <string>
@@ -113,8 +112,8 @@ class Blackboard
113112
template <typename T>
114113
void set(const std::string& key, const T& value)
115114
{
116-
std::unique_lock<std::mutex> lock_entry(entry_mutex_);
117-
std::unique_lock<std::mutex> lock(mutex_);
115+
std::unique_lock lock_entry(entry_mutex_);
116+
std::unique_lock lock(mutex_);
118117

119118
// search first if this port was remapped.
120119
// Change the parent_bb_ in that case
@@ -205,7 +204,7 @@ class Blackboard
205204

206205
void debugMessage() const;
207206

208-
std::vector<StringView> getKeys() const;
207+
std::vector<StringView> getKeys(bool include_remapped = true) const;
209208

210209
void clear()
211210
{
@@ -216,7 +215,7 @@ class Blackboard
216215

217216
// Lock this mutex before using get() and getAny() and unlock it while you have
218217
// done using the value.
219-
std::mutex& entryMutex()
218+
std::recursive_mutex& entryMutex() const
220219
{
221220
return entry_mutex_;
222221
}
@@ -236,12 +235,11 @@ class Blackboard
236235
};
237236

238237
mutable std::mutex mutex_;
239-
mutable std::mutex entry_mutex_;
238+
mutable std::recursive_mutex entry_mutex_;
240239
std::unordered_map<std::string, Entry> storage_;
241240
std::weak_ptr<Blackboard> parent_bb_;
242241
std::unordered_map<std::string, std::string> internal_to_external_;
243242
};
244243

245244
} // namespace BT
246245

247-
#endif // BLACKBOARD_H

include/behaviortree_cpp/scripting/operators.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct ExprName : ExprBase
6868
}
6969
}
7070
// search now in the variables table
71+
std::unique_lock entry_lock(env.vars->entryMutex());
7172
auto any_ptr = env.vars->getAny(name);
7273
if( !any_ptr )
7374
{
@@ -358,6 +359,8 @@ struct ExprAssignment : ExprBase
358359
throw std::runtime_error("Assignment left operand not an lvalue");
359360
}
360361
const auto& key = varname->name;
362+
363+
std::unique_lock entry_lock(env.vars->entryMutex());
361364
auto any_ptr = env.vars->getAny(key);
362365
if( !any_ptr )
363366
{

src/blackboard.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ void Blackboard::addSubtreeRemapping(StringView internal, StringView external)
6464

6565
void Blackboard::debugMessage() const
6666
{
67-
for (const auto& entry_it : storage_)
67+
for (const auto& [key, entry] : storage_)
6868
{
69-
auto port_type = entry_it.second.port_info.type();
69+
auto port_type = entry.port_info.type();
7070
if (port_type == typeid(void))
7171
{
72-
port_type = entry_it.second.value.type();
72+
port_type = entry.value.type();
7373
}
7474

75-
std::cout << entry_it.first << " (" << BT::demangle(port_type) << ")" << std::endl;
75+
std::cout << key << " (" << BT::demangle(port_type) << ")" << std::endl;
7676
}
7777

7878
for (const auto& [from, to] : internal_to_external_)
@@ -83,18 +83,26 @@ void Blackboard::debugMessage() const
8383
}
8484
}
8585

86-
std::vector<StringView> Blackboard::getKeys() const
86+
std::vector<StringView> Blackboard::getKeys(bool include_remapped) const
8787
{
88-
if (storage_.empty())
88+
const size_t N = storage_.size() + (include_remapped ? internal_to_external_.size() : 0 );
89+
if (N == 0)
8990
{
9091
return {};
9192
}
9293
std::vector<StringView> out;
93-
out.reserve(storage_.size());
94+
out.reserve(N);
9495
for (const auto& entry_it : storage_)
9596
{
9697
out.push_back(entry_it.first);
9798
}
99+
if(include_remapped)
100+
{
101+
for (const auto& [key, remapped] : internal_to_external_)
102+
{
103+
out.push_back(key);
104+
}
105+
}
98106
return out;
99107
}
100108

0 commit comments

Comments
 (0)