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