aboutsummaryrefslogtreecommitdiffstats
path: root/yage/util
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-11-16 16:27:47 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-11-16 16:27:47 +0000
commit82a3db85138c91df397fd820a3b5d1a0b5c21ef9 (patch)
tree6c6435962594df18eb2b6ed1d07740aecd0778ff /yage/util
parent443ae47fc210bcfe10f6f6c5ac8aa3453e1d29d2 (diff)
downloadYAGE-82a3db85138c91df397fd820a3b5d1a0b5c21ef9.tar.gz
YAGE-82a3db85138c91df397fd820a3b5d1a0b5c21ef9.zip
Asynchronous logging added
Diffstat (limited to 'yage/util')
-rw-r--r--yage/util/active.cpp37
-rw-r--r--yage/util/active.h29
2 files changed, 65 insertions, 1 deletions
diff --git a/yage/util/active.cpp b/yage/util/active.cpp
new file mode 100644
index 00000000..13e7fc38
--- /dev/null
+++ b/yage/util/active.cpp
@@ -0,0 +1,37 @@
+#include "active.h"
+
+namespace yage
+{
+
+Active::Active() : running_(true) {}
+
+Active::~Active()
+{
+ send([this] { running_ = false; });
+ thread_.join();
+}
+
+std::unique_ptr<Active> Active::create()
+{
+ std::unique_ptr<Active> result(new Active);
+
+ result->thread_ = std::thread(&Active::run, result.get());
+
+ return result;
+}
+
+void Active::send(Callback message)
+{
+ queue_.push(message);
+}
+
+void Active::run()
+{
+ Callback fn;
+ while (running_) {
+ queue_.pop(fn);
+ fn();
+ }
+}
+
+} // namespace yage
diff --git a/yage/util/active.h b/yage/util/active.h
index 877ab75e..ca8d30ad 100644
--- a/yage/util/active.h
+++ b/yage/util/active.h
@@ -1,11 +1,38 @@
#ifndef YAGE_UTIL_ACTIVE_H
#define YAGE_UTIL_ACTIVE_H
+#include "syncqueue.h"
+
+#include <functional>
+#include <memory>
+#include <thread>
+
+namespace yage
+{
+
class Active
{
public:
+ typedef std::function<void()> Callback;
+
+ Active(const Active &) = delete;
+ Active &operator=(const Active &) = delete;
+
+ ~Active();
+
+ static std::unique_ptr<Active> create();
+
+ void send(Callback message);
+
+private:
Active();
- virtual ~Active();
+ void run();
+
+ bool running_;
+ SyncQueue<Callback> queue_;
+ std::thread thread_;
};
+} // namespace yage
+
#endif