1 /*
2     Copyright (c) 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 #if _MSC_VER && !defined(__INTEL_COMPILER)
18     // unreachable code
19     #pragma warning( push )
20     #pragma warning( disable: 4702 )
21 #endif
22 
23 #if __INTEL_COMPILER && _MSC_VER
24 #pragma warning(disable : 2586) // decorated name length exceeded, name was truncated
25 #endif
26 
27 #include "common/test.h"
28 #include "oneapi/tbb/collaborative_call_once.h"
29 
30 #include "common/utils.h"
31 #include "common/utils_concurrency_limit.h"
32 #include "common/spin_barrier.h"
33 #include "oneapi/tbb/parallel_for.h"
34 #include "oneapi/tbb/task_group.h"
35 
36 #include <type_traits>
37 #include <exception>
38 
39 //! \file conformance_collaborative_call_once.cpp
40 //! \brief Test for [algorithms.collaborative_call_once] specification
41 
42 //! Test for collaborative_once_flag member functions to be matched with spec
43 //! \brief \ref interface \ref requirement
44 TEST_CASE("collaborative_once_flag member functions match") {
45     REQUIRE_MESSAGE(std::is_default_constructible<oneapi::tbb::collaborative_once_flag>::value == true,
46         "collaborative_once_flag must be default constructible");
47     REQUIRE_MESSAGE(std::is_copy_constructible<oneapi::tbb::collaborative_once_flag>::value == false,
48         "collaborative_once_flag must not be copy constructible");
49     REQUIRE_MESSAGE(std::is_copy_assignable<oneapi::tbb::collaborative_once_flag>::value == false,
50         "collaborative_once_flag must not be copy assignable");
51     REQUIRE_MESSAGE(std::is_move_constructible<oneapi::tbb::collaborative_once_flag>::value == false,
52         "collaborative_once_flag must not be move constructible");
53     REQUIRE_MESSAGE(std::is_move_assignable<oneapi::tbb::collaborative_once_flag>::value == false,
54         "collaborative_once_flag must not be move assignable");
55 }
56 
57 //! Test for collaborative_call_once to execute function exactly once
58 //! \brief \ref interface \ref requirement
59 TEST_CASE("collaborative_call_once executes function exactly once") {
60     oneapi::tbb::collaborative_once_flag once_flag;
61 
62     for (int iter = 0; iter < 100; ++iter) {
__anon8e61f74e0102(int number) 63         oneapi::tbb::collaborative_call_once(once_flag, [](int number) {
64             // Will be executed only on first iteration
65             REQUIRE(number == 0);
66         }, iter);
67     }
68 
69     // concurrent call
70     std::size_t num_threads = utils::get_platform_max_threads();
71     utils::SpinBarrier barrier{num_threads};
72 
73     int flag = 0;
__anon8e61f74e0202null74     auto func = [&flag] { flag++; };
75 
76     oneapi::tbb::collaborative_once_flag once_flag_concurrent;
__anon8e61f74e0302(std::size_t) 77     utils::NativeParallelFor(num_threads, [&](std::size_t) {
78         barrier.wait();
79         oneapi::tbb::collaborative_call_once(once_flag_concurrent, func);
80     });
81     REQUIRE(flag == 1);
82 }
83 
84 
85 #if TBB_USE_EXCEPTIONS
86 
87 //! Exception is received only by winner thread
88 //! \brief \ref error_guessing \ref requirement
89 TEST_CASE("Exception is received only by winner thread") {
90     int num_threads = static_cast<int>(utils::get_platform_max_threads());
91     utils::SpinBarrier barrier(num_threads);
92 
93     oneapi::tbb::task_group tg;
94     oneapi::tbb::collaborative_once_flag flag;
95 
96     for (int i = 0; i < num_threads-1; ++i) {
__anon8e61f74e0402null97         tg.run([&flag, &barrier] {
98             barrier.wait();
99             try {
100                 oneapi::tbb::collaborative_call_once(flag, [] { });
101             } catch(...) {
102                 REQUIRE_MESSAGE(false, "Unreachable code");
103             }
104         });
105     };
106 
107     bool exception_happened{false};
108     try {
__anon8e61f74e0602null109         oneapi::tbb::collaborative_call_once(flag, [&barrier] {
110             barrier.wait();
111             throw std::exception{};
112         });
113     } catch (std::exception&) {
114         exception_happened = true;
115     }
116 
117     REQUIRE_MESSAGE(exception_happened == true, "Exception hasn't been received from the winner thread");
118     tg.wait();
119 }
120 
121 #endif
122 
123 #if _MSC_VER && !defined(__INTEL_COMPILER)
124     #pragma warning( pop )
125 #endif
126