2016-03-24 04:39:00 +08:00
|
|
|
// 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
|
|
|
|
|
2019-11-09 09:44:50 +08:00
|
|
|
#include <atomic>
|
|
|
|
|
2016-03-24 07:30:06 +08:00
|
|
|
#include <stdlib.h>
|
2016-03-24 04:39:00 +08:00
|
|
|
#include "main.h"
|
|
|
|
#include <Eigen/CXX11/Tensor>
|
|
|
|
|
|
|
|
static void test_notification_single()
|
|
|
|
{
|
|
|
|
ThreadPool thread_pool(1);
|
|
|
|
|
2019-11-09 09:44:50 +08:00
|
|
|
std::atomic<int> counter(0);
|
2016-03-24 04:39:00 +08:00
|
|
|
Eigen::Notification n;
|
2019-11-09 09:44:50 +08:00
|
|
|
auto func = [&n, &counter](){ n.Wait(); ++counter;};
|
2016-03-24 04:39:00 +08:00
|
|
|
thread_pool.Schedule(func);
|
2020-12-03 03:04:04 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
2016-03-24 04:39:00 +08:00
|
|
|
|
|
|
|
// The thread should be waiting for the notification.
|
|
|
|
VERIFY_IS_EQUAL(counter, 0);
|
|
|
|
|
|
|
|
// Unblock the thread
|
|
|
|
n.Notify();
|
|
|
|
|
2020-12-03 03:04:04 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
2016-03-24 04:39:00 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2019-11-09 09:44:50 +08:00
|
|
|
std::atomic<int> counter(0);
|
2016-03-24 04:39:00 +08:00
|
|
|
Eigen::Notification n;
|
2019-11-09 09:44:50 +08:00
|
|
|
auto func = [&n, &counter](){ n.Wait(); ++counter;};
|
2016-03-24 04:39:00 +08:00
|
|
|
thread_pool.Schedule(func);
|
|
|
|
thread_pool.Schedule(func);
|
|
|
|
thread_pool.Schedule(func);
|
|
|
|
thread_pool.Schedule(func);
|
2020-12-03 03:04:04 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
2016-03-24 04:39:00 +08:00
|
|
|
VERIFY_IS_EQUAL(counter, 0);
|
|
|
|
n.Notify();
|
2020-12-03 03:04:04 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
2016-03-24 04:39:00 +08:00
|
|
|
VERIFY_IS_EQUAL(counter, 4);
|
|
|
|
}
|
|
|
|
|
2018-07-17 20:46:15 +08:00
|
|
|
EIGEN_DECLARE_TEST(cxx11_tensor_notification)
|
2016-03-24 04:39:00 +08:00
|
|
|
{
|
|
|
|
CALL_SUBTEST(test_notification_single());
|
|
|
|
CALL_SUBTEST(test_notification_multiple());
|
|
|
|
}
|