gst-plugin-threadshare 0.15.2

GStreamer Threadshare Plugin
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// Copyright (C) 2018-2020 Sebastian Dröge <sebastian@centricular.com>
// Copyright (C) 2019-2022 François Laignel <fengalin@free.fr>
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at
// <https://mozilla.org/MPL/2.0/>.
//
// SPDX-License-Identifier: MPL-2.0

use futures::future::poll_fn;
use futures::pin_mut;

use std::cell::OnceCell;
use std::future::Future;
use std::panic;
#[cfg(feature = "tuning")]
use std::sync::atomic::AtomicU64;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc as sync_mpsc;
use std::sync::{Arc, Condvar, Mutex, Weak};
use std::task::Poll;
use std::thread;
use std::time::{Duration, Instant};

use waker_fn::waker_fn;

use super::task::{SubTaskOutput, TaskId, TaskQueue};
use super::{CallOnDrop, JoinHandle, Reactor};
use crate::runtime::RUNTIME_CAT;

thread_local! {
    static CURRENT_SCHEDULER: OnceCell<Scheduler> = const { OnceCell::new() };
}

#[derive(Debug)]
pub(super) enum Scheduler {
    Blocking(Blocking),
    Throttling(ThrottlingHandleWeak),
}

impl Scheduler {
    pub fn is_blocking(&self) -> bool {
        matches!(*self, Scheduler::Blocking(..))
    }
}

#[derive(Debug, Default)]
pub(super) struct Blocking {
    tasks_queue: TaskQueue,
}

impl Blocking {
    const MAX_SUCCESSIVE_TASKS: usize = 64;

    pub fn block_on<F>(future: F) -> F::Output
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        CURRENT_SCHEDULER.with(|cur_sched| {
            let cur_sched = cur_sched.get_or_init(|| {
                Reactor::init(Duration::ZERO);
                Scheduler::Blocking(Blocking::default())
            });

            match cur_sched {
                Scheduler::Throttling(hdl_weak) => {
                    let msg = match hdl_weak.upgrade() {
                        Some(hdl) => {
                            format!(
                                "Attempt to block on existing Context {}",
                                hdl.context_name()
                            )
                        }
                        _ => "Attempt to block on terminated Context".to_string(),
                    };
                    gst::error!(RUNTIME_CAT, "{msg}");
                    panic!("{msg}");
                }
                Scheduler::Blocking(sched) => {
                    let (task_id, task) = sched.tasks_queue.add(future);

                    gst::trace!(RUNTIME_CAT, "Blocking on current thread with {task_id:?}");

                    let _guard = CallOnDrop::new(|| {
                        gst::trace!(
                            RUNTIME_CAT,
                            "Done blocking on current thread with {task_id:?}",
                        );
                    });

                    // Blocking on `task` which is cheap to `poll`.
                    let waker = waker_fn(|| ());
                    let cx = &mut std::task::Context::from_waker(&waker);
                    pin_mut!(task);

                    let mut now;
                    let mut tasks_checked;
                    'main: loop {
                        now = Instant::now();
                        Reactor::with_mut(|reactor| reactor.react(now).ok());

                        if let Poll::Ready(t) = task.as_mut().poll(cx) {
                            return t;
                        }

                        tasks_checked = 0;
                        while tasks_checked < Self::MAX_SUCCESSIVE_TASKS {
                            let Ok(runnable) = sched.tasks_queue.pop_runnable() else {
                                continue 'main;
                            };

                            if let Err(err) = panic::catch_unwind(|| runnable.run()) {
                                gst::error!(
                                    RUNTIME_CAT,
                                    "A task panicked blocking on current thread"
                                );

                                panic::resume_unwind(err);
                            }

                            tasks_checked += 1;
                        }
                    }
                }
            }
        })
    }
}

#[derive(Debug, Default)]
pub(super) struct Throttling {
    context_name: Arc<str>,
    max_throttling: Duration,
    task_queue: TaskQueue,
    must_unpark: Mutex<bool>,
    must_unpark_cvar: Condvar,
    #[cfg(feature = "tuning")]
    parked_duration: AtomicU64,
    // this is needed to make sure sources are unregistered when stopping the scheduler
    io_handler: Arc<Mutex<super::reactor::IOHandler>>,
}

impl Throttling {
    const MAX_SUCCESSIVE_TASKS: usize = 64;

