aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/tbb/src/tbb/global_control.cpp
blob: a9eac2cbc34ca984bb4ca106eb78a14a8e4ea32d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
    Copyright (c) 2005-2021 Intel Corporation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "oneapi/tbb/detail/_config.h"
#include "oneapi/tbb/detail/_template_helpers.h"

#include "oneapi/tbb/global_control.h"
#include "oneapi/tbb/tbb_allocator.h"
#include "oneapi/tbb/spin_mutex.h"

#include "governor.h"
#include "market.h"
#include "misc.h"

#include <atomic>
#include <set>

namespace tbb {
namespace detail {
namespace r1 {

//! Comparator for a set of global_control objects
struct control_storage_comparator {
    bool operator()(const global_control* lhs, const global_control* rhs) const;
};

class control_storage {
    friend struct global_control_impl;
    friend std::size_t global_control_active_value(int);
protected:
    std::size_t my_active_value{0};
    std::set<global_control*, control_storage_comparator, tbb_allocator<global_control*>> my_list{};
    spin_mutex my_list_mutex{};
public:
    virtual std::size_t default_value() const = 0;
    virtual void apply_active(std::size_t new_active) {
        my_active_value = new_active;
    }
    virtual bool is_first_arg_preferred(std::size_t a, std::size_t b) const {
        return a>b; // prefer max by default
    }
    virtual std::size_t active_value() {
        spin_mutex::scoped_lock lock(my_list_mutex); // protect my_list.empty() call
        return !my_list.empty() ? my_active_value : default_value();
    }
};

class alignas(max_nfs_size) allowed_parallelism_control : public control_storage {
    virtual std::size_t default_value() const override {
        return max(1U, governor::default_num_threads());
    }
    virtual bool is_first_arg_preferred(std::size_t a, std::size_t b) const override {
        return a<b; // prefer min allowed parallelism
    }
    virtual void apply_active(std::size_t new_active) override {
        control_storage::apply_active(new_active);
        __TBB_ASSERT( my_active_value>=1, NULL );
        // -1 to take external thread into account
        market::set_active_num_workers( my_active_value-1 );
    }
    virtual std::size_t active_value() override {
        spin_mutex::scoped_lock lock(my_list_mutex); // protect my_list.empty() call
        if (my_list.empty())
            return default_value();
        // non-zero, if market is active
        const std::size_t workers = market::max_num_workers();
        // We can't exceed market's maximal number of workers.
        // +1 to take external thread into account
        return workers? min(workers+1, my_active_value): my_active_value;
    }
public:
    std::size_t active_value_if_present() const {
        return !my_list.empty() ? my_active_value : 0;
    }
};

class alignas(max_nfs_size) stack_size_control : public control_storage {
    virtual std::size_t default_value() const override {
        return ThreadStackSize;
    }
    virtual void apply_active(std::size_t new_active) override {
        control_storage::apply_active(new_active);
#if __TBB_WIN8UI_SUPPORT && (_WIN32_WINNT < 0x0A00)
        __TBB_ASSERT( false, "For Windows 8 Store* apps we must not set stack size" );
#endif
    }
};

class alignas(max_nfs_size) terminate_on_exception_control : public control_storage {
    virtual std::size_t default_value() const override {
        return 0;
    }
};

#if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
class alignas(max_nfs_size) lifetime_control : public control_storage {
    virtual bool is_first_arg_preferred(std::size_t, std::size_t) const override {
        return false; // not interested
    }
    virtual std::size_t default_value() const override {
        return 0;
    }
    virtual void apply_active(std::size_t new_active) override {
        if (new_active == 1) {
            // reserve the market reference
            market::global_market_mutex_type::scoped_lock lock( market::theMarketMutex );
            if (market::theMarket) {
                market::add_ref_unsafe(lock, /*is_public*/ true);
            }
        } else if (new_active == 0) { // new_active == 0
            // release the market reference
            market::global_market_mutex_type::scoped_lock lock( market::theMarketMutex );
            if (market::theMarket != nullptr) {
                lock.release();
                market::theMarket->release(/*is_public*/ true, /*blocking_terminate*/ false);
            }
        }
        control_storage::apply_active(new_active);
    }

public:
    bool is_empty() {
        spin_mutex::scoped_lock lock(my_list_mutex);
        return my_list.empty();
    }
};
#endif // __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE

static allowed_parallelism_control allowed_parallelism_ctl;
static stack_size_control stack_size_ctl;
static terminate_on_exception_control terminate_on_exception_ctl;
#if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
static lifetime_control lifetime_ctl;
static control_storage *controls[] = {&allowed_parallelism_ctl, &stack_size_ctl, &terminate_on_exception_ctl, &lifetime_ctl};
#else
static control_storage *controls[] = {&allowed_parallelism_ctl, &stack_size_ctl, &terminate_on_exception_ctl};
#endif // __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE

//! Comparator for a set of global_control objects
inline bool control_storage_comparator::operator()(const global_control* lhs, const global_control* rhs) const {
    __TBB_ASSERT_RELEASE(lhs->my_param < global_control::parameter_max , NULL);
    return lhs->my_value < rhs->my_value || (lhs->my_value == rhs->my_value && lhs < rhs);
}

unsigned market::app_parallelism_limit() {
    return allowed_parallelism_ctl.active_value_if_present();
}

bool terminate_on_exception() {
    return global_control::active_value(global_control::terminate_on_exception) == 1;
}

#if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
unsigned market::is_lifetime_control_present() {
    return !lifetime_ctl.is_empty();
}
#endif // __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE

struct global_control_impl {
private:
    static bool erase_if_present(control_storage* const c, d1::global_control& gc) {
        auto it = c->my_list.find(&gc);
        if (it != c->my_list.end()) {
            c->my_list.erase(it);
            return true;
        }
        return false;
    }

public:

