pipewire_native_spa/interface/
thread.rs

1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: Copyright (c) 2025 Asymptotic Inc.
3// SPDX-FileCopyrightText: Copyright (c) 2025 Arun Raghavan
4
5use std::{any::Any, pin::Pin};
6
7use crate::dict::Dict;
8
9use super::plugin::Interface;
10
11pub const STACK_SIZE: &str = "thread.stack-size";
12
13pub struct Thread {
14    pub inner: Box<dyn Any>,
15}
16
17pub type ThreadReturn = Box<dyn Any + Send + 'static>;
18
19#[allow(clippy::type_complexity)]
20pub struct ThreadUtilsImpl {
21    pub inner: Pin<Box<dyn Any>>,
22
23    pub create: fn(
24        this: &ThreadUtilsImpl,
25        props: Option<&Dict>,
26        start: Box<dyn FnOnce() -> ThreadReturn + Send + 'static>,
27    ) -> Option<Thread>,
28    pub join: fn(this: &ThreadUtilsImpl, thread: Thread) -> std::io::Result<ThreadReturn>,
29
30    pub get_rt_range:
31        fn(this: &ThreadUtilsImpl, props: Option<&Dict>) -> std::io::Result<(i32, i32)>,
32
33    pub acquire_rt:
34        fn(this: &ThreadUtilsImpl, thread: &Thread, priority: i32) -> std::io::Result<()>,
35    pub drop_rt: fn(this: &ThreadUtilsImpl, thread: &Thread) -> std::io::Result<()>,
36}
37
38impl ThreadUtilsImpl {
39    pub fn create<F>(&self, props: Option<&Dict>, start: F) -> Option<Thread>
40    where
41        F: FnOnce() -> ThreadReturn + Send + 'static,
42    {
43        (self.create)(self, props, Box::new(start))
44    }
45
46    pub fn join(&self, thread: Thread) -> std::io::Result<ThreadReturn> {
47        (self.join)(self, thread)
48    }
49
50    pub fn get_rt_range(&self, props: Option<&Dict>) -> std::io::Result<(i32, i32)> {
51        (self.get_rt_range)(self, props)
52    }
53
54    pub fn acquire_rt(&self, thread: &Thread, priority: i32) -> std::io::Result<()> {
55        (self.acquire_rt)(self, thread, priority)
56    }
57
58    pub fn drop_rt(&self, thread: &Thread) -> std::io::Result<()> {
59        (self.drop_rt)(self, thread)
60    }
61}
62
63impl Interface for ThreadUtilsImpl {
64    unsafe fn make_native(&self) -> *mut super::ffi::CInterface {
65        crate::support::ffi::thread::make_native(self)
66    }
67
68    unsafe fn free_native(thread: *mut super::ffi::CInterface) {
69        crate::support::ffi::thread::free_native(thread)
70    }
71}