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
use std::mem;
use std::ptr;
use std::time::Duration;
use libc::c_void;
use libc::eventfd;
use libc::read;
use libc::write;
use libc::POLLIN;
use serde::Deserialize;
use serde::Serialize;
use super::duration_to_timespec;
use super::errno_result;
use super::RawDescriptor;
use super::Result;
use crate::descriptor::AsRawDescriptor;
use crate::descriptor::FromRawDescriptor;
use crate::descriptor::IntoRawDescriptor;
use crate::descriptor::SafeDescriptor;
use crate::EventWaitResult;
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub(crate) struct PlatformEvent {
event_handle: SafeDescriptor,
}
pub trait EventExt {
fn write_count(&self, v: u64) -> Result<()>;
fn read_count(&self) -> Result<u64>;
}
impl EventExt for crate::Event {
fn write_count(&self, v: u64) -> Result<()> {
self.0.write_count(v)
}
fn read_count(&self) -> Result<u64> {
self.0.read_count()
}
}
impl PlatformEvent {
pub fn new() -> Result<PlatformEvent> {
let ret = unsafe { eventfd(0, 0) };
if ret < 0 {
return errno_result();
}
Ok(PlatformEvent {
event_handle: unsafe { SafeDescriptor::from_raw_descriptor(ret) },
})
}
pub fn write_count(&self, v: u64) -> Result<()> {
let ret = unsafe {
write(
self.as_raw_descriptor(),
&v as *const u64 as *const c_void,
mem::size_of::<u64>(),
)
};
if ret <= 0 {
return errno_result();
}
Ok(())
}
pub fn read_count(&self) -> Result<u64> {
let mut buf: u64 = 0;
let ret = unsafe {
read(
self.as_raw_descriptor(),
&mut buf as *mut u64 as *mut c_void,
mem::size_of::<u64>(),
)
};
if ret <= 0 {
return errno_result();
}
Ok(buf)
}
pub fn signal(&self) -> Result<()> {
self.write_count(1)
}
pub fn wait(&self) -> Result<()> {
self.read_count().map(|_| ())
}
pub fn wait_timeout(&self, timeout: Duration) -> Result<EventWaitResult> {
let mut pfd = libc::pollfd {
fd: self.as_raw_descriptor(),
events: POLLIN,
revents: 0,
};
let timeoutspec: libc::timespec = duration_to_timespec(timeout);
let ret = unsafe {
libc::ppoll(
&mut pfd as *mut libc::pollfd,
1,
&timeoutspec,
ptr::null_mut(),
)
};
if ret < 0 {
return errno_result();
}
if pfd.revents == 0 {
return Ok(EventWaitResult::TimedOut);
}
self.wait()?;
Ok(EventWaitResult::Signaled)
}
pub fn reset(&self) -> Result<()> {
let _: EventWaitResult = self.wait_timeout(Duration::ZERO)?;
Ok(())
}
pub fn try_clone(&self) -> Result<PlatformEvent> {
self.event_handle
.try_clone()
.map(|event_handle| PlatformEvent { event_handle })
}
}
impl AsRawDescriptor for PlatformEvent {
fn as_raw_descriptor(&self) -> RawDescriptor {
self.event_handle.as_raw_descriptor()
}
}
impl FromRawDescriptor for PlatformEvent {
unsafe fn from_raw_descriptor(descriptor: RawDescriptor) -> Self {
PlatformEvent {
event_handle: SafeDescriptor::from_raw_descriptor(descriptor),
}
}
}
impl IntoRawDescriptor for PlatformEvent {
fn into_raw_descriptor(self) -> RawDescriptor {
self.event_handle.into_raw_descriptor()
}
}
impl From<PlatformEvent> for SafeDescriptor {
fn from(evt: PlatformEvent) -> Self {
evt.event_handle
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Event;
use crate::EventExt;
#[test]
fn new() {
Event::new().unwrap();
}
#[test]
fn read_write() {
let evt = Event::new().unwrap();
evt.write_count(55).unwrap();
assert_eq!(evt.read_count(), Ok(55));
}
#[test]
fn clone() {
let evt = Event::new().unwrap();
let evt_clone = evt.try_clone().unwrap();
evt.write_count(923).unwrap();
assert_eq!(evt_clone.read_count(), Ok(923));
}
#[test]
fn timeout() {
let evt = Event::new().expect("failed to create eventfd");
assert_eq!(
evt.wait_timeout(Duration::from_millis(1))
.expect("failed to read from eventfd with timeout"),
EventWaitResult::TimedOut
);
}
}