1 /*
2     Copyright (c) 2005-2022 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 _WIN32 || _WIN64
18 #define _CRT_SECURE_NO_WARNINGS
19 #endif
20 
21 #if _MSC_VER && !defined(__INTEL_COMPILER)
22 // structure was padded due to alignment specifier
23 #pragma warning( disable: 4324 )
24 #endif
25 
26 #include "common/test.h"
27 #include "common/utils.h"
28 #define __TBB_TEST_DEFINE_PRIVATE_PUBLIC 1
29 #include "common/inject_scheduler.h"
30 #define private public
31 #define protected public
32 #include "tbb/concurrent_queue.h"
33 #undef protected
34 #undef private
35 
36 #include <limits>
37 
38 //! \file test_concurrent_queue_whitebox.cpp
39 //! \brief Test for [internal] functionality
40 
41 template <typename Q>
42 class FloggerBody {
43 public:
44     FloggerBody& operator=( const FloggerBody& ) = delete;
45 
FloggerBody(Q & queue,std::size_t el_num)46     FloggerBody( Q& queue, std::size_t el_num )
47         : q(queue), elem_num(el_num) {}
48 
operator ()(const int thread_id) const49     void operator()( const int thread_id ) const {
50         using value_type = typename Q::value_type;
51         value_type elem = value_type(thread_id);
52         for (std::size_t i = 0; i < elem_num; ++i) {
53             q.push(elem);
54             bool res = q.try_pop(elem);
55             CHECK_FAST(res);
56         }
57     }
58 
59 private:
60     Q& q;
61     std::size_t elem_num;
62 }; // class FloggerBody
63 
64 template <typename Q>
test_flogger_help(Q & q,std::size_t items_per_page)65 void test_flogger_help( Q& q, std::size_t items_per_page ) {
66     std::size_t nq = q.my_queue_representation->n_queue;
67     std::size_t reserved_elem_num = nq * items_per_page - 1;
68     std::size_t hack_val = std::numeric_limits<std::size_t>::max() & ~reserved_elem_num;
69 
70     q.my_queue_representation->head_counter = hack_val;
71     q.my_queue_representation->tail_counter = hack_val;
72 
73     std::size_t k = q.my_queue_representation->tail_counter & -(std::ptrdiff_t)nq;
74 
75     for (std::size_t i = 0; i < nq; ++i) {
76         q.my_queue_representation->array[i].head_counter = k;
77         q.my_queue_representation->array[i].tail_counter = k;
78     }
79 
80     // To induce the overflow occurrence
81     utils::NativeParallelFor(static_cast<typename Q::value_type>(utils::MaxThread), FloggerBody<Q>(q, reserved_elem_num + 20));
82 
83     REQUIRE_MESSAGE(q.empty(), "Failed flogger/empty test");
84     REQUIRE_MESSAGE(q.my_queue_representation->head_counter < hack_val, "Failed wraparound test");
85 }
86 
87 //! \brief \ref error_guessing
88 TEST_CASE("Test CQ Wrapparound") {
89     for (int i = 0; i < 1000; ++i) {
90         tbb::concurrent_queue<int> q;
91         test_flogger_help(q, q.my_queue_representation->items_per_page);
92     }
93 }
94 
95 //! \brief \ref error_guessing
96 TEST_CASE("Test CBQ Wrapparound") {
97     for (int i = 0; i < 1000; ++i) {
98         tbb::concurrent_bounded_queue<int> q;
99         test_flogger_help(q, q.my_queue_representation->items_per_page);
100     }
101 }
102