    static void create(d1::global_control& gc) {
        __TBB_ASSERT_RELEASE(gc.my_param < global_control::parameter_max, NULL);
        control_storage* const c = controls[gc.my_param];

        spin_mutex::scoped_lock lock(c->my_list_mutex);
        if (c->my_list.empty() || c->is_first_arg_preferred(gc.my_value, c->my_active_value)) {
            // to guarantee that apply_active() is called with current active value,
            // calls it here and in internal_destroy() under my_list_mutex
            c->apply_active(gc.my_value);
        }
        c->my_list.insert(&gc);
    }

    static void destroy(d1::global_control& gc) {
        __TBB_ASSERT_RELEASE(gc.my_param < global_control::parameter_max, NULL);
        control_storage* const c = controls[gc.my_param];
#if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
        __TBB_ASSERT(gc.my_param == global_control::scheduler_handle || !c->my_list.empty(), NULL);
#else
        __TBB_ASSERT(!c->my_list.empty(), NULL);
#endif // __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
        // Concurrent reading and changing global parameter is possible.
        spin_mutex::scoped_lock lock(c->my_list_mutex);
        std::size_t new_active = (std::size_t)(-1), old_active = c->my_active_value;

        if (!erase_if_present(c, gc)) {
#if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
            __TBB_ASSERT(gc.my_param == global_control::scheduler_handle , NULL);
            return;
#else
            __TBB_ASSERT(false, "Unreachable code");
#endif // __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
        }
        if (c->my_list.empty()) {
            __TBB_ASSERT(new_active == (std::size_t) - 1, NULL);
            new_active = c->default_value();
        } else {
            new_active = (*c->my_list.begin())->my_value;
        }
        if (new_active != old_active) {
            c->apply_active(new_active);
        }
    }

    static bool remove_and_check_if_empty(d1::global_control& gc) {
        __TBB_ASSERT_RELEASE(gc.my_param < global_control::parameter_max, NULL);
        control_storage* const c = controls[gc.my_param];
        __TBB_ASSERT(!c->my_list.empty(), NULL);

        spin_mutex::scoped_lock lock(c->my_list_mutex);
        erase_if_present(c, gc);
        return c->my_list.empty();
    }
#if TBB_USE_ASSERT
    static bool is_present(d1::global_control& gc) {
        __TBB_ASSERT_RELEASE(gc.my_param < global_control::parameter_max, NULL);
        control_storage* const c = controls[gc.my_param];

        spin_mutex::scoped_lock lock(c->my_list_mutex);
        auto it = c->my_list.find(&gc);
        if (it != c->my_list.end()) {
            return true;
        }
        return false;
    }
#endif // TBB_USE_ASSERT
};

void __TBB_EXPORTED_FUNC create(d1::global_control& gc) {
    global_control_impl::create(gc);
}
void __TBB_EXPORTED_FUNC destroy(d1::global_control& gc) {
    global_control_impl::destroy(gc);
}

bool remove_and_check_if_empty(d1::global_control& gc) {
    return global_control_impl::remove_and_check_if_empty(gc);
}
#if TBB_USE_ASSERT
bool is_present(d1::global_control& gc) {
    return global_control_impl::is_present(gc);
}
#endif // TBB_USE_ASSERT
std::size_t __TBB_EXPORTED_FUNC global_control_active_value(int param) {
    __TBB_ASSERT_RELEASE(param < global_control::parameter_max, NULL);
    return controls[param]->active_value();
}

} // namespace r1
} // namespace detail
} // namespace tbb