Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make set_for_current function unsafe #346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bastion-executor/benches/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn stress_stats<S: SmpStats + Sync + Send>(stats: &'static S) {
let mut handles = Vec::with_capacity(*core_count());
for core in get_cores() {
let handle = thread::spawn(move || {
placement::set_for_current(*core);
unsafe { placement::set_for_current(*core) };
for i in 0..100 {
stats.store_load(core.id, 10);
if i % 3 == 0 {
Expand Down
22 changes: 11 additions & 11 deletions src/bastion-executor/src/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn get_num_cores() -> Option<usize> {
}
///
/// Sets the current threads affinity
pub fn set_for_current(core_id: CoreId) {
pub unsafe fn set_for_current(core_id: CoreId) {
tracing::trace!("Executor: placement: set affinity on core {}", core_id.id);
set_for_current_helper(core_id);
}
Expand All @@ -41,7 +41,7 @@ fn get_core_ids_helper() -> Option<Vec<CoreId>> {
#[cfg(target_os = "linux")]
#[inline]
fn set_for_current_helper(core_id: CoreId) {
linux::set_for_current(core_id);
unsafe { linux::set_for_current(core_id) };
}

#[cfg(target_os = "linux")]
Expand All @@ -68,7 +68,7 @@ mod linux {
}
}

pub fn set_for_current(core_id: CoreId) {
pub unsafe fn set_for_current(core_id: CoreId) {
// Turn `core_id` into a `libc::cpu_set_t` with only
// one core active.
let mut set = new_cpu_set();
Expand Down Expand Up @@ -141,7 +141,7 @@ mod linux {

assert!(!ids.is_empty());

set_for_current(ids[0]);
unsafe { set_for_current(ids[0]) };

// Ensure that the system pinned the current thread
// to the specified core.
Expand Down Expand Up @@ -177,7 +177,7 @@ fn get_core_ids_helper() -> Option<Vec<CoreId>> {
#[cfg(target_os = "windows")]
#[inline]
fn set_for_current_helper(core_id: CoreId) {
windows::set_for_current(core_id);
unsafe { windows::set_for_current(core_id);
}

#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -211,7 +211,7 @@ mod windows {
}
}

pub fn set_for_current(core_id: CoreId) {
pub unsafe fn set_for_current(core_id: CoreId) {
// Convert `CoreId` back into mask.
let mask: DWORD_PTR = 1 << core_id.id;

Expand Down Expand Up @@ -267,7 +267,7 @@ mod windows {

assert!(ids.len() > 0);

set_for_current(ids[0]);
unsafe { set_for_current(ids[0]) };
}
}
}
Expand All @@ -283,7 +283,7 @@ fn get_core_ids_helper() -> Option<Vec<CoreId>> {
#[cfg(target_os = "macos")]
#[inline]
fn set_for_current_helper(core_id: CoreId) {
macos::set_for_current(core_id);
unsafe { macos::set_for_current(core_id) };
}

#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -328,7 +328,7 @@ mod macos {
)
}

pub fn set_for_current(core_id: CoreId) {
pub unsafe fn set_for_current(core_id: CoreId) {
let thread_affinity_policy_count: MachMsgTypeNumberT =
mem::size_of::<ThreadAffinityPolicyDataT>() as MachMsgTypeNumberT
/ mem::size_of::<IntegerT>() as MachMsgTypeNumberT;
Expand Down Expand Up @@ -370,7 +370,7 @@ mod macos {

assert!(ids.len() > 0);

set_for_current(ids[0]);
unsafe { set_for_current(ids[0]) };
}
}
}
Expand Down Expand Up @@ -410,6 +410,6 @@ mod tests {

assert!(!ids.is_empty());

set_for_current(ids[0]);
unsafe { set_for_current(ids[0]) };
}
}
2 changes: 1 addition & 1 deletion src/bastion-executor/src/thread_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl DynamicPoolManager {
fn affinity_pinner() {
if 1 != *load_balancer::core_count() {
let mut core = ROUND_ROBIN_PIN.lock().unwrap();
placement::set_for_current(*core);
unsafe { placement::set_for_current(*core) };
core.id = (core.id + 1) % *load_balancer::core_count();
}
}
Expand Down