Skip to content

Commit 6276766

Browse files
committed
minor changes
1 parent ca918ed commit 6276766

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

include/behaviortree_cpp/utils/timer_queue.h

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <atomic>
34
#include <mutex>
45
#include <condition_variable>
56
#include <thread>
@@ -22,32 +23,39 @@ class Semaphore
2223

2324
void notify()
2425
{
25-
std::unique_lock<std::mutex> lock(m_mtx);
26-
m_count++;
26+
{
27+
std::lock_guard<std::mutex> lock(m_mtx);
28+
m_count++;
29+
}
2730
m_cv.notify_one();
2831
}
2932

30-
void wait()
31-
{
32-
std::unique_lock<std::mutex> lock(m_mtx);
33-
m_cv.wait(lock, [this]() { return m_count > 0; });
34-
m_count--;
35-
}
36-
3733
template <class Clock, class Duration>
3834
bool waitUntil(const std::chrono::time_point<Clock, Duration>& point)
3935
{
4036
std::unique_lock<std::mutex> lock(m_mtx);
41-
if (!m_cv.wait_until(lock, point, [this]() { return m_count > 0; }))
37+
if (!m_cv.wait_until(lock, point, [this]() {
38+
return m_count > 0 || m_unlock;
39+
}))
40+
{
4241
return false;
42+
}
4343
m_count--;
44+
m_unlock = false;
4445
return true;
4546
}
4647

48+
void manualUnlock()
49+
{
50+
m_unlock = true;
51+
m_cv.notify_one();
52+
}
53+
4754
private:
4855
std::mutex m_mtx;
4956
std::condition_variable m_cv;
50-
unsigned int m_count;
57+
unsigned m_count = 0;
58+
std::atomic_bool m_unlock = false;
5159
};
5260
} // namespace details
5361

@@ -73,9 +81,9 @@ class TimerQueue
7381

7482
~TimerQueue()
7583
{
84+
m_finish = true;
7685
cancelAll();
77-
// Abusing the timer queue to trigger the shutdown.
78-
add(std::chrono::milliseconds(0), [this](bool) { m_finish = true; });
86+
m_checkWork.manualUnlock();
7987
m_th.join();
8088
}
8189

@@ -179,8 +187,8 @@ class TimerQueue
179187
}
180188
else
181189
{
182-
// No timers exist, so wait forever until something changes
183-
m_checkWork.wait();
190+
// No timers exist, so wait an arbitrary amount of time
191+
m_checkWork.waitUntil( _Clock::now() + std::chrono::milliseconds(10));
184192
}
185193

186194
// Check and execute as much work as possible, such as, all expired

