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 #define TBB_PREVIEW_MUTEXES 1
23 
24 #include "common/test.h"
25 #include "common/utils.h"
26 #define __TBB_TEST_DEFINE_PRIVATE_PUBLIC 1
27 #include "common/inject_scheduler.h"
28 #define private public
29 #define protected public
30 #include "tbb/concurrent_queue.h"
31 #include "../../src/tbb/concurrent_bounded_queue.cpp"
32 #include "../../src/tbb/misc.cpp"
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 
46     FloggerBody( Q& queue, std::size_t el_num )
47         : q(queue), elem_num(el_num) {}
48 
49     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             q.try_pop(elem);
55         }
56     }
57 
58 private:
59     Q& q;
60     std::size_t elem_num;
61 }; // class FloggerBody
62 
63 template <typename Q>
64 void test_flogger_help( Q& q, std::size_t items_per_page ) {
65     std::size_t nq = q.my_queue_representation->n_queue;
66     std::size_t reserved_elem_num = nq * items_per_page - 1;
67     std::size_t hack_val = std::numeric_limits<std::size_t>::max() & ~reserved_elem_num;
68 
69     q.my_queue_representation->head_counter = hack_val;
70     q.my_queue_representation->tail_counter = hack_val;
71 
72     std::size_t k = q.my_queue_representation->tail_counter & -(std::ptrdiff_t)nq;
73 
74     for (std::size_t i = 0; i < nq; ++i) {
75         q.my_queue_representation->array[i].head_counter = k;
76         q.my_queue_representation->array[i].tail_counter = k;
77     }
78 
79     // To induce the overflow occurrence
80     utils::NativeParallelFor(utils::MaxThread, FloggerBody<Q>(q, reserved_elem_num + 20));
81 
82     REQUIRE_MESSAGE(q.empty(), "Failed flogger/empty test");
83     REQUIRE_MESSAGE(q.my_queue_representation->head_counter < hack_val, "Failed wraparound test");
84 }
85 
86 template <typename T>
87 void test_flogger() {
88     {
89         tbb::concurrent_queue<T> q;
90         test_flogger_help(q, q.my_queue_representation->items_per_page);
91     }
92     {
93         tbb::concurrent_bounded_queue<T> q;
94         test_flogger_help(q, q.my_queue_representation->items_per_page);
95     }
96 }
97 
98 //! \brief \ref error_guessing
99 TEST_CASE("Test Wrapparound") {
100     test_flogger<int>();
101     // TODO: add test with unsigned char
102 }
103