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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//! A wrapper around platform event notification APIs (currently via [mio](https://github.com/carllerche/mio)) that can also handle high-resolution timer events, including those set (on another thread) *during* a `notifier.wait()` call.
//!
//! **[Crates.io](https://crates.io/crates/notifier) │ [Repo](https://github.com/alecmocatta/notifier)**
//!
//! Delivers **edge-triggered** notifications for file descriptor state changes (corresponding to `mio::Ready::readable() | mio::Ready::writable() | mio::unix::UnixReady::hup() | mio::unix::UnixReady::error()`) as well as elapsing of instants.
//!
//! It's designed to be used in conjunction with a library that exhaustively collects events (e.g. connected, data in, data available to be written, remote closed, bytes acked, connection errors) upon each edge-triggered notification – for example [`tcp_typed`](https://github.com/alecmocatta/tcp_typed).
//!
//! # Note
//!
//! Currently doesn't support Windows.

#![doc(html_root_url = "https://docs.rs/notifier/0.1.1")]
#![warn(
	// missing_copy_implementations,
	// missing_debug_implementations,
	// missing_docs,
	trivial_casts,
	trivial_numeric_casts,
	unused_import_braces,
	unused_qualifications,
	unused_results,
	clippy::pedantic
)] // from https://github.com/rust-unofficial/patterns/blob/master/anti_patterns/deny-warnings.md
#![allow(
	clippy::new_without_default,
	clippy::indexing_slicing,
	clippy::needless_pass_by_value,
	clippy::inline_always
)]

mod heap;
mod timer;

use either::Either;
use log::trace;
use std::{cmp, collections::HashSet, marker, mem, sync, time};

#[cfg(unix)]
type Fd = std::os::unix::io::RawFd;
#[cfg(windows)]
type Fd = std::os::windows::io::RawHandle;

pub struct NotifierContext<'a, Key: 'a>
where
	Key: Clone + Into<usize>,
	usize: Into<Key>,
{
	executor: &'a Notifier<Key>,
	key: Key,
}
impl<'a, Key: 'a> NotifierContext<'a, Key>
where
	Key: Clone + Into<usize>,
	usize: Into<Key>,
{
	#[inline(always)]
	pub fn add_trigger(&self) -> (Triggerer, Triggeree) {
		self.executor.add_trigger(self.key.clone())
	}
	#[inline(always)]
	pub fn queue(&self) {
		let _ = self.add_instant(time::Instant::now());
	}
	#[inline(always)]
	pub fn add_fd(&self, fd: Fd) {
		self.executor.add_fd(fd, self.key.clone())
	}
	#[inline(always)]
	pub fn remove_fd(&self, fd: Fd) {
		self.executor.remove_fd(fd, self.key.clone())
	}
	#[inline(always)]
	pub fn add_instant(&self, instant: time::Instant) -> heap::Slot {
		self.executor.add_instant(instant, self.key.clone())
	}
	#[inline(always)]
	pub fn remove_instant(&self, slot: heap::Slot) {
		self.executor.remove_instant(slot)
	}
}

#[cfg(feature = "tcp_typed")]
impl<'a, Key: 'a> tcp_typed::Notifier for NotifierContext<'a, Key>
where
	Key: Clone + Into<usize>,
	usize: Into<Key>,
{
	type InstantSlot = heap::Slot;
	#[inline(always)]
	fn queue(&self) {
		self.queue()
	}
	#[inline(always)]
	fn add_fd(&self, fd: Fd) {
		self.add_fd(fd)
	}
	#[inline(always)]
	fn remove_fd(&self, fd: Fd) {
		self.remove_fd(fd)
	}
	#[inline(always)]
	fn add_instant(&self, instant: time::Instant) -> heap::Slot {
		self.add_instant(instant)
	}
	#[inline(always)]
	fn remove_instant(&self, slot: heap::Slot) {
		self.remove_instant(slot)
	}
}

