Skip to content
Draft
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
3 changes: 3 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[disallowed-types]]
path = "std::thread::ThreadId"
reason = "Use crate::platform::ThreadId for `no_std` compatibility"
1 change: 1 addition & 0 deletions newsfragments/6056.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `PyThread_get_thread_ident` to the ffi crate
3 changes: 2 additions & 1 deletion pyo3-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ pub use self::pyport::*;
pub use self::pystate::*;
pub use self::pystrtod::*;
pub use self::pythonrun::*;
pub use self::pythread::*;
pub use self::pytypedefs::*;
pub use self::rangeobject::*;
pub use self::refcount::*;
Expand Down Expand Up @@ -578,7 +579,7 @@ mod pythonrun;
// skipped pystrhex.h
// skipped pystrcmp.h
mod pystrtod;
// skipped pythread.h
mod pythread;
// skipped pytime.h
mod pytypedefs;
mod rangeobject;
Expand Down
6 changes: 6 additions & 0 deletions pyo3-ffi/src/pythread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use core::ffi::c_ulong;

extern_libpython! {
#[cfg_attr(PyPy, link_name = "PyPyThread_get_thread_ident")]
pub fn PyThread_get_thread_ident() -> c_ulong;
Comment thread
davidhewitt marked this conversation as resolved.
}
10 changes: 4 additions & 6 deletions src/err/err_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

use crate::platform::prelude::*;
use core::cell::UnsafeCell;
use std::{
sync::{Mutex, Once},
thread::ThreadId,
};
use std::sync::{Mutex, Once};

use crate::platform::thread::{self, ThreadId};
#[cfg(not(Py_3_12))]
use crate::sync::MutexExt;
use crate::{
Expand Down Expand Up @@ -98,7 +96,7 @@ impl PyErrState {
// re-entrancy guarantees.
if let Some(thread) = self.normalizing_thread.lock().unwrap().as_ref() {
assert!(
!(*thread == std::thread::current().id()),
!(*thread == thread::current().id()),
"Re-entrant normalization of PyErrState detected"
);
}
Expand All @@ -109,7 +107,7 @@ impl PyErrState {
self.normalizing_thread
.lock()
.unwrap()
.replace(std::thread::current().id());
.replace(thread::current().id());

// Safety: no other thread can access the inner value while we are normalizing it.
let state = unsafe {
Expand Down
3 changes: 2 additions & 1 deletion src/impl_/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#[allow(unused_imports, reason = "conditionally used")]
use crate::platform::prelude::*;
use crate::platform::thread;
use crate::{
exceptions::{PyAttributeError, PyNotImplementedError, PyRuntimeError},
ffi,
Expand All @@ -24,7 +25,7 @@ use core::{
marker::PhantomData,
ptr::{self, NonNull},
};
use std::{sync::Mutex, thread};
use std::sync::Mutex;

mod assertions;
pub mod doc;
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/pyclass/lazy_type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#![allow(clippy::undocumented_unsafe_blocks)]

use crate::platform::prelude::*;
use crate::platform::thread::{self, ThreadId};
use core::{ffi::CStr, marker::PhantomData};
use std::thread::{self, ThreadId};

#[cfg(Py_3_14)]
use crate::err::error_on_minusone;
Expand Down
2 changes: 2 additions & 0 deletions src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ pub use hashbrown::{HashMap, HashSet};
// TODO conditionally import these based on "std" feature
#[cfg(not(feature = "hashbrown"))]
pub use std::collections::{HashMap, HashSet};

pub mod thread;
26 changes: 26 additions & 0 deletions src/platform/thread.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use core::ffi::c_ulong;
use core::num::NonZero;

use pyo3_ffi::PyThread_get_thread_ident;

#[must_use]
pub fn current() -> Thread {
Thread {
// SAFETY: PyThread_get_thread_ident never returns zero
// https://docs.python.org/3/c-api/threads.html#c.PyThread_get_thread_ident
id: ThreadId(unsafe { NonZero::new_unchecked(PyThread_get_thread_ident()) }),
}
}

pub struct Thread {
id: ThreadId,
}

impl Thread {
pub fn id(&self) -> ThreadId {
self.id
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct ThreadId(NonZero<c_ulong>);