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