xref: /oneTBB/src/tbb/market.h (revision 71e1bb8e)
1 /*
2     Copyright (c) 2005-2023 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_H
18 #define _TBB_market_H
19 
20 #include "oneapi/tbb/rw_mutex.h"
21 #include "oneapi/tbb/tbb_allocator.h"
22 #include "oneapi/tbb/task_arena.h"
23 
24 #include "permit_manager.h"
25 #include "pm_client.h"
26 
27 #include <atomic>
28 #include <vector>
29 
30 namespace tbb {
31 namespace detail {
32 namespace r1 {
33 
34 class market : public permit_manager {
35 public:
36     market(unsigned soft_limit);
37 
38     pm_client* create_client(arena& a) override;
39     void register_client(pm_client* client, d1::constraints&) override;
40     void unregister_and_destroy_client(pm_client& c) override;
41 
42     //! Request that arena's need in workers should be adjusted.
43     void adjust_demand(pm_client&, int mandatory_delta, int workers_delta) override;
44 
45     //! Set number of active workers
46     void set_active_num_workers(int soft_limit) override;
47 private:
48     //! Recalculates the number of workers assigned to each arena in the list.
49     void update_allotment();
50 
51     //! Keys for the arena map array. The lower the value the higher priority of the arena list.
52     static constexpr unsigned num_priority_levels = d1::num_priority_levels;
53 
54     using mutex_type = d1::rw_mutex;
55     mutex_type my_mutex;
56 
57     //! Current application-imposed limit on the number of workers
58     int my_num_workers_soft_limit;
59 
60     //! Number of workers that were requested by all arenas on all priority levels
61     int my_total_demand{0};
62 
63     //! Number of workers that were requested by arenas per single priority list item
64     int my_priority_level_demand[num_priority_levels] = {0};
65 
66     //! How many times mandatory concurrency was requested from the market
67     int my_mandatory_num_requested{0};
68 
69     //! Per priority list of registered arenas
70     using clients_container_type = std::vector<pm_client*, tbb::tbb_allocator<pm_client*>>;
71     clients_container_type my_clients[num_priority_levels];
72 }; // class market
73 
74 } // namespace r1
75 } // namespace detail
76 } // namespace tbb
77 
78 #endif /* _TBB_market_H */
79