1 /*
2     Copyright (c) 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 #ifndef __TBB_market_concurrent_monitor_H
18 #define __TBB_market_concurrent_monitor_H
19 
20 #include "concurrent_monitor.h"
21 #include "scheduler_common.h"
22 
23 #include <atomic>
24 
25 namespace tbb {
26 namespace detail {
27 namespace r1 {
28 
29 struct market_context {
30     market_context() = default;
31 
market_contextmarket_context32     market_context(std::uintptr_t first_addr, arena* a) :
33         my_uniq_addr(first_addr), my_arena_addr(a)
34     {}
35 
36     std::uintptr_t my_uniq_addr{0};
37     arena* my_arena_addr{nullptr};
38 };
39 
40 #if __TBB_RESUMABLE_TASKS
41 class resume_node : public wait_node<market_context> {
42     using base_type = wait_node<market_context>;
43 public:
resume_node(market_context ctx,execution_data_ext & ed_ext,task_dispatcher & target)44     resume_node(market_context ctx, execution_data_ext& ed_ext, task_dispatcher& target)
45         : base_type(ctx), my_curr_dispatcher(ed_ext.task_disp), my_target_dispatcher(&target)
46         , my_suspend_point(my_curr_dispatcher->get_suspend_point())
47     {}
48 
~resume_node()49     virtual ~resume_node() {
50         if (this->my_skipped_wakeup) {
51             spin_wait_until_eq(this->my_notify_calls, 1);
52         }
53 
54         poison_pointer(my_curr_dispatcher);
55         poison_pointer(my_target_dispatcher);
56         poison_pointer(my_suspend_point);
57     }
58 
init()59     void init() override {
60         base_type::init();
61     }
62 
wait()63     void wait() override {
64         my_curr_dispatcher->resume(*my_target_dispatcher);
65         __TBB_ASSERT(!this->my_is_in_list.load(std::memory_order_relaxed), "Still in the queue?");
66     }
67 
reset()68     void reset() override {
69         base_type::reset();
70         spin_wait_until_eq(this->my_notify_calls, 1);
71         my_notify_calls.store(0, std::memory_order_relaxed);
72     }
73 
74     // notify is called (perhaps, concurrently) twice from:
75     //   - concurrent_monitor::notify
76     //   - post_resume_action::register_waiter
77     // The second notify is called after thread switches the stack
78     // (Because we can not call resume while the stack is occupied)
79     // We need calling resume only when both notifications are performed.
notify()80     void notify() override {
81         if (++my_notify_calls == 2) {
82             r1::resume(my_suspend_point);
83         }
84     }
85 
86 private:
87     friend class thread_data;
88     friend struct suspend_point_type::resume_task;
89     task_dispatcher* my_curr_dispatcher;
90     task_dispatcher* my_target_dispatcher;
91     suspend_point_type* my_suspend_point;
92     std::atomic<int> my_notify_calls{0};
93 };
94 #endif // __TBB_RESUMABLE_TASKS
95 
96 class market_concurrent_monitor : public concurrent_monitor_base<market_context> {
97     using base_type = concurrent_monitor_base<market_context>;
98 public:
99     using base_type::base_type;
100 
~market_concurrent_monitor()101     ~market_concurrent_monitor() {
102         destroy();
103     }
104 
105     /** per-thread descriptor for concurrent_monitor */
106     using thread_context = sleep_node<market_context>;
107 #if __TBB_RESUMABLE_TASKS
108     using resume_context = resume_node;
109 #endif
110 };
111 
112 } // namespace r1
113 } // namespace detail
114 } // namespace tbb
115 
116 #endif // __TBB_market_concurrent_monitor_H
117