audio-device 0.1.0-alpha.6

A library for interacting with audio devices
Documentation
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use crate::libc as c;
use crate::loom::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use crate::loom::sync::{Arc, Mutex};
use crate::loom::thread;
use crate::runtime::atomic_waker::AtomicWaker;
use crate::unix::errno::Errno;
use crate::Result;
use std::collections::HashMap;
use std::mem;

/// The token associated with the current waiter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Token(c::c_int);

/// A guard for the returned events of the poll handler.
///
/// Dropping this handle will allow the background thread to poll the handler
/// again.
///
/// Constructed by waiting on [AsyncPoll::returned_events].
pub struct PollEventsGuard<'a> {
    events: c::c_short,
    shared: &'a Shared,
    token: Token,
}

impl PollEventsGuard<'_> {
    /// Access the returned events.
    pub fn events(&self) -> c::c_short {
        self.events
    }
}

impl Drop for PollEventsGuard<'_> {
    fn drop(&mut self) {
        self.shared.holders.lock().released.push(self.token);

        if let Err(e) = self.shared.parker.send(1) {
            log::error!("failed to unpark background thread: {}", e);
        }
    }
}

/// An unsafe asynchronous poller around a `pollfd`.
///
/// See [AsyncPoll::new].
pub struct AsyncPoll {
    shared: Arc<Shared>,
    waker: Arc<Waker>,
}

impl AsyncPoll {
    /// Construct a new poll handle around the given `pollfd`, registering it
    /// for interest in receiving events asynchronously.
    ///
    /// Dropping the returned handle will unregister interest.
    ///
    /// # Panics
    ///
    /// Panics unless an audio runtime is available.
    ///
    /// See [Runtime][crate::runtime::Runtime].
    ///
    /// # Safety
    ///
    /// This is unsafe, because the caller must ensure that the provided
    /// `pollfd` is not closed before this handle is dropped.
    pub unsafe fn new(descriptor: c::pollfd) -> Result<AsyncPoll, Errno> {
        crate::runtime::with_poll(|poll| {
            let waker = Arc::new(Waker {
                waker: AtomicWaker::new(),
                descriptor,
                returned_events: AtomicUsize::new(0),
            });

            poll.shared.holders.lock().added.push(waker.clone());
            poll.shared.parker.send(1)?;

            Ok(AsyncPoll {
                shared: poll.shared.clone(),
                waker,
            })
        })
    }

    /// Wait for events to be triggered on the background driver and return a
    /// guard to the events.
    ///
    /// Once this guard is dropped the driver will be released to register more
    /// interest.
    pub async fn returned_events(&self) -> PollEventsGuard<'_> {
        use std::future::Future;
        use std::pin::Pin;
        use std::task::{Context, Poll};

        return ReturnedEvents(self).await;

        struct ReturnedEvents<'a>(&'a AsyncPoll);

        impl<'a> Future for ReturnedEvents<'a> {
            type Output = PollEventsGuard<'a>;

            fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
                self.0.waker.waker.register_by_ref(cx.waker());
                let returned_events = self.0.waker.returned_events.swap(0, Ordering::Acquire);

                if returned_events != 0 {
                    Poll::Ready(PollEventsGuard {
                        events: returned_events as c::c_short,
                        shared: &*self.0.shared,
                        token: self.0.waker.token(),
                    })
                } else {
                    Poll::Pending
                }
            }
        }
    }
}

impl Drop for AsyncPoll {
    fn drop(&mut self) {
        self.shared.holders.lock().removed.push(self.waker.token());

        if let Err(e) = self.shared.parker.send(1) {
            log::error!("failed to unpark background thread: {}", e);
        }
    }
}

/// Data on the waker for a handle.
pub(crate) struct Waker {
    /// The waker to call when waking up the task waiting for events.
    pub(crate) waker: AtomicWaker,
    /// The descriptors associated with this waker.
    descriptor: c::pollfd,
    /// The last revents decoded. `None` if no events are ready.
    returned_events: AtomicUsize,
}

impl Waker {
    /// Get the token associated with this waker.
    ///
    /// Note: always the first file descriptor.
    fn token(&self) -> Token {
        Token(self.descriptor.fd)
    }
}

pub(crate) struct Shared {
    pub(crate) running: AtomicBool,
    pub(crate) holders: Mutex<Events>,
    pub(crate) parker: EventFd,
}

#[derive(Default)]
pub(crate) struct Events {
    added: Vec<Arc<Waker>>,
    released: Vec<Token>,
    removed: Vec<Token>,
}

impl Events {
    // Process all queued elements in the driver.
    fn process(&mut self, driver: &mut Driver, wakers: &mut Vec<Arc<Waker>>) -> Result<()> {
        let mut added = mem::replace(&mut self.added, Vec::new());

        for waker in added.drain(..) {
            let loc = Loc {
                descriptor: driver.descriptors.len(),
                waker: wakers.len(),
            };

            driver.locations.insert(waker.token(), loc);
            driver.descriptors.push(waker.descriptor);
            wakers.push(waker);
        }

        let mut removed = mem::replace(&mut self.removed, Vec::new());

        for token in removed.drain(..) {
            if let Some(loc) = driver.locations.remove(&token) {
                driver.descriptors.swap_remove(loc.descriptor);
                wakers.swap_remove(loc.waker);

                // re-organize unless we're removing the last waker.
                if wakers.len() != loc.waker {
                    // NB: redirect swap removed.
                    let token = wakers[loc.waker].token();
                    driver.locations.insert(token, loc);
                }
            }
        }

        let mut released = mem::replace(&mut self.released, Vec::new());

        for r in released.drain(..) {
            if let Some(Loc { descriptor, waker }) = driver.locations.get(&r) {
                driver.descriptors[*descriptor].fd = wakers[*waker].descriptor.fd;
            }
        }

        self.added = added;
        self.removed = removed;
        self.released = released;
        Ok(())
    }
}

/// An executor to drive things which are woken up by polling.
pub struct PollDriver {
    thread: Option<thread::JoinHandle<()>>,
    shared: Arc<Shared>,
}

impl PollDriver {
    /// Construct a new events windows event object driver and return its
    /// handle.
    pub fn new() -> Result<Self> {
        let shared = Arc::new(Shared {
            running: AtomicBool::new(true),
            holders: Mutex::new(Events::default()),
            parker: EventFd::new()?,
        });

        let thread = thread::spawn({
            let shared = shared.clone();
            || Driver::start(shared)
        });

        let handle = Self {
            thread: Some(thread),
            shared,
        };

        Ok(handle)
    }

    /// Join the current handle.
    ///
    /// # Panics
    ///
    /// This panics if the background thread panicked. But this should only ever
    /// happen if there's a bug.
    pub fn join(mut self) {
        self.inner_join();
    }

    fn inner_join(&mut self) {
        if let Some(thread) = self.thread.take() {
            self.shared.running.store(false, Ordering::Release);

            if let Err(errno) = self.shared.parker.send(0) {
                panic!("failed to set event: {}", errno);
            }

            if thread.join().is_err() {
                panic!("event handler thread panicked");
            }
        }
    }
}

impl Drop for PollDriver {
    fn drop(&mut self) {
        let _ = self.inner_join();
    }
}

#[derive(Debug, Clone, Copy)]
struct Loc {
    descriptor: usize,
    waker: usize,
}

struct Driver {
    /// Location of a given token.
    locations: HashMap<Token, Loc>,
    /// The descriptors being driven.
    descriptors: Vec<libc::pollfd>,
}

impl Driver {
    fn run(mut self, guard: &mut PanicGuard) -> Result<()> {
        while guard.shared.running.load(Ordering::Acquire) {
            let mut result = unsafe {
                errno!(libc::poll(
                    self.descriptors.as_mut_ptr(),
                    self.descriptors.len() as libc::c_ulong,
                    -1,
                ))?
            };

            let mut notified = false;

            for (n, e) in self.descriptors.iter_mut().enumerate() {
                if e.revents == 0 {
                    continue;
                }

                if result == 0 {
                    break;
                }

                result -= 1;

                if n == 0 {
                    let _ = guard.shared.parker.recv()?;
                    notified = true;
                    continue;
                }

                // Disable file descriptor and wakeup the task to receive the
                // returned events.
                e.fd = -1;
                let waker = &guard.wakers[n - 1];
                waker
                    .returned_events
                    .store(std::mem::take(&mut e.revents) as usize, Ordering::Release);
                waker.waker.wake();
            }

            if notified {
                let mut holders = guard.shared.holders.lock();
                holders.process(&mut self, &mut guard.wakers)?;
            }
        }

        return Ok(());
    }

    fn start(shared: Arc<Shared>) {
        let state = Driver {
            locations: HashMap::new(),
            descriptors: vec![libc::pollfd {
                fd: shared.parker.fd,
                events: libc::POLLIN,
                revents: 0,
            }],
        };

        let mut guard = PanicGuard {
            shared,
            wakers: vec![],
        };

        if let Err(e) = state.run(&mut guard) {
            panic!("poll thread errored: {}", e)
        }

        mem::forget(guard);
    }
}

/// Wrap a panic guard around self which will release any resources it
/// has allocated when dropped and mark itself as panicked.
struct PanicGuard {
    shared: Arc<Shared>,
    wakers: Vec<Arc<Waker>>,
}

impl Drop for PanicGuard {
    fn drop(&mut self) {
        self.shared.running.store(false, Ordering::Release);

        // Wake up every waker so that they can observe the panic.
        for waker in self.wakers.iter() {
            waker.waker.wake();
        }
    }
}

/// Helper wrapper around an eventfd.
pub(crate) struct EventFd {
    fd: c::c_int,
}

impl EventFd {
    fn new() -> Result<Self> {
        unsafe {
            Ok(Self {
                fd: errno!(c::eventfd(0, c::EFD_NONBLOCK))?,
            })
        }
    }

    /// Add the given number to the eventfd.
    fn send(&self, v: u64) -> Result<(), Errno> {
        unsafe {
            let n = v.to_ne_bytes();
            errno!(c::write(self.fd, n.as_ptr() as *const c::c_void, 8))?;
            Ok(())
        }
    }

    /// Read the next value from the eventfd.
    fn recv(&self) -> Result<u64> {
        unsafe {
            let mut bytes = [0u8; 8];
            let read = errno!(c::read(self.fd, bytes.as_mut_ptr() as *mut c::c_void, 8))?;

            assert!(read == 8);
            Ok(u64::from_ne_bytes(bytes))
        }
    }
}

impl Drop for EventFd {
    fn drop(&mut self) {
        unsafe {
            let _ = libc::close(self.fd);
        }
    }
}