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