struct TimeEvent<Key>(time::Instant, Key);
impl<Key> PartialEq for TimeEvent<Key> {
	#[inline(always)]
	fn eq(&self, other: &Self) -> bool {
		self.0.eq(&other.0)
	}
}
impl<Key> Eq for TimeEvent<Key> {}
impl<Key> PartialOrd for TimeEvent<Key> {
	#[inline(always)]
	fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
		Some(self.0.cmp(&other.0))
	}
}
impl<Key> Ord for TimeEvent<Key> {
	#[inline(always)]
	fn cmp(&self, other: &Self) -> cmp::Ordering {
		self.0.cmp(&other.0)
	}
}
pub struct Notifier<Key>
where
	Key: Clone + Into<usize>,
	usize: Into<Key>,
{
	notifier_timeout: NotifierTimeout<Key>,
	// queue: Vec<Key>,
	timer: sync::RwLock<heap::Heap<TimeEvent<Key>>>,
}
impl<Key> Notifier<Key>
where
	Key: Clone + Into<usize>,
	usize: Into<Key>,
{
	pub fn new() -> Self {
		Self {
			notifier_timeout: NotifierTimeout::new(),
			// queue: Vec::new(),
			timer: sync::RwLock::new(heap::Heap::new()),
		}
	}

	pub fn context(&self, key: Key) -> NotifierContext<Key> {
		NotifierContext {
			executor: self,
			key,
		}
	}

	fn add_fd(&self, fd: Fd, data: Key) {
		self.notifier_timeout.add(
			&mio::unix::EventedFd(&fd),
			mio::Ready::readable()
				| mio::Ready::writable()
				| mio::unix::UnixReady::hup()
				| mio::unix::UnixReady::error(), // EPOLLRDHUP?
			data,
		);
	}

	fn remove_fd(&self, fd: Fd, data: Key) {
		self.notifier_timeout
			.delete(&mio::unix::EventedFd(&fd), data);
	}

	fn add_instant(&self, instant: time::Instant, data: Key) -> heap::Slot {
		trace!("add_instant {:?}", instant);
		let mut timer = self.timer.write().unwrap();
		let slot = timer.push(TimeEvent(instant, data));
		self.notifier_timeout.update_timeout(instant);
		slot
	}

	fn remove_instant(&self, slot: heap::Slot) {
		let _ = self.timer.write().unwrap().remove(slot); // TODO
	}

	fn add_trigger(&self, data: Key) -> (Triggerer, Triggeree) {
		let (registration, set_readiness) = mio::Registration::new2();
		self.notifier_timeout
			.add(&registration, mio::Ready::readable(), data);
		(Triggerer(set_readiness), Triggeree(registration))
	}

	pub fn wait<F: FnMut(Either<mio::Ready, time::Instant>, Key)>(&self, mut f: F) {
		let mut done_any = false;
		let now = time::Instant::now();
		let timeout = {
			loop {
				let TimeEvent(timeout, poll_key) = {
					let timer = &mut *self.timer.write().unwrap();
					if timer.peek().is_some() && timer.peek().unwrap().0 <= now {
						trace!(
							"timeout unelapsed {:?} <= {:?}",
							timer.peek().unwrap().0,
							now
						);
					}
					if timer.peek().is_none() || timer.peek().unwrap().0 > now {
						break;
					}
					timer.pop().unwrap()
				};
				done_any = true;
				trace!("ran timeout {:?}", timeout);
				f(Either::Right(timeout), poll_key)
			}
			self.timer.read().unwrap().peek().map(|x| x.0)
		};
		trace!("\\wait {:?}", timeout);
		if let Some(timeout) = timeout {
			self.notifier_timeout.update_timeout(timeout);
		}
		self.notifier_timeout
			.wait(done_any, |flags, poll_key| f(Either::Left(flags), poll_key));
		trace!("/wait");
		let now = time::Instant::now();
		loop {
			let TimeEvent(timeout, poll_key) = {
				let timer = &mut *self.timer.write().unwrap();
				if timer.peek().is_some() && timer.peek().unwrap().0 <= now {
					trace!(
						"timeout unelapsed {:?} <= {:?}",
						timer.peek().unwrap().0,
						now
					);
				}
				if timer.peek().is_none() || timer.peek().unwrap().0 > now {
					break;
				}
				timer.pop().unwrap()
			};
			trace!("ran timeout {:?}", timeout);
			f(Either::Right(timeout), poll_key)
		}
	}
}
pub struct Triggerer(mio::SetReadiness);
impl Drop for Triggerer {
	fn drop(&mut self) {
		self.0.set_readiness(mio::Ready::readable()).unwrap();
	}
}
pub struct Triggeree(mio::Registration);

