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