Skip to content

Commit 18cb1d4

Browse files
committed
add crossbeam scoped thread
1 parent f2e5d8d commit 18cb1d4

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

dpdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ log = "0.4"
2222
simple_logger = "1"
2323
anyhow = "1.0"
2424
arrayvec = "0.5"
25+
crossbeam = "0.7"
2526

2627
[lib]
2728
name = "dpdk"

dpdk/src/eal.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Wrapper for DPDK's environment abstraction layer (EAL).
22
use crate::ffi;
33
use arrayvec::*;
4+
use crossbeam::thread::{Scope, ScopedJoinHandle};
45
use log::{debug, info, warn};
56
use std::collections::{HashMap, HashSet};
67
use std::convert::{TryFrom, TryInto};
@@ -134,6 +135,31 @@ impl LCoreId {
134135
f()
135136
})
136137
}
138+
139+
/// Launch a thread pined to this core.
140+
/// TODO: change it to crossbeam's `spawn` signature when we start to use crossbeam.
141+
pub fn launch_scoped<'scope, 'env, F, T>(
142+
self,
143+
s: &'scope Scope<'env>,
144+
f: F,
145+
) -> ScopedJoinHandle<'scope, T>
146+
where
147+
F: FnOnce() -> T,
148+
F: Send + 'env,
149+
T: Send + 'env,
150+
{
151+
let lcore_id = self.0;
152+
s.spawn(move |_| {
153+
// Safety: foreign function.
154+
let ret = unsafe {
155+
dpdk_sys::rte_thread_set_affinity(&mut dpdk_sys::rte_lcore_cpuset(lcore_id))
156+
};
157+
if ret < 0 {
158+
warn!("Failed to set affinity on lcore {}", lcore_id);
159+
}
160+
f()
161+
})
162+
}
137163
}
138164

139165
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
@@ -903,8 +929,7 @@ impl Eal {
903929
for lcore_id in &lcore_id_list {
904930
let lcore_id = *lcore_id;
905931
// Safety: foreign function.
906-
let socket_id =
907-
unsafe { dpdk_sys::rte_lcore_to_socket_id(lcore_id.try_into().unwrap()) };
932+
let socket_id = unsafe { dpdk_sys::rte_lcore_to_socket_id(lcore_id) };
908933
// Safety: foreign function.
909934
let cpu_id = unsafe { dpdk_sys::rte_lcore_to_cpu_id(lcore_id.try_into().unwrap()) };
910935
debug!(

0 commit comments

Comments
 (0)