const POLL_BUF_LENGTH: usize = 100;
const POLL_TIMER: mio::Token = mio::Token(usize::max_value() - 1); // max_value() is taken by mio

struct NotifierTimeout<Key>
where
	Key: Clone + Into<usize>,
	usize: Into<Key>,
{
	poll: mio::Poll,
	timer: timer::Timer,
	timeout: sync::Mutex<Option<time::Instant>>,
	strip: sync::Mutex<Option<HashSet<usize>>>,
	marker: marker::PhantomData<fn(Key)>,
}
impl<Key> NotifierTimeout<Key>
where
	Key: Clone + Into<usize>,
	usize: Into<Key>,
{
	fn new() -> Self {
		let poll = mio::Poll::new().unwrap();
		let timer = timer::Timer::new();
		poll.register(
			&timer,
			POLL_TIMER,
			mio::Ready::readable(),
			mio::PollOpt::edge(),
		)
		.unwrap();
		Self {
			poll,
			timer,
			timeout: sync::Mutex::new(None),
			strip: sync::Mutex::new(None),
			marker: marker::PhantomData,
		}
	}

	fn add<E: mio::event::Evented + ?Sized>(&self, fd: &E, events: mio::Ready, data: Key) {
		let data: usize = data.into();
		assert_ne!(mio::Token(data), POLL_TIMER);
		if let Some(ref mut strip) = *self.strip.lock().unwrap() {
			let _ = strip.remove(&data);
		}
		self.poll
			.register(fd, mio::Token(data), events, mio::PollOpt::edge())
			.unwrap();
	}

	fn delete<E: mio::event::Evented + ?Sized>(&self, fd: &E, data: Key) {
		self.poll.deregister(fd).unwrap();
		if let Some(ref mut strip) = *self.strip.lock().unwrap() {
			let x = strip.insert(data.into());
			assert!(x);
		}
	}

	fn update_timeout(&self, timeout: time::Instant) {
		let mut current_timeout = self.timeout.lock().unwrap();
		trace!("update_timeout {:?} {:?}", current_timeout, timeout);
		if current_timeout.is_none() || timeout < current_timeout.unwrap() {
			*current_timeout = Some(timeout);
			self.timer.set_timeout(timeout);
		}
	}

	fn wait<F: FnMut(mio::Ready, Key)>(&self, mut nonblock: bool, mut f: F) {
		let mut events = mio::Events::with_capacity(POLL_BUF_LENGTH);
		loop {
			let x = mem::replace(&mut *self.strip.lock().unwrap(), Some(HashSet::new()));
			assert!(x.is_none());
			let n = loop {
				trace!("\\mio_wait {:?}", nonblock);
				let n = self
					.poll
					.poll(
						&mut events,
						if nonblock {
							Some(time::Duration::new(0, 0))
						} else {
							None
						},
					)
					.unwrap();
				trace!("/mio_wait: {:?}", n);
				if !nonblock && n == 0 {
					continue;
				}
				let mut current_timeout = self.timeout.lock().unwrap();
				if self.timer.elapsed() {
					*current_timeout = None;
				}
				break n;
			};
			assert!(n <= events.capacity());
			let strip = mem::replace(&mut *self.strip.lock().unwrap(), None).unwrap(); // TODO: currently Context needs to do its own check for strips added after this point
			for x in events
				.iter()
				.filter(|x| x.token() != POLL_TIMER && !strip.contains(&x.token().0))
			{
				f(x.readiness(), x.token().0.into())
			}
			if n < events.capacity() {
				break;
			}
			nonblock = true;
		}
	}
}
impl<Key> Drop for NotifierTimeout<Key>
where
	Key: Clone + Into<usize>,
	usize: Into<Key>,
{
	fn drop(&mut self) {
		self.poll.deregister(&self.timer).unwrap();
	}
}