-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreemption.rs
240 lines (202 loc) · 6.91 KB
/
preemption.rs
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
use crate::reusable::ReusableSync;
use crate::timer::Timer;
use gotcha::Group;
use gotcha::group_thread_set;
use signal::pthread::pthread_kill;
use signal::pthread::pthread_self;
use signal::Handler;
use signal::Operation;
use signal::Set;
use signal::Signal;
use signal::Sigset;
use signal::sigaction;
use std::cell::RefCell;
use std::io::Result as IoResult;
use std::os::raw::c_int;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
thread_local! {
static SIGNAL: RefCell<Option<RealThreadId>> = RefCell::default();
// Whether we had to delay preemption checks until the end of a nonpreemptible call.
static DEFERRED: AtomicBool = AtomicBool::new(false);
}
pub fn thread_signal() -> Result<Signal, ()> {
// Because this is called from signal handlers, it might happen during thread teardown, when
// the thread-local variable is being/has been destructed. In such a case, we simply report
// that the current thread has no preemption signal assigned (any longer).
SIGNAL.try_with(|signal|
signal.borrow().as_ref().map(|signal| {
let RealThreadId (signal) = signal;
signal.borrow().as_ref().map(|signal|
*signal.signal
)
}).unwrap_or(None).ok_or(())
).unwrap_or(Err(()))
}
extern fn resume_preemption() {
// Skip if this trampoline is running in a destructor during thread teardown.
drop(enable_preemption(None));
}
pub fn enable_preemption(group: Option<Group>) -> Result<Option<Signal>, ()> {
use timetravel::errno::errno;
// We must access errno_group() even when we won't use it so that the initial
// enable_preemption() call bootstraps later resume_preemption() ones!
let erryes = errno_group(group);
let errno = *errno();
// We can only call thread_signal() if the preemption signal is already blocked; otherwise,
// the signal handler might race on the thread-local SIGNAL variable. It's fine to do when:
// * We have been passed a group, because in this case preemption was previously disabled.
// - OR -
// * Preemption has been deferred, because when setting the flag, the signal handler will
// have masked out the signal.
let mut unblock = None;
if let Some(group) = group {
// It's important we don't unmask the preemption signal until we've switched groups;
// otherwise, its handler may run immediately and remask it!
group_thread_set!(group);
unblock.replace(thread_signal()?);
}
// else the caller is asserting the group change has already been performed.
if DEFERRED.with(|deferred| deferred.swap(false, Ordering::Relaxed)) {
let signal = unblock.get_or_insert(thread_signal()?);
drop(pthread_kill(pthread_self(), *signal));
}
if let Some(signal) = unblock {
drop(mask(Operation::Unblock, signal));
}
// Propagate any errors encountered during our libc replacements back to the calling libset.
if group.is_none() && errno != 0 {
*erryes = errno;
}
Ok(unblock)
}
pub fn disable_preemption(block: Option<Signal>) {
group_thread_set!(Group::SHARED);
if let Some(signal) = block {
// Mask the preemption signal without calling thread_signal(), which would by racy.
drop(mask(Operation::Block, signal));
}
SIGNAL.with(|signal| signal.replace(None));
DEFERRED.with(|deferred| deferred.store(false, Ordering::Relaxed));
}
pub fn is_preemptible() -> bool {
use gotcha::group_thread_get;
! group_thread_get!().is_shared()
}
// It is only safe to call this function while preemption is (temporarily) disabled!
pub fn defer_preemption(signum: Option<(&mut Sigset, Signal)>) {
debug_assert!(! is_preemptible());
// We must first mask the signal so no attempted preemption races on DEFERRED!
if let Some((sigmask, signo)) = signum {
// Caller is asserting we are beneath a signal handler, so we should only update the
// outside world's mask.
sigmask.add(signo);
} else {
drop(mask(Operation::Block, thread_signal().unwrap()));
}
DEFERRED.with(|deferred| deferred.store(true, Ordering::Relaxed));
}
pub fn thread_setup(thread: RealThreadId, handler: Handler, quantum: u64) -> IoResult<()> {
use gotcha::shared_hook;
use std::sync::Once;
let RealThreadId (signal) = thread;
if signal.borrow().is_none() {
signal.replace(Some(PreemptionSignal::new(handler, quantum)?));
}
SIGNAL.with(|signal| signal.replace(Some(thread)));
static INIT: Once = Once::new();
INIT.call_once(|| shared_hook(resume_preemption));
Ok(())
}
fn errno_group(group: Option<Group>) -> &'static mut c_int {
use gotcha::group_lookup_symbol_fn;
use libc::__errno_location;
thread_local! {
static ERRNO_LOCATION: RefCell<Option<unsafe extern fn() -> *mut c_int>> =
RefCell::new(None);
}
// We save the location in a thread-local variable so the next time we need to find its
// *location,* we won't clear its *value* in the process!
ERRNO_LOCATION.with(|errno_location| {
let mut errno_location = errno_location.borrow_mut();
if let Some(group) = group {
errno_location.replace(unsafe {
group_lookup_symbol_fn!(group, __errno_location)
}.unwrap());
}
let __errno_location = errno_location.unwrap();
unsafe {
&mut *__errno_location()
}
})
}
fn mask(un: Operation, no: Signal) -> IoResult<()> {
use signal::pthread_sigmask;
let mut set = Sigset::empty();
set.add(no);
pthread_sigmask(un, &set, None)
}
pub struct RealThreadId (&'static RefCell<Option<PreemptionSignal>>);
impl RealThreadId {
pub fn current() -> Self {
use crate::lifetime::unbound;
thread_local! {
static SIGNALER: RefCell<Option<PreemptionSignal>> = RefCell::default();
}
Self (SIGNALER.with(|signaler| unsafe {
unbound(signaler)
}))
}
}
struct PreemptionSignal {
signal: ReusableSync<'static, Signal>,
timer: Timer,
}
impl PreemptionSignal {
fn new(handler: Handler, quantum: u64) -> IoResult<Self> {
use crate::signals::assign_signal;
use crate::timer::Clock;
use crate::timer::Sigevent;
use crate::timer::itimerspec;
use crate::timer::timer_create;
use crate::timer::timer_settime;
use libc::SA_RESTART;
use libc::SA_SIGINFO;
use libc::timespec;
use signal::Action;
use signal::Sigaction;
let signal = assign_signal().expect("libinger: no available signal for preempting this thread");
let sa = Sigaction::new(handler, Sigset::empty(), SA_SIGINFO | SA_RESTART);
sigaction(*signal, &sa, None)?;
mask(Operation::Block, *signal)?;
let mut se = Sigevent::signal(*signal);
let timer = timer_create(Clock::Real, &mut se)?;
let quantum: i64 = quantum as _;
let mut it = itimerspec {
it_interval: timespec {
tv_sec: 0,
tv_nsec: quantum * 1_000,
},
it_value: timespec {
tv_sec: 0,
tv_nsec: quantum * 1_000,
},
};
timer_settime(timer, false, &mut it, None)?;
Ok(Self {
signal,
timer,
})
}
}
impl Drop for PreemptionSignal {
fn drop(&mut self) {
use crate::timer::timer_delete;
if let Err(or) = timer_delete(self.timer) {
eprintln!("libinger: unable to delete POSIX timer: {}", or);
}
if let Err(or) = sigaction(*self.signal, &(), None) {
eprintln!("libinger: unable to unregister signal handler: {}", or);
}
}
}