1 | /* |
2 | Copyright (c) 2005-2019 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 | #define HARNESS_DEFINE_PRIVATE_PUBLIC 1 |
18 | #include "harness_inject_scheduler.h" |
19 | #define private public |
20 | #define protected public |
21 | #include "tbb/concurrent_queue.h" |
22 | #include "../tbb/concurrent_queue.cpp" |
23 | #undef protected |
24 | #undef private |
25 | #include "harness.h" |
26 | |
27 | #if _MSC_VER==1500 && !__INTEL_COMPILER |
28 | // VS2008/VC9 seems to have an issue; limits pull in math.h |
29 | #pragma warning( push ) |
30 | #pragma warning( disable: 4985 ) |
31 | #endif |
32 | #include <limits> |
33 | #if _MSC_VER==1500 && !__INTEL_COMPILER |
34 | #pragma warning( pop ) |
35 | #endif |
36 | |
37 | template <typename Q> |
38 | class FloggerBody : NoAssign { |
39 | Q& q; |
40 | size_t elem_num; |
41 | public: |
42 | FloggerBody(Q& q_, size_t elem_num_) : q(q_), elem_num(elem_num_) {} |
43 | void operator()(const int threadID) const { |
44 | typedef typename Q::value_type value_type; |
45 | value_type elem = value_type(threadID); |
46 | for (size_t i = 0; i < elem_num; ++i) { |
47 | q.push(elem); |
48 | (void) q.try_pop(elem); |
49 | } |
50 | } |
51 | }; |
52 | |
53 | template <typename Q> |
54 | void TestFloggerHelp(Q& q, size_t items_per_page) { |
55 | size_t nq = q.my_rep->n_queue; |
56 | size_t reserved_elem_num = nq * items_per_page - 1; |
57 | size_t hack_val = std::numeric_limits<std::size_t>::max() & ~reserved_elem_num; |
58 | q.my_rep->head_counter = hack_val; |
59 | q.my_rep->tail_counter = hack_val; |
60 | size_t k = q.my_rep->tail_counter & -(ptrdiff_t)nq; |
61 | |
62 | for (size_t i=0; i<nq; ++i) { |
63 | q.my_rep->array[i].head_counter = k; |
64 | q.my_rep->array[i].tail_counter = k; |
65 | } |
66 | NativeParallelFor(MaxThread, FloggerBody<Q>(q, reserved_elem_num + 20)); // to induce the overflow occurrence |
67 | ASSERT(q.empty(), "FAILED flogger/empty test." ); |
68 | ASSERT(q.my_rep->head_counter < hack_val, "FAILED wraparound test." ); |
69 | } |
70 | |
71 | template <typename T> |
72 | void TestFlogger() { |
73 | { |
74 | tbb::concurrent_queue<T> q; |
75 | REMARK("Wraparound on strict_ppl::concurrent_queue..." ); |
76 | TestFloggerHelp(q, q.my_rep->items_per_page); |
77 | REMARK(" works.\n" ); |
78 | } |
79 | { |
80 | tbb::concurrent_bounded_queue<T> q; |
81 | REMARK("Wraparound on tbb::concurrent_bounded_queue..." ); |
82 | TestFloggerHelp(q, q.items_per_page); |
83 | REMARK(" works.\n" ); |
84 | } |
85 | } |
86 | |
87 | void TestWraparound() { |
88 | REMARK("Testing Wraparound...\n" ); |
89 | TestFlogger<int>(); |
90 | TestFlogger<unsigned char>(); |
91 | REMARK("Done Testing Wraparound.\n" ); |
92 | } |
93 | |
94 | int TestMain () { |
95 | TestWraparound(); |
96 | return Harness::Done; |
97 | } |
98 | |