src/xml_parsing.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void XMLParser::PImpl::loadDocImpl(tinyxml2::XMLDocument* doc, bool add_includes
224224

225225
XMLPrinter printer;
226226
doc->Print(&printer);
227-
auto xml_text = std::string(printer.CStr(), size_t(printer.CStrSize() - 1));
227+
auto xml_text = std::string(printer.CStr(), size_t(printer.CStrSize()));
228228

229229
// Verify the validity of the XML before adding any behavior trees to the parser's list of registered trees
230230
VerifyXML(xml_text, registered_nodes);
@@ -254,14 +254,14 @@ void VerifyXML(const std::string& xml_text,
254254
auto xml_error = doc.Parse(xml_text.c_str(), xml_text.size());
255255
if (xml_error)
256256
{
257-
char buffer[200];
257+
char buffer[512];
258258
sprintf(buffer, "Error parsing the XML: %s", doc.ErrorName());
259259
throw RuntimeError(buffer);
260260
}
261261

262262
//-------- Helper functions (lambdas) -----------------
263263
auto ThrowError = [&](int line_num, const std::string& text) {
264-
char buffer[256];
264+
char buffer[512];
265265
sprintf(buffer, "Error at line %d: -> %s", line_num, text.c_str());
266266
throw RuntimeError(buffer);
267267
};
@@ -300,10 +300,10 @@ void VerifyXML(const std::string& xml_text,
300300
for (auto node = xml_root->FirstChildElement(); node != nullptr;
301301
node = node->NextSiblingElement())
302302
{
303-
const char* name = node->Name();
304-
if (StrEqual(name, "Action") || StrEqual(name, "Decorator") ||
305-
StrEqual(name, "SubTree") || StrEqual(name, "Condition") ||
306-
StrEqual(name, "Control"))
303+
const std::string name = node->Name();
304+
if (name == "Action" || name == "Decorator" ||
305+
name == "SubTree" || name == "Condition" ||
306+
name == "Control")
307307
{
308308
const char* ID = node->Attribute("ID");
309309
if (!ID)
@@ -321,8 +321,8 @@ void VerifyXML(const std::string& xml_text,
321321

322322
recursiveStep = [&](const XMLElement* node) {
323323
const int children_count = ChildrenCount(node);
324-
const char* name = node->Name();
325-
if (StrEqual(name, "Decorator"))
324+
const std::string name = node->Name();
325+
if (name == "Decorator")
326326
{
327327
if (children_count != 1)
328328
{
@@ -335,7 +335,7 @@ void VerifyXML(const std::string& xml_text,
335335
"attribute [ID]");
336336
}
337337
}
338-
else if (StrEqual(name, "Action"))
338+
else if (name == "Action")
339339
{
340340
if (children_count != 0)
341341
{
@@ -348,7 +348,7 @@ void VerifyXML(const std::string& xml_text,
348348
"attribute [ID]");
349349
}
350350
}
351-
else if (StrEqual(name, "Condition"))
351+
else if (name == "Condition")
352352
{
353353
if (children_count != 0)
354354
{
@@ -361,7 +361,7 @@ void VerifyXML(const std::string& xml_text,
361361
"attribute [ID]");
362362
}
363363
}
364-
else if (StrEqual(name, "Control"))
364+
else if (name == "Control")
365365
{
366366
if (children_count == 0)
367367
{
@@ -374,16 +374,16 @@ void VerifyXML(const std::string& xml_text,
374374
"attribute [ID]");
375375
}
376376
}
377-
else if (StrEqual(name, "Sequence") || StrEqual(name, "SequenceStar") ||
378-
StrEqual(name, "Fallback"))
377+
else if (name == "Sequence" || name == "SequenceStar" ||
378+
name == "Fallback")
379379
{
380380
if (children_count == 0)
381381
{
382382
ThrowError(node->GetLineNum(), "A Control node must have at least 1 "
383383
"child");
384384
}
385385
}
386-
else if (StrEqual(name, "SubTree"))
386+
else if (name == "SubTree")
387387
{
388388
auto child = node->FirstChildElement();
389389

@@ -405,7 +405,7 @@ void VerifyXML(const std::string& xml_text,
405405
"attribute [ID]");
406406
}
407407
}
408-
else if (StrEqual(name, "BehaviorTree"))
408+
else if (name == "BehaviorTree")
409409
{
410410
if (children_count != 1)
411411
{
@@ -433,7 +433,7 @@ void VerifyXML(const std::string& xml_text,
433433
}
434434
}
435435
//recursion
436-
if (StrEqual(name, "SubTree") == false)
436+
if (name != "SubTree" )
437437
{
438438
for (auto child = node->FirstChildElement(); child != nullptr;
439439
child = child->NextSiblingElement())
@@ -730,7 +730,7 @@ void BT::XMLParser::PImpl::recursivelyCreateSubtree(
730730
recursiveStep = [&](TreeNode::Ptr parent_node,
731731
Tree::Subtree::Ptr subtree,
732732
std::string prefix,
733-
const XMLElement* element)
733+
const XMLElement* element)
734734
{
735735
// create the node
736736
auto node = createNodeFromXML(element, blackboard, parent_node, prefix, output_tree);

0 commit comments

Comments
 (0)