Skip to content
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
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 @@ -484,6 +484,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 @@ -577,7 +578,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;
use core::num::NonZero;

extern_libpython! {
pub fn PyThread_get_thread_ident() -> NonZero<c_ulong>;
}
10 changes: 4 additions & 6 deletions src/err/err_state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
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 @@ -94,7 +92,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 @@ -105,7 +103,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
@@ -1,3 +1,4 @@
use crate::platform::thread;
use crate::{
exceptions::{PyAttributeError, PyNotImplementedError, PyRuntimeError},
ffi,
Expand All @@ -19,7 +20,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
@@ -1,5 +1,5 @@
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/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ pub(crate) mod ffi_ptr_ext;
pub(crate) mod py_result_ext;
pub(crate) mod sealed;

mod platform;

/// Old module which contained some implementation details of the `#[pyproto]` module.
///
/// Prefer using the same content from `pyo3::pyclass`, e.g. `use pyo3::pyclass::CompareOp` instead
Expand Down
4 changes: 4 additions & 0 deletions src/platform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! This module is to support platform compatiblity with `no_std` environments.
#![allow(unused_imports)]

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

use pyo3_ffi::PyThread_get_thread_ident;

#[must_use]
pub fn current() -> Thread {
Thread {
id: ThreadId(unsafe { 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>);
Loading