    pub fn start(context_name: &str, max_throttling: Duration) -> ThrottlingHandle {
        // Name the thread so that it appears in panic messages.
        let thread = thread::Builder::new().name(context_name.to_string());

        let (handle_sender, handle_receiver) = sync_mpsc::channel();
        let context_name: Arc<str> = Arc::from(context_name);
        let join = thread
            .spawn(move || {
                gst::debug!(
                    RUNTIME_CAT,
                    "Started Scheduler thread for Context {context_name}",
                );

                let handle = CURRENT_SCHEDULER.with(|cur_sched| {
                    Reactor::init(max_throttling);
                    let handle = Reactor::with(|reactor| {
                        ThrottlingHandle::new(Arc::new(Throttling {
                            context_name: context_name.clone(),
                            max_throttling,
                            io_handler: reactor.io_handler.clone(),
                            ..Default::default()
                        }))
                    });

                    cur_sched.set(Scheduler::Throttling(handle.downgrade())).expect("new thread");

                    handle
                });

                let this = Arc::clone(&handle.0.scheduler);
                let must_shutdown = handle.0.must_shutdown.clone();
                let handle_weak = handle.downgrade();
                handle_sender.send(handle).unwrap();

                let shutdown_fut = poll_fn(move |_| {
                    if must_shutdown.load(Ordering::SeqCst) {
                        Poll::Ready(())
                    } else {
                        Poll::Pending
                    }
                });

                let _guard = CallOnDrop::new(|| {
                    gst::trace!(RUNTIME_CAT, "Closing Scheduler for Context {context_name}");

                    Reactor::clear();
                });

                // Blocking on `shutdown_fut` which is cheap to `poll`.
                match this.block_on_priv(shutdown_fut) {
                    Ok(_) => {
                        gst::debug!(
                            RUNTIME_CAT,
                            "Scheduler thread shut down for Context {context_name}",
                        );
                    }
                    Err(e) => {
                        gst::error!(
                            RUNTIME_CAT,
                            "Scheduler thread shut down due to an error within Context {context_name}",
                        );

                        if let Some(handle) = handle_weak.upgrade() {
                            handle.self_shutdown();
                        }

                        panic::resume_unwind(e);
                    }
                }
            })
            .expect("Failed to spawn Scheduler thread");

        let handle = handle_receiver.recv().expect("Context thread init failed");
        handle.set_join_handle(join);

        handle
    }

    // Important: the `termination_future` MUST be cheap to poll.
    //
    // Examples of appropriate `termination_future` are:
    //
    // - an `executor::Task` returned by `self.tasks.add(..)`.
    // - a `JoinHandle` returned by `Handle::spawn`.
    // - a custom future with few cycles (ex. checking an `AtomicBool`).
    fn block_on_priv<F>(&self, termination_future: F) -> std::thread::Result<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        let waker = waker_fn(|| ());
        let cx = &mut std::task::Context::from_waker(&waker);
        pin_mut!(termination_future);

        let mut now;
        // This is to ensure reactor invocation on the first iteration.
        let mut last_react = Instant::now().checked_sub(self.max_throttling).unwrap();
        let mut tasks_checked;
        'main: loop {
            // Only check I/O and timers every `max_throttling`.
            now = Instant::now();
            if now - last_react >= self.max_throttling {
                last_react = now;
                Reactor::with_mut(|reactor| reactor.react(now).ok());
            }

            if let Poll::Ready(t) = termination_future.as_mut().poll(cx) {
                return Ok(t);
            }

            tasks_checked = 0;
            while tasks_checked < Self::MAX_SUCCESSIVE_TASKS {
                match self.task_queue.pop_runnable() {
                    Ok(runnable) => {
                        panic::catch_unwind(|| runnable.run()).inspect_err(|_err| {
                            gst::error!(RUNTIME_CAT, "A task panicked {}", self.context_name);
                        })?;

                        tasks_checked += 1;
                    }
                    _ => {
                        let mut must_unpark = self.must_unpark.lock().unwrap();
                        loop {
                            if *must_unpark {
                                *must_unpark = false;
                                continue 'main;
                            }

                            if let Some(parking_duration) =
                                self.max_throttling.checked_sub(last_react.elapsed())
                            {
                                #[cfg(feature = "tuning")]
                                self.parked_duration.fetch_add(
                                    parking_duration.subsec_nanos() as u64,
                                    Ordering::Relaxed,
                                );

                                let result = self
                                    .must_unpark_cvar
                                    .wait_timeout(must_unpark, parking_duration)
                                    .unwrap();

                                must_unpark = result.0;
                            } else {
                                *must_unpark = false;
                                continue 'main;
                            }
                        }
                    }
                }
            }
        }
    }

    fn unpark(&self) {
        let mut must_unpark = self.must_unpark.lock().unwrap();
        *must_unpark = true;
        self.must_unpark_cvar.notify_one();
    }

    pub fn is_throttling_thread() -> bool {
        CURRENT_SCHEDULER.with(|cur_scheduler| {
            let Some(sched) = cur_scheduler.get() else {
                return false;
            };
            !sched.is_blocking()
        })
    }

    pub fn current() -> Option<ThrottlingHandle> {
        CURRENT_SCHEDULER.with(|cur_scheduler| match cur_scheduler.get()? {
            Scheduler::Blocking(_) => None,
            Scheduler::Throttling(hdl_weak) => hdl_weak.upgrade(),
        })
    }

    fn is_current(&self) -> bool {
        CURRENT_SCHEDULER.with(|cur_sched| match cur_sched.get() {
            None | Some(Scheduler::Blocking(_)) => false,
            Some(Scheduler::Throttling(hdl_weak)) => match hdl_weak.upgrade() {
                Some(hdl) => std::ptr::eq(self, Arc::as_ptr(&hdl.0.scheduler)),
                _ => false,
            },
        })
    }
}

#[derive(Debug)]
struct ThrottlingHandleInner {
    scheduler: Arc<Throttling>,
    must_shutdown: Arc<AtomicBool>,
    join: Mutex<Option<thread::JoinHandle<()>>>,
}

impl ThrottlingHandleInner {
    fn new(scheduler: Arc<Throttling>) -> Self {
        ThrottlingHandleInner {
            scheduler,
            must_shutdown: Default::default(),
            join: Default::default(),
        }
    }

    #[track_caller]
    fn context_name(&self) -> &str {
        self.scheduler.context_name.as_ref()
    }

    fn remove_io(
        &self,
        source: &Arc<super::reactor::Source>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        self.scheduler
            .io_handler
            .lock()
            // this is called by drop implementations => make sure not to panic
            .map_err(|_err| std::sync::PoisonError::new(()))?
            .remove_io(source)?;

        Ok(())
    }
}

impl Drop for ThrottlingHandleInner {
    fn drop(&mut self) {
        if !self.must_shutdown.fetch_or(true, Ordering::SeqCst) {
            // Was not already shutting down.
            self.scheduler.unpark();

            gst::trace!(
                RUNTIME_CAT,
                "Shutting down Scheduler thread for Context {}",
                self.context_name(),
            );

            // Don't block shutting down itself
            if !self.scheduler.is_current()
                && let Some(join_handler) = self.join.lock().unwrap().take()
            {
                gst::trace!(
                    RUNTIME_CAT,
                    "Waiting for Scheduler thread to shutdown for Context {}",
                    self.scheduler.context_name
                );

                let _ = join_handler.join();
            }
        }
    }
}

#[derive(Clone, Debug)]
pub(super) struct ThrottlingHandleWeak(Weak<ThrottlingHandleInner>);

impl ThrottlingHandleWeak {
    pub(super) fn upgrade(&self) -> Option<ThrottlingHandle> {
        self.0.upgrade().map(ThrottlingHandle)
    }
}

#[derive(Clone, Debug)]
pub(super) struct ThrottlingHandle(Arc<ThrottlingHandleInner>);

impl ThrottlingHandle {
    fn new(scheduler: Arc<Throttling>) -> Self {
        ThrottlingHandle(Arc::new(ThrottlingHandleInner::new(scheduler)))
    }

    fn set_join_handle(&self, join: thread::JoinHandle<()>) {
        *self.0.join.lock().unwrap() = Some(join);
    }

    fn self_shutdown(self) {
        self.0.must_shutdown.store(true, Ordering::SeqCst);
        *self.0.join.lock().unwrap() = None;
    }

    pub fn context_name(&self) -> &str {
        self.0.context_name()
    }

    pub fn max_throttling(&self) -> Duration {
        self.0.scheduler.max_throttling
    }

    #[cfg(feature = "tuning")]
    pub fn parked_duration(&self) -> Duration {
        Duration::from_nanos(self.0.scheduler.parked_duration.load(Ordering::Relaxed))
    }

    /// Executes the provided function relatively to this [`Scheduler`]'s [`Reactor`].
    ///
    /// Useful to initialize i/o sources and timers from outside
    /// of a [`Scheduler`].
    ///
    /// # Panic
    ///
    /// This will block current thread and would panic if run
    /// from the [`Scheduler`].
    pub fn enter<'a, F, O>(&'a self, f: F) -> O
    where
        F: FnOnce() -> O + Send + 'a,
        O: Send + 'a,
    {
        assert!(
            !self.0.scheduler.is_current(),
            "Attempting to `enter()` current `Context` from itself"
        );

        // Safety: bounding `self` to `'a` and blocking on the task
        // ensures that the lifetime bounds satisfy the safety
        // requirements for `TaskQueue::add_sync`.
        let task = unsafe { self.0.scheduler.task_queue.add_sync(f) };
        self.0.scheduler.unpark();
        futures::executor::block_on(task)
    }

    pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        let (task_id, task) = self.0.scheduler.task_queue.add(future);
        JoinHandle::new(task_id, task, self)
    }

    pub fn spawn_and_unpark<F>(&self, future: F) -> JoinHandle<F::Output>
    where
        F: Future + Send + 'static,
        F::Output: Send + 'static,
    {
        let (task_id, task) = self.0.scheduler.task_queue.add(future);
        self.0.scheduler.unpark();
        JoinHandle::new(task_id, task, self)
    }

    pub(super) fn unpark(&self) {
        self.0.scheduler.unpark();
    }

    pub fn add_sub_task<T>(&self, task_id: TaskId, sub_task: T) -> Result<(), T>
    where
        T: Future<Output = SubTaskOutput> + Send + 'static,
    {
        self.0.scheduler.task_queue.add_sub_task(task_id, sub_task)
    }

    pub fn downgrade(&self) -> ThrottlingHandleWeak {
        ThrottlingHandleWeak(Arc::downgrade(&self.0))
    }

    pub async fn drain_sub_tasks(&self, task_id: TaskId) -> SubTaskOutput {
        let sub_tasks_fut = self.0.scheduler.task_queue.drain_sub_tasks(task_id);
        sub_tasks_fut.await
    }

    /// Synchronously removes the I/O source from the reactor.
    pub(super) fn remove_io(
        &self,
        source: &Arc<super::reactor::Source>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        self.0.remove_io(source)
    }
}

impl PartialEq for ThrottlingHandle {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;
    use std::time::Duration;

    use super::{Blocking, Reactor, Throttling, ThrottlingHandle};
    use crate::runtime::timer;

    #[test]
    fn block_on_task_join_handle() {
        use futures::channel::oneshot;
        use std::sync::mpsc;

        let (join_sender, join_receiver) = mpsc::channel();
        let (shutdown_sender, shutdown_receiver) = oneshot::channel();

        std::thread::spawn(move || {
            let handle = ThrottlingHandle::new(Arc::new(Throttling {
                context_name: "block_on_task_join_handle".into(),
                max_throttling: Duration::from_millis(2),
                ..Default::default()
            }));

            Reactor::init(Duration::from_millis(2));

            let join_handle = handle.spawn(async {
                timer::delay_for(Duration::from_millis(5)).await;
                42
            });

            let _ = join_sender.send(join_handle);
            let _ = handle.0.scheduler.block_on_priv(shutdown_receiver);
        });

        let task_join_handle = join_receiver.recv().unwrap();
        let res = Blocking::block_on(task_join_handle).unwrap();

        let _ = shutdown_sender.send(());

        assert_eq!(res, 42);
    }

    #[test]
    fn block_on_timer() {
        let res = Blocking::block_on(async {
            timer::delay_for(Duration::from_millis(5)).await;
            42
        });

        assert_eq!(res, 42);
    }

    #[test]
    fn enter_non_static() {
        let handle = Throttling::start("enter_non_static", Duration::from_millis(2));

        let mut flag = false;
        handle.enter(|| flag = true);
        assert!(flag);
    }
}