Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/ActiveObject.cpp

Issue 29706560: Issue 5179 - Implement asynchronous executor with a controllable lifetime (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: address comments Created March 1, 2018, 11:14 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « libadblockplus.gyp ('k') | src/AsyncExecutor.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/ActiveObject.cpp
diff --git a/src/ActiveObject.cpp b/src/ActiveObject.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..af72a43d740c4056ab20aee922444dc4efdc30fe
--- /dev/null
+++ b/src/ActiveObject.cpp
@@ -0,0 +1,67 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-present eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <AdblockPlus/ActiveObject.h>
+
+using namespace AdblockPlus;
+
+ActiveObject::ActiveObject()
+ : isRunning(true)
+{
+ thread = std::thread([this]
+ {
+ ThreadFunc();
+ });
+}
+
+ActiveObject::~ActiveObject()
+{
+ Post([this]
+ {
+ isRunning = false;
+ });
+ thread.join();
+}
+
+void ActiveObject::Post(const Call& call)
+{
+ if (!call)
+ return;
+ calls.push_back(call);
+}
+
+void ActiveObject::Post(Call&& call)
+{
+ if (!call)
+ return;
+ calls.push_back(std::move(call));
+}
+
+void ActiveObject::ThreadFunc()
+{
+ while (isRunning)
+ {
+ Call call = calls.pop_front();
+ try
+ {
+ call();
+ }
+ catch (...)
+ {
+ // do nothing, but the thread will be alive.
+ }
+ }
+}
« no previous file with comments | « libadblockplus.gyp ('k') | src/AsyncExecutor.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld