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