forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreempt.hh
77 lines (64 loc) · 1.74 KB
/
preempt.hh
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
/*
* Copyright (C) 2018-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <seastar/util/bool_class.hh>
#include <seastar/util/noncopyable_function.hh>
#include <seastar/core/preempt.hh>
#include <seastar/core/thread.hh>
#include "seastarx.hh"
class is_preemptible_tag;
using is_preemptible = bool_class<is_preemptible_tag>;
/// A function which decides when to preempt.
/// If it returns true then the algorithm will be interrupted.
using preemption_check = noncopyable_function<bool() noexcept>;
inline
preemption_check default_preemption_check() {
return [] () noexcept {
return seastar::need_preempt();
};
}
inline
preemption_check never_preempt() {
return [] () noexcept {
return false;
};
}
inline
preemption_check always_preempt() {
return [] () noexcept {
return true;
};
}
struct basic_preemption_source {
bool should_preempt() {
return seastar::need_preempt();
}
void thread_yield() {
seastar::thread::yield();
}
};
struct custom_preemption_source {
struct impl {
virtual bool should_preempt() = 0;
virtual void thread_yield() = 0;
virtual ~impl() = default;
};
std::unique_ptr<impl> _impl;
bool should_preempt() {
return _impl ? _impl->should_preempt() : seastar::need_preempt();
}
void thread_yield() {
_impl ? _impl->thread_yield() : seastar::thread::yield();
}
custom_preemption_source() = default;
custom_preemption_source(std::unique_ptr<impl> i) : _impl(std::move(i)) {}
};
#ifdef SCYLLA_ENABLE_PREEMPTION_SOURCE
using preemption_source = custom_preemption_source;
#else
using preemption_source = basic_preemption_source;
#endif