Skip to content

Commit

Permalink
Use mach2 crate for mach_thread_self example
Browse files Browse the repository at this point in the history
The `libc` crate has deprecated its Mach API in favor of this crate.
  • Loading branch information
nvzqz committed Nov 25, 2024
1 parent 4c94258 commit 11a44b8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
3 changes: 3 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ twox-hash = { version = "1.6", optional = true }
[target.'cfg(unix)'.dependencies]
libc = { workspace = true }

[target.'cfg(target_os = "macos")'.dependencies]
mach2 = "0.4"

[target.'cfg(any(windows, target_os = "linux", target_os = "android"))'.dependencies]
winapi = { version = "0.3.9", features = ["processthreadsapi"] }

Expand Down
13 changes: 4 additions & 9 deletions examples/benches/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,22 +268,17 @@ mod thread_id {
#[cfg(target_os = "macos")]
#[divan::bench]
fn mach_thread_self() -> impl Drop {
struct Thread(libc::thread_t);
struct Thread(mach2::mach_types::thread_port_t);

impl Drop for Thread {
fn drop(&mut self) {
extern "C" {
fn mach_port_deallocate(
task: libc::mach_port_t,
name: libc::mach_port_t,
) -> libc::kern_return_t;
unsafe {
mach2::mach_port::mach_port_deallocate(mach2::traps::mach_task_self(), self.0);
}

unsafe { mach_port_deallocate(libc::mach_task_self(), self.0) };
}
}

Thread(unsafe { libc::mach_thread_self() })
Thread(unsafe { mach2::mach_init::mach_thread_self() })
}

// https://man7.org/linux/man-pages/man2/gettid.2.html
Expand Down

0 comments on commit 11a44b8

Please sign in to comment.