aboutsummaryrefslogtreecommitdiffhomepage
path: root/unsupported/test/cxx11_tensor_notification.cpp
diff options
context:
space:
mode:
authorGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2016-03-23 13:39:00 -0700
committerGravatar Benoit Steiner <benoit.steiner.goog@gmail.com>2016-03-23 13:39:00 -0700
commit2062ee2d269eac5ff78f70ac3133d0a47c22d9fa (patch)
tree81402dcde425a5f94c01c5463435ed52963e937b /unsupported/test/cxx11_tensor_notification.cpp
parentfc3660285fe326744eb67711126d2764a1f97100 (diff)
Added a test to verify that notifications are working properly
Diffstat (limited to 'unsupported/test/cxx11_tensor_notification.cpp')
-rw-r--r--unsupported/test/cxx11_tensor_notification.cpp72
1 files changed, 72 insertions, 0 deletions
diff --git a/unsupported/test/cxx11_tensor_notification.cpp b/unsupported/test/cxx11_tensor_notification.cpp
new file mode 100644
index 000000000..961d4edf6
--- /dev/null
+++ b/unsupported/test/cxx11_tensor_notification.cpp
@@ -0,0 +1,72 @@
+// This file is part of Eigen, a lightweight C++ template library
+// for linear algebra.
+//
+// Copyright (C) 2015 Vijay Vasudevan <vrv@google.com>
+//
+// This Source Code Form is subject to the terms of the Mozilla
+// Public License v. 2.0. If a copy of the MPL was not distributed
+// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#define EIGEN_USE_THREADS
+
+#include <unistd.h>
+#include <malloc.h>
+#include "main.h"
+#include <Eigen/CXX11/Tensor>
+
+namespace {
+
+void WaitAndAdd(Eigen::Notification* n, int* counter) {
+ n->Wait();
+ *counter = *counter + 1;
+}
+
+} // namespace
+
+static void test_notification_single()
+{
+ ThreadPool thread_pool(1);
+
+ int counter = 0;
+ Eigen::Notification n;
+ std::function<void()> func = std::bind(&WaitAndAdd, &n, &counter);
+ thread_pool.Schedule(func);
+ sleep(1);
+
+ // The thread should be waiting for the notification.
+ VERIFY_IS_EQUAL(counter, 0);
+
+ // Unblock the thread
+ n.Notify();
+
+ sleep(1);
+
+ // Verify the counter has been incremented
+ VERIFY_IS_EQUAL(counter, 1);
+}
+
+// Like test_notification_single() but enqueues multiple threads to
+// validate that all threads get notified by Notify().
+static void test_notification_multiple()
+{
+ ThreadPool thread_pool(1);
+
+ int counter = 0;
+ Eigen::Notification n;
+ std::function<void()> func = std::bind(&WaitAndAdd, &n, &counter);
+ thread_pool.Schedule(func);
+ thread_pool.Schedule(func);
+ thread_pool.Schedule(func);
+ thread_pool.Schedule(func);
+ sleep(1);
+ VERIFY_IS_EQUAL(counter, 0);
+ n.Notify();
+ sleep(1);
+ VERIFY_IS_EQUAL(counter, 4);
+}
+
+void test_cxx11_tensor_notification()
+{
+ CALL_SUBTEST(test_notification_single());
+ CALL_SUBTEST(test_notification_multiple());
+}