提交 6b0acc44 authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Support thread initializer function for worker threads, can be used for pinning…

Support thread initializer function for worker threads, can be used for pinning worker threads to CPU cores
上级 5799a6f2
......@@ -3,6 +3,7 @@
# Unreleased
* Fix for receiving data over payload channel
* Support thread initializer function for worker threads, can be used for pinning worker threads to CPU cores
# 0.8.5
......
......@@ -171,7 +171,7 @@ pub struct WorkerDtlsFiles {
}
/// Settings for worker to be created with.
#[derive(Debug, Clone)]
#[derive(Clone)]
#[non_exhaustive]
pub struct WorkerSettings {
/// Logging level for logs generated by the media worker thread.
......@@ -187,6 +187,9 @@ pub struct WorkerSettings {
///
/// If `None`, a certificate is dynamically created.
pub dtls_files: Option<WorkerDtlsFiles>,
/// Function that will be called under worker thread before worker starts, can be used for
/// pinning worker threads to CPU cores.
pub thread_initializer: Option<Arc<dyn Fn() + Send + Sync>>,
/// Custom application data.
pub app_data: AppData,
}
......@@ -198,11 +201,37 @@ impl Default for WorkerSettings {
log_tags: Vec::new(),
rtc_ports_range: 10000..=59999,
dtls_files: None,
thread_initializer: None,
app_data: AppData::default(),
}
}
}
impl fmt::Debug for WorkerSettings {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let WorkerSettings {
log_level,
log_tags,
rtc_ports_range,
dtls_files,
thread_initializer,
app_data,
} = self;
f.debug_struct("WorkerSettings")
.field("log_level", &log_level)
.field("log_tags", &log_tags)
.field("rtc_ports_range", &rtc_ports_range)
.field("dtls_files", &dtls_files)
.field(
"thread_initializer",
&thread_initializer.as_ref().map(|_| "ThreadInitializer"),
)
.field("app_data", &app_data)
.finish()
}
}
/// Worker settings that can be updated in runtime.
#[derive(Default, Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
......@@ -271,11 +300,12 @@ impl Inner {
async fn new(
executor: Arc<Executor<'static>>,
WorkerSettings {
app_data,
log_level,
log_tags,
rtc_ports_range,
dtls_files,
thread_initializer,
app_data,
}: WorkerSettings,
worker_manager: WorkerManager,
) -> io::Result<Arc<Self>> {
......@@ -326,7 +356,12 @@ impl Inner {
payload_channel,
status_receiver,
buffer_worker_messages_guard,
} = utils::run_worker_with_channels(id, Arc::clone(&executor), spawn_args);
} = utils::run_worker_with_channels(
id,
thread_initializer,
Arc::clone(&executor),
spawn_args,
);
let handlers = Handlers::default();
......
......@@ -6,7 +6,7 @@ use uuid::Uuid;
struct EventHandlersList<V: Clone + 'static> {
index: usize,
#[allow(clippy::type_complexity)]
callbacks: HashMap<usize, Arc<Box<dyn Fn(V) + Send + Sync>>>,
callbacks: HashMap<usize, Arc<dyn Fn(V) + Send + Sync>>,
}
impl<V: Clone + 'static> Default for EventHandlersList<V> {
......
......@@ -52,6 +52,7 @@ pub(super) struct WorkerRunResult {
pub(super) fn run_worker_with_channels(
id: WorkerId,
thread_initializer: Option<Arc<dyn Fn() + Send + Sync>>,
executor: Arc<Executor<'static>>,
args: Vec<String>,
) -> WorkerRunResult {
......@@ -74,6 +75,9 @@ pub(super) fn run_worker_with_channels(
std::thread::Builder::new()
.name(format!("mediasoup-worker-{}", id))
.spawn(move || {
if let Some(thread_initializer) = thread_initializer {
thread_initializer();
}
let argc = args.len() as c_int;
let args_cstring = args
.into_iter()
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论