1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #include "common/test.h"
18 #include "common/utils.h"
19 
20 #include "oneapi/tbb/task_arena.h"
21 #include "oneapi/tbb/task_scheduler_observer.h"
22 #include "oneapi/tbb/enumerable_thread_specific.h"
23 #include "oneapi/tbb/parallel_for.h"
24 
25 //! \file conformance_task_arena.cpp
26 //! \brief Test for [scheduler.task_arena scheduler.task_scheduler_observer] specification
27 
28 //! Test task arena interfaces
29 //! \brief \ref requirement \ref interface
30 TEST_CASE("Arena interfaces") {
31     //! Initialization interfaces
32     oneapi::tbb::task_arena a(1,1); a.initialize();
33     std::atomic<bool> done{ false };
34     //! Enqueue interface
35     a.enqueue([&done] {
36         CHECK(oneapi::tbb::this_task_arena::max_concurrency() == 2);
37         done = true;
38     });
39     //! Execute interface
40     a.execute([&] {
41         //! oneapi::tbb::this_task_arena interfaces
42         CHECK(oneapi::tbb::this_task_arena::current_thread_index() >= 0);
43         //! Attach interface
44         oneapi::tbb::task_arena attached_arena = oneapi::tbb::task_arena(oneapi::tbb::task_arena::attach());
45         CHECK(attached_arena.is_active());
46     });
47     while (!done) {
48         utils::yield();
49     }
50     //! Terminate interface
51     a.terminate();
52 }
53 
54 //! Test tasks isolation for inner oneapi::tbb::parallel_for loop
55 //! \brief \ref requirement \ref interface
56 TEST_CASE("Task isolation") {
57     const int N1 = 1000, N2 = 1000;
58     oneapi::tbb::enumerable_thread_specific<int> ets;
59     oneapi::tbb::parallel_for(0, N1, [&](int i) {
60         // Set a thread specific value
61         ets.local() = i;
62         // Run the second parallel loop in an isolated region to prevent the current thread
63         // from taking tasks related to the outer parallel loop.
64         oneapi::tbb::this_task_arena::isolate([&]{
65             oneapi::tbb::parallel_for(0, N2, utils::DummyBody(10));
66         });
67         REQUIRE(ets.local() == i);
68     });
69 }
70 
71 class conformance_observer: public oneapi::tbb::task_scheduler_observer {
72 public:
73     std::atomic<bool> is_entry_called{false};
74     std::atomic<bool> is_exit_called{false};
75 
76     conformance_observer( oneapi::tbb::task_arena &a ) : oneapi::tbb::task_scheduler_observer(a) {
77         observe(true); // activate the observer
78     }
79 
80     void on_scheduler_entry(bool) override {
81         is_entry_called.store(true, std::memory_order_relaxed);
82     }
83 
84     void on_scheduler_exit(bool) override {
85         is_exit_called.store(true, std::memory_order_relaxed);
86     }
87 
88     bool is_callbacks_called() {
89         return is_entry_called.load(std::memory_order_relaxed)
90             && is_exit_called.load(std::memory_order_relaxed);
91     }
92 };
93 
94 //! Test task arena observer interfaces
95 //! \brief \ref requirement \ref interface
96 TEST_CASE("Task arena observer") {
97     oneapi::tbb::task_arena a; a.initialize();
98     conformance_observer observer(a);
99     a.execute([&] {
100         oneapi::tbb::parallel_for(0, 100, utils::DummyBody(10), oneapi::tbb::simple_partitioner());
101     });
102     REQUIRE(observer.is_callbacks_called());
103 }
104 
105 //! Test task arena copy constructor
106 //! \brief \ref interface \ref requirement
107 TEST_CASE("Task arena copy constructor") {
108     oneapi::tbb::task_arena arena(1);
109     oneapi::tbb::task_arena copy = arena;
110 
111     REQUIRE(arena.max_concurrency() == copy.max_concurrency());
112     REQUIRE(arena.is_active() == copy.is_active());
113 }
114