diff --git a/compio-py-dynamic-openssl/src/ssl.rs b/compio-py-dynamic-openssl/src/ssl.rs index 14539b1..be0d7ff 100644 --- a/compio-py-dynamic-openssl/src/ssl.rs +++ b/compio-py-dynamic-openssl/src/ssl.rs @@ -398,12 +398,12 @@ mod error { pub struct ErrorCode(c_int); impl ErrorCode { - pub const ZERO_RETURN: ErrorCode = ErrorCode(ffi::SSL_ERROR_ZERO_RETURN); - pub const WANT_READ: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_READ); - pub const WANT_WRITE: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_WRITE); - pub const SYSCALL: ErrorCode = ErrorCode(ffi::SSL_ERROR_SYSCALL); pub const SSL: ErrorCode = ErrorCode(ffi::SSL_ERROR_SSL); + pub const SYSCALL: ErrorCode = ErrorCode(ffi::SSL_ERROR_SYSCALL); pub const WANT_CLIENT_HELLO_CB: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_CLIENT_HELLO_CB); + pub const WANT_READ: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_READ); + pub const WANT_WRITE: ErrorCode = ErrorCode(ffi::SSL_ERROR_WANT_WRITE); + pub const ZERO_RETURN: ErrorCode = ErrorCode(ffi::SSL_ERROR_ZERO_RETURN); pub fn from_raw(raw: c_int) -> ErrorCode { ErrorCode(raw) diff --git a/compio-py/src/event_loop.rs b/compio-py/src/event_loop.rs index aba5438..2d06475 100644 --- a/compio-py/src/event_loop.rs +++ b/compio-py/src/event_loop.rs @@ -13,8 +13,7 @@ use once_cell::sync::OnceCell; use pyo3::{ IntoPyObjectExt, buffer::PyBuffer, - exceptions::PyRuntimeError, - exceptions::PyValueError, + exceptions::{PyRuntimeError, PyValueError}, ffi::c_str, prelude::*, types::{PyDict, PyMapping, PyTuple, PyWeakrefReference}, @@ -22,10 +21,9 @@ use pyo3::{ use crate::{ handle::{Handle, TimerHandle}, - import, + import, net, owned::{self, OwnedRefCell}, runtime::{self, Runtime}, - socket, }; static COMPIO_FUTURE: OnceCell> = OnceCell::new(); @@ -268,7 +266,7 @@ impl CompioLoop { }; let pyloop = slf.clone().unbind(); slf.borrow() - .spawn_py(py, socket::PySocket::new(pyloop, domain, ty, protocol)) + .spawn_py(py, net::PySocket::new(pyloop, domain, ty, protocol)) } } @@ -284,8 +282,9 @@ impl CompioLoop { } } - /// Spawn a Rust Future onto the event loop, returning a connected Python Future. - /// When the Python Future is cancelled, the Rust Future is also cancelled. + /// Spawn a Rust Future onto the event loop, returning a connected Python + /// Future. When the Python Future is cancelled, the Rust Future is also + /// cancelled. pub fn spawn_py<'py, F>(&self, py: Python<'py>, fut: F) -> PyResult> where F: Future>> + 'static, diff --git a/compio-py/src/import.rs b/compio-py/src/import.rs index d26ee26..771aa0f 100644 --- a/compio-py/src/import.rs +++ b/compio-py/src/import.rs @@ -3,20 +3,21 @@ //! Python module import utilities with caching. //! -//! This module provides macros and functions for efficiently importing Python modules -//! and accessing their attributes with automatic caching using `OnceCell`. +//! This module provides macros and functions for efficiently importing Python +//! modules and accessing their attributes with automatic caching using +//! `OnceCell`. //! //! # Thread Safety //! -//! All cached imports and attributes use `OnceCell::sync`, making them safe to access -//! from multiple threads. The first thread to access will perform the import/getattr, -//! and subsequent accesses will use the cached value. +//! All cached imports and attributes use `OnceCell::sync`, making them safe to +//! access from multiple threads. The first thread to access will perform the +//! import/getattr, and subsequent accesses will use the cached value. //! //! # Performance //! -//! By caching module imports and attribute lookups, this module avoids the overhead -//! of repeated Python import statements and attribute resolution, which can be -//! significant in performance-critical code paths. +//! By caching module imports and attribute lookups, this module avoids the +//! overhead of repeated Python import statements and attribute resolution, +//! which can be significant in performance-critical code paths. //! //! # Example //! @@ -34,8 +35,9 @@ use pyo3::prelude::*; /// Generates a function that imports a Python module with caching. /// -/// This macro creates a public function that imports the specified Python module -/// on first call and caches it for subsequent calls using a static `OnceCell`. +/// This macro creates a public function that imports the specified Python +/// module on first call and caches it for subsequent calls using a static +/// `OnceCell`. /// /// # Generated Function Signature /// @@ -45,7 +47,8 @@ use pyo3::prelude::*; /// /// # Arguments /// -/// * `$module_name` - The name of the Python module to import (as an identifier) +/// * `$module_name` - The name of the Python module to import (as an +/// identifier) /// /// # Example /// @@ -60,8 +63,8 @@ use pyo3::prelude::*; /// /// # Thread Safety /// -/// The generated function is thread-safe. Multiple threads can call it concurrently, -/// and only the first call will perform the actual import. +/// The generated function is thread-safe. Multiple threads can call it +/// concurrently, and only the first call will perform the actual import. macro_rules! import { ($module_name:ident) => { pub fn $module_name<'py>(py: Python<'py>) -> PyResult<&'py Bound<'py, PyModule>> { @@ -105,7 +108,8 @@ macro_rules! import { /// /// # Thread Safety /// -/// Like `import!`, this macro generates thread-safe code using static `OnceCell`. +/// Like `import!`, this macro generates thread-safe code using static +/// `OnceCell`. /// /// # Note /// @@ -121,8 +125,9 @@ macro_rules! getattr { /// Creates a module wrapper with standard imports and boilerplate. /// -/// This macro combines the `import!` macro with a module definition that includes -/// the standard `use` statements needed for working with Python objects. +/// This macro combines the `import!` macro with a module definition that +/// includes the standard `use` statements needed for working with Python +/// objects. /// /// # Arguments /// diff --git a/compio-py/src/lib.rs b/compio-py/src/lib.rs index ffc72e9..fd29ef1 100644 --- a/compio-py/src/lib.rs +++ b/compio-py/src/lib.rs @@ -9,11 +9,10 @@ use pyo3::{buffer::PyBuffer, prelude::*}; mod event_loop; mod handle; mod import; +mod net; mod owned; mod runtime; mod send_wrapper; -mod socket; -mod ssl; mod thread; /// A Python module implemented in Rust. The name of this module must match @@ -38,8 +37,7 @@ fn _core(m: &Bound<'_, PyModule>) -> PyResult<()> { event_loop::register(m)?; handle::register(m)?; - socket::register(m)?; - ssl::register(m)?; + net::register(m)?; Ok(()) } diff --git a/compio-py/src/net/mod.rs b/compio-py/src/net/mod.rs new file mode 100644 index 0000000..fae8eb0 --- /dev/null +++ b/compio-py/src/net/mod.rs @@ -0,0 +1,209 @@ +// SPDX-License-Identifier: Apache-2.0 OR MulanPSL-2.0 +// Copyright 2026 Fantix King + +use std::{io, net::Shutdown}; + +use compio::{ + buf::{BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut, buf_try}, + driver::{ + ToSharedFd, impl_raw_fd, + op::{self, BufResultExt}, + }, + io::{AsyncRead, AsyncWrite}, +}; +use pyo3::{ + exceptions::PyTypeError, + prelude::*, + types::{PyByteArray, PyBytes, PyList, PyTuple}, +}; +use socket2::{Domain, Protocol, SockAddr, Socket as Socket2, Type}; + +pub use self::socket::PySocket; +use crate::{ + import, + runtime::{self, Attacher}, +}; + +mod socket; +mod ssl; + +#[derive(Debug, Clone)] +pub struct SocketStream { + inner: Socket, +} + +impl AsyncRead for &SocketStream { + #[inline] + async fn read(&mut self, buf: B) -> BufResult { + self.inner.recv(buf, 0).await + } +} + +impl AsyncWrite for &SocketStream { + async fn write(&mut self, buf: T) -> BufResult { + self.inner.send(buf, 0).await + } + + async fn flush(&mut self) -> io::Result<()> { + Ok(()) + } + + async fn shutdown(&mut self) -> io::Result<()> { + self.inner.shutdown(Shutdown::Write).await + } +} + +impl AsyncRead for SocketStream { + #[inline] + async fn read(&mut self, buf: B) -> BufResult { + (&*self).read(buf).await + } + + #[inline] + async fn read_vectored(&mut self, buf: V) -> BufResult { + (&*self).read_vectored(buf).await + } +} + +impl AsyncWrite for SocketStream { + async fn write(&mut self, buf: T) -> BufResult { + (&*self).write(buf).await + } + + async fn flush(&mut self) -> io::Result<()> { + (&*self).flush().await + } + + async fn shutdown(&mut self) -> io::Result<()> { + (&*self).shutdown().await + } +} + +impl_raw_fd!(SocketStream, socket2::Socket, inner, socket); + +async fn name_to_ip(name: Vec, family: impl Into) -> PyResult { + let family = family.into(); + runtime::asyncify(move || { + Python::attach(|py| { + let args = (name, py.None(), family); + let result_list: Bound = + import::socket::getaddrinfo(py, args, None)?.cast_into()?; + let result: Bound = result_list.get_item(0)?.cast_into()?; + let addr: Bound = result.get_item(result.len() - 1)?.cast_into()?; + addr.get_item(0)?.extract() + }) + }) + .await +} + +fn idna_converter(obj: &Bound, f: F) -> PyResult +where + F: FnOnce(&[u8]) -> PyResult, +{ + if let Ok(bytes) = obj.cast::() { + f(bytes.as_bytes()) + } else if let Ok(bytes) = obj.cast::() { + f(unsafe { bytes.as_bytes() }) + } else if let Ok(str) = obj.extract::<&str>() { + if str.is_ascii() { + f(str.as_bytes()) + } else { + f(obj + .call_method1("encode", ("idna",))? + .cast::()? + .as_bytes()) + } + } else { + Err(PyTypeError::new_err(format!( + "str, bytes or bytearray expected, not {}", + obj.get_type() + ))) + } +} + +pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> { + socket::register(m)?; + ssl::register(m)?; + Ok(()) +} + +// Socket is copied and modified from compio-net: +// https://github.com/compio-rs/compio/blob/8cf1b4db78c93f37d462b85bd1f445caa7fca4d5/compio-net/src/socket.rs +// Copyright (c) 2023 Berrysoft + +#[derive(Debug, Clone)] +struct Socket { + socket: Attacher, +} + +impl Socket { + fn from_socket2(socket: Socket2) -> io::Result { + let socket = Attacher::new(socket)?; + Ok(Self { socket }) + } + + async fn new(domain: Domain, ty: Type, protocol: Option) -> io::Result { + Self::from_socket2({ + #[cfg(windows)] + { + runtime::asyncify(move || Socket2::new(domain, ty, protocol)).await? + } + #[cfg(unix)] + { + use compio::driver::op::CreateSocket; + + let op = CreateSocket::new( + domain.into(), + ty.into(), + protocol.map(|p| p.into()).unwrap_or_default(), + ); + let (_, op) = buf_try!(@try runtime::execute(op).await); + op.into_inner() + } + }) + } + + async fn connect_async(&self, addr: &SockAddr) -> io::Result<()> { + let op = op::Connect::new(self.to_shared_fd(), addr.clone()); + let (_, _op) = buf_try!(@try runtime::execute(op).await); + #[cfg(windows)] + _op.update_context()?; + Ok(()) + } + + async fn recv(&self, buffer: B, flags: i32) -> BufResult { + let fd = self.to_shared_fd(); + let op = op::Recv::new(fd, buffer, flags); + let res = runtime::execute(op).await.into_inner(); + unsafe { res.map_advanced() } + } + + async fn send(&self, buffer: T, flags: i32) -> BufResult { + let fd = self.to_shared_fd(); + let op = op::Send::new(fd, buffer, flags); + runtime::execute(op).await.into_inner() + } + + async fn shutdown(&self, how: Shutdown) -> io::Result<()> { + #[cfg(unix)] + { + let fd = self.to_shared_fd(); + let op = op::ShutdownSocket::new(fd, how); + runtime::execute(op).await.0?; + Ok(()) + } + #[cfg(windows)] + self.socket.shutdown(how) + } + + async fn close(self) -> io::Result<()> { + let fd = self.socket.into_inner().take().await; + if let Some(fd) = fd { + let op = op::CloseSocket::new(fd.into()); + runtime::execute(op).await.0?; + } + Ok(()) + } +} + +impl_raw_fd!(Socket, Socket2, socket, socket); diff --git a/compio-py/src/socket.rs b/compio-py/src/net/socket.rs similarity index 69% rename from compio-py/src/socket.rs rename to compio-py/src/net/socket.rs index 2adf77b..14776e5 100644 --- a/compio-py/src/socket.rs +++ b/compio-py/src/net/socket.rs @@ -2,18 +2,13 @@ // Copyright 2026 Fantix King use std::{ - io, net::{Ipv4Addr, Shutdown, SocketAddr, SocketAddrV4}, sync::Arc, }; use compio::{ - buf::{BufResult, IntoInner, IoBuf, IoBufMut, IoVectoredBufMut, buf_try}, - driver::{ - AsRawFd, ToSharedFd, impl_raw_fd, - op::{self, BufResultExt}, - }, - io::{AsyncRead, AsyncWrite}, + buf::buf_try, + driver::AsRawFd, tls::{ TlsAcceptor, TlsConnector, py_dynamic_openssl::{self, SSLContext}, @@ -24,17 +19,15 @@ use pyo3::{ IntoPyObjectExt, exceptions::{PyOSError, PyTypeError, PyValueError}, prelude::*, - types::{PyByteArray, PyBytes, PyList, PyTuple}, + types::{PyBytes, PyList}, }; -use socket2::{Domain, Protocol, SockAddr, Socket as Socket2, Type}; +use socket2::{Domain, Protocol, Type}; -use crate::{ - Either, - event_loop::CompioLoop, - extract_py_err, import, py_any_to_buffer, - runtime::{self, Attacher}, +use super::{ + Socket, SocketStream, idna_converter, name_to_ip, ssl::{RustlsContext, SSLImpl, SSLSocket, SSLSocketMetadata}, }; +use crate::{Either, event_loop::CompioLoop, extract_py_err, import, py_any_to_buffer}; #[pyclass(unsendable, name = "Socket")] pub struct PySocket { @@ -326,182 +319,7 @@ impl PySocket { } } -#[derive(Debug, Clone)] -pub struct SocketStream { - inner: Socket, -} - -impl AsyncRead for &SocketStream { - #[inline] - async fn read(&mut self, buf: B) -> BufResult { - self.inner.recv(buf, 0).await - } -} - -impl AsyncWrite for &SocketStream { - async fn write(&mut self, buf: T) -> BufResult { - self.inner.send(buf, 0).await - } - - async fn flush(&mut self) -> io::Result<()> { - Ok(()) - } - - async fn shutdown(&mut self) -> io::Result<()> { - self.inner.shutdown(Shutdown::Write).await - } -} - -impl AsyncRead for SocketStream { - #[inline] - async fn read(&mut self, buf: B) -> BufResult { - (&*self).read(buf).await - } - - #[inline] - async fn read_vectored(&mut self, buf: V) -> BufResult { - (&*self).read_vectored(buf).await - } -} - -impl AsyncWrite for SocketStream { - async fn write(&mut self, buf: T) -> BufResult { - (&*self).write(buf).await - } - - async fn flush(&mut self) -> io::Result<()> { - (&*self).flush().await - } - - async fn shutdown(&mut self) -> io::Result<()> { - (&*self).shutdown().await - } -} - -impl_raw_fd!(SocketStream, socket2::Socket, inner, socket); - -async fn name_to_ip(name: Vec, family: impl Into) -> PyResult { - let family = family.into(); - runtime::asyncify(move || { - Python::attach(|py| { - let args = (name, py.None(), family); - let result_list: Bound = - import::socket::getaddrinfo(py, args, None)?.cast_into()?; - let result: Bound = result_list.get_item(0)?.cast_into()?; - let addr: Bound = result.get_item(result.len() - 1)?.cast_into()?; - addr.get_item(0)?.extract() - }) - }) - .await -} - -fn idna_converter(obj: &Bound, f: F) -> PyResult -where - F: FnOnce(&[u8]) -> PyResult, -{ - if let Ok(bytes) = obj.cast::() { - f(bytes.as_bytes()) - } else if let Ok(bytes) = obj.cast::() { - f(unsafe { bytes.as_bytes() }) - } else if let Ok(str) = obj.extract::<&str>() { - if str.is_ascii() { - f(str.as_bytes()) - } else { - f(obj - .call_method1("encode", ("idna",))? - .cast::()? - .as_bytes()) - } - } else { - Err(PyTypeError::new_err(format!( - "str, bytes or bytearray expected, not {}", - obj.get_type() - ))) - } -} - pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; Ok(()) } - -// Socket is copied and modified from compio-net: -// https://github.com/compio-rs/compio/blob/8cf1b4db78c93f37d462b85bd1f445caa7fca4d5/compio-net/src/socket.rs -// Copyright (c) 2023 Berrysoft - -#[derive(Debug, Clone)] -struct Socket { - socket: Attacher, -} - -impl Socket { - fn from_socket2(socket: Socket2) -> io::Result { - let socket = Attacher::new(socket)?; - Ok(Self { socket }) - } - - async fn new(domain: Domain, ty: Type, protocol: Option) -> io::Result { - Self::from_socket2({ - #[cfg(windows)] - { - runtime::asyncify(move || Socket2::new(domain, ty, protocol)).await? - } - #[cfg(unix)] - { - use compio::driver::op::CreateSocket; - - let op = CreateSocket::new( - domain.into(), - ty.into(), - protocol.map(|p| p.into()).unwrap_or_default(), - ); - let (_, op) = buf_try!(@try runtime::execute(op).await); - op.into_inner() - } - }) - } - - async fn connect_async(&self, addr: &SockAddr) -> io::Result<()> { - let op = op::Connect::new(self.to_shared_fd(), addr.clone()); - let (_, _op) = buf_try!(@try runtime::execute(op).await); - #[cfg(windows)] - _op.update_context()?; - Ok(()) - } - - async fn recv(&self, buffer: B, flags: i32) -> BufResult { - let fd = self.to_shared_fd(); - let op = op::Recv::new(fd, buffer, flags); - let res = runtime::execute(op).await.into_inner(); - unsafe { res.map_advanced() } - } - - async fn send(&self, buffer: T, flags: i32) -> BufResult { - let fd = self.to_shared_fd(); - let op = op::Send::new(fd, buffer, flags); - runtime::execute(op).await.into_inner() - } - - async fn shutdown(&self, how: Shutdown) -> io::Result<()> { - #[cfg(unix)] - { - let fd = self.to_shared_fd(); - let op = op::ShutdownSocket::new(fd, how); - runtime::execute(op).await.0?; - Ok(()) - } - #[cfg(windows)] - self.socket.shutdown(how) - } - - async fn close(self) -> io::Result<()> { - let fd = self.socket.into_inner().take().await; - if let Some(fd) = fd { - let op = op::CloseSocket::new(fd.into()); - runtime::execute(op).await.0?; - } - Ok(()) - } -} - -impl_raw_fd!(Socket, Socket2, socket, socket); diff --git a/compio-py/src/ssl.rs b/compio-py/src/net/ssl.rs similarity index 99% rename from compio-py/src/ssl.rs rename to compio-py/src/net/ssl.rs index f9424a3..31e51ff 100644 --- a/compio-py/src/ssl.rs +++ b/compio-py/src/net/ssl.rs @@ -42,7 +42,7 @@ use webpki::{ UnknownStatusPolicy, }; -use crate::{Either, event_loop::CompioLoop, import, py_any_to_buffer, socket::SocketStream}; +use crate::{Either, event_loop::CompioLoop, import, net::SocketStream, py_any_to_buffer}; #[derive(Debug, Clone, Copy, Default)] pub enum SSLImpl { diff --git a/compio-py/src/owned.rs b/compio-py/src/owned.rs index c55f23a..655f81f 100644 --- a/compio-py/src/owned.rs +++ b/compio-py/src/owned.rs @@ -3,10 +3,10 @@ //! Thread-safe cell with single-owner access control and reference counting. //! -//! This module provides `OwnedRefCell`, which combines lazy initialization with -//! exclusive ownership semantics. At any given time, at most one thread "owns" the -//! cell and can access its contents. The owning thread can hold multiple references -//! simultaneously through reference counting. +//! This module provides `OwnedRefCell`, which combines lazy initialization +//! with exclusive ownership semantics. At any given time, at most one thread +//! "owns" the cell and can access its contents. The owning thread can hold +//! multiple references simultaneously through reference counting. //! //! This module is inspired by: //! * `std::cell::RefCell` for interior mutability and reference counting @@ -14,8 +14,6 @@ //! * https://github.com/compio-rs/compio/blob/master/compio-runtime/src/runtime/send_wrapper.rs //! * https://github.com/thk1/send_wrapper -use pyo3::PyErr; -use pyo3::exceptions::PyRuntimeError; use std::{ cell::{Cell, UnsafeCell}, marker, mem, @@ -23,6 +21,8 @@ use std::{ sync::atomic::{AtomicU32, Ordering}, }; +use pyo3::{PyErr, exceptions::PyRuntimeError}; + /// Sentinel value indicating that no thread currently owns the cell. const NO_OWNER: u32 = 0; @@ -55,20 +55,24 @@ type OwnedResult = Result; /// A thread-safe cell with single-owner access control. /// -/// `OwnedRefCell` provides synchronized access to an optional value with the following -/// guarantees: +/// `OwnedRefCell` provides synchronized access to an optional value with the +/// following guarantees: /// /// - **Single ownership**: At most one thread can own the cell at any time -/// - **Reference counting**: The owning thread can create multiple `Ref` instances -/// - **Reentrant control**: Callers can specify whether reentrant access is allowed -/// - **Lazy initialization**: The value can be initialized exactly once after creation +/// - **Reference counting**: The owning thread can create multiple `Ref` +/// instances +/// - **Reentrant control**: Callers can specify whether reentrant access is +/// allowed +/// - **Lazy initialization**: The value can be initialized exactly once after +/// creation /// /// # Thread Safety /// -/// The cell is `Sync` when `T: Send`. The `owner` atomic ensures exclusive ownership, -/// while the `borrow` counter tracks active references from the owning thread. Since -/// only the owning thread can access `borrow` (protected by the `owner` lock), using -/// a non-atomic `Cell` is sound and more efficient than `AtomicUsize`. +/// The cell is `Sync` when `T: Send`. The `owner` atomic ensures exclusive +/// ownership, while the `borrow` counter tracks active references from the +/// owning thread. Since only the owning thread can access `borrow` (protected +/// by the `owner` lock), using a non-atomic `Cell` is sound and more efficient +/// than `AtomicUsize`. /// /// # Examples /// @@ -87,8 +91,8 @@ type OwnedResult = Result; /// ``` pub struct OwnedRefCell { /// The inner storage for the optional value. - /// Uses `UnsafeCell` to allow interior mutability while maintaining `Sync` safety - /// through the owner lock. + /// Uses `UnsafeCell` to allow interior mutability while maintaining `Sync` + /// safety through the owner lock. inner: UnsafeCell>, /// Atomic tracking of which thread (by thread ID) currently owns this cell. @@ -96,8 +100,8 @@ pub struct OwnedRefCell { owner: AtomicU32, /// Reference counter tracking the number of active `Ref` instances. - /// This is a `Cell` (not `AtomicUsize`) because only the owning thread can access it, - /// with access protected by the `owner` atomic lock. + /// This is a `Cell` (not `AtomicUsize`) because only the owning thread can + /// access it, with access protected by the `owner` atomic lock. borrow: Cell, } @@ -108,8 +112,10 @@ impl OwnedRefCell { /// /// This is safe because: /// - Reads are protected by the atomic `owner` field - /// - Modifications only occur when a thread holds ownership via `OwnershipGuard` - /// - The borrow counter ensures the value isn't dropped while references exist + /// - Modifications only occur when a thread holds ownership via + /// `OwnershipGuard` + /// - The borrow counter ensures the value isn't dropped while references + /// exist #[inline] fn get_unchecked(&self) -> Option<&T> { unsafe { &*self.inner.get() }.as_ref() @@ -117,19 +123,21 @@ impl OwnedRefCell { /// Attempts to acquire ownership of this cell for the current thread. /// - /// This method acquires an `OwnershipGuard` which increments the reference count. - /// When all `OwnershipGuard` instances are dropped, ownership is released. + /// This method acquires an `OwnershipGuard` which increments the reference + /// count. When all `OwnershipGuard` instances are dropped, ownership is + /// released. /// /// # Parameters /// - /// - `reentrant`: If `true`, allows the current owner to acquire again (increments ref count). - /// If `false`, returns `Err` if the cell is already owned. + /// - `reentrant`: If `true`, allows the current owner to acquire again + /// (increments ref count). If `false`, returns `Err` if the cell is + /// already owned. /// /// # Returns /// /// - `Ok(OwnershipGuard)` if ownership was acquired or reacquired - /// - `Err(thread_id)` if another thread owns the cell, or if `reentrant=false` and - /// this thread already owns it + /// - `Err(thread_id)` if another thread owns the cell, or if + /// `reentrant=false` and this thread already owns it /// /// # Examples /// @@ -150,8 +158,9 @@ impl OwnedRefCell { /// Gets a reference to the value in the cell, if it exists. /// - /// This is a convenience method that acquires ownership (allowing reentrant access) - /// and returns a reference to the value if it has been initialized. + /// This is a convenience method that acquires ownership (allowing reentrant + /// access) and returns a reference to the value if it has been + /// initialized. /// /// # Returns /// @@ -166,8 +175,8 @@ impl OwnedRefCell { /// Initializes the cell with a value. /// - /// The calling thread must acquire ownership (or already own it) before initializing. - /// This method allows reentrant access. + /// The calling thread must acquire ownership (or already own it) before + /// initializing. This method allows reentrant access. /// /// # Panics /// @@ -226,23 +235,24 @@ impl Default for OwnedRefCell { /// - The `owner` atomic ensures exclusive ownership (only one thread at a time) /// - The `borrow` counter is only accessed by the owning thread /// - The value can be sent between threads when ownership is transferred -/// - After initialization, the value is immutable (only read through shared references) -// FIXME: since compio-rs/compio#660, `Proactor` is no longer `Send`, so we tentatively -// allow any `T` to be accessible from multiple threads, leaving undefined behavior -// if `T` is not `Send`. A proper fix would be to explicitly pin the thread ID by the -// runtime when it is known to be safe. See also #6. +/// - After initialization, the value is immutable (only read through shared +/// references) +// FIXME: since compio-rs/compio#660, `Proactor` is no longer `Send`, so we +// tentatively allow any `T` to be accessible from multiple threads, leaving +// undefined behavior if `T` is not `Send`. A proper fix would be to explicitly +// pin the thread ID by the runtime when it is known to be safe. See also #6. // unsafe impl Sync for OwnedRefCell where T: Send {} unsafe impl Sync for OwnedRefCell {} unsafe impl Send for OwnedRefCell {} /// A reference to a value inside an `OwnedRefCell`. /// -/// This type derefs to `T` and holds an `OwnershipGuard` to maintain the reference count. -/// When dropped, the reference count is decremented, and ownership may be released -/// if this was the last reference. +/// This type derefs to `T` and holds an `OwnershipGuard` to maintain the +/// reference count. When dropped, the reference count is decremented, and +/// ownership may be released if this was the last reference. /// -/// `Ref` is neither `Send` nor `Sync` because it must be dropped on the same thread -/// that created it to maintain correct ownership tracking. +/// `Ref` is neither `Send` nor `Sync` because it must be dropped on the same +/// thread that created it to maintain correct ownership tracking. pub struct Ref<'a, T> { /// Reference to the value stored in the cell. value: &'a T, @@ -260,19 +270,21 @@ impl Deref for Ref<'_, T> { } } -/// RAII guard that manages ownership and reference counting for an `OwnedRefCell`. +/// RAII guard that manages ownership and reference counting for an +/// `OwnedRefCell`. /// -/// Each `OwnershipGuard` increments the borrow counter when created and decrements it when -/// dropped. Ownership is released (reset to `NO_OWNER`) only when the reference count -/// reaches zero. +/// Each `OwnershipGuard` increments the borrow counter when created and +/// decrements it when dropped. Ownership is released (reset to `NO_OWNER`) only +/// when the reference count reaches zero. /// -/// `OwnershipGuard` is neither `Send` nor `Sync` (via `PhantomData<*const ()>`) because -/// it must be dropped on the same thread that created it to maintain correct ownership -/// tracking and borrow counting. +/// `OwnershipGuard` is neither `Send` nor `Sync` (via `PhantomData<*const ()>`) +/// because it must be dropped on the same thread that created it to maintain +/// correct ownership tracking and borrow counting. pub struct OwnershipGuard<'a> { /// Reference to the atomic owner field tracking which thread owns the cell. owner: &'a AtomicU32, - /// Reference to the non-atomic borrow counter (protected by the owner lock). + /// Reference to the non-atomic borrow counter (protected by the owner + /// lock). borrow: &'a Cell, /// Marker type to make this `!Send` and `!Sync`. /// Using `*const ()` because raw pointers are neither `Send` nor `Sync`. @@ -280,9 +292,11 @@ pub struct OwnershipGuard<'a> { } impl<'a> OwnershipGuard<'a> { - /// Attempts to create a new `OwnershipGuard` by acquiring or reacquiring ownership. + /// Attempts to create a new `OwnershipGuard` by acquiring or reacquiring + /// ownership. /// - /// This method uses a compare-and-swap loop to atomically manage the owner field. + /// This method uses a compare-and-swap loop to atomically manage the owner + /// field. /// /// # Parameters /// @@ -293,8 +307,8 @@ impl<'a> OwnershipGuard<'a> { /// # Returns /// /// - `Ok(OwnershipGuard)` if ownership was acquired or reacquired - /// - `Err(thread_id)` if another thread owns the cell, or if `reentrant=false` - /// and this thread already owns it + /// - `Err(thread_id)` if another thread owns the cell, or if + /// `reentrant=false` and this thread already owns it #[inline] fn new( owner: &'a AtomicU32, @@ -312,7 +326,8 @@ impl<'a> OwnershipGuard<'a> { while old == NO_OWNER { // Attempt to atomically swap NO_OWNER with our thread ID // Success ordering: AcqRel - // - Acquire: synchronize with previous Release store (though NO_OWNER means none) + // - Acquire: synchronize with previous Release store (though NO_OWNER means + // none) // - Release: publish our ownership to other threads // Failure ordering: Acquire // - Synchronize with concurrent modifications @@ -342,14 +357,16 @@ impl<'a> OwnershipGuard<'a> { /// /// # Safety /// - /// This must only be called after successfully acquiring ownership (either via CAS - /// or reentrant check). The caller must ensure that only the owning thread calls this. + /// This must only be called after successfully acquiring ownership (either + /// via CAS or reentrant check). The caller must ensure that only the + /// owning thread calls this. /// /// # Note on `Cell` safety /// /// Using `Cell::get()` and `Cell::set()` is safe here because: /// - Only the owning thread can call this (protected by the `owner` lock) - /// - The `OwnershipGuard` is `!Send`, so it can't be transferred to another thread + /// - The `OwnershipGuard` is `!Send`, so it can't be transferred to another + /// thread /// - This avoids the overhead of atomic operations while maintaining safety #[inline] fn new_unchecked(owner: &'a AtomicU32, borrow: &'a Cell) -> OwnershipGuard<'a> { @@ -367,9 +384,10 @@ impl<'a> OwnershipGuard<'a> { impl Drop for OwnershipGuard<'_> { /// Decrements the reference count when the guard is dropped. /// - /// If the count reaches zero, releases ownership by resetting the owner field - /// to `NO_OWNER`. Uses Release ordering to ensure all modifications made while - /// holding ownership are visible to the next thread that acquires it. + /// If the count reaches zero, releases ownership by resetting the owner + /// field to `NO_OWNER`. Uses Release ordering to ensure all + /// modifications made while holding ownership are visible to the next + /// thread that acquires it. #[inline] fn drop(&mut self) { // Decrement the borrow count @@ -388,9 +406,9 @@ impl Drop for OwnershipGuard<'_> { #[cfg(test)] mod tests { + use std::{sync::Arc, thread}; + use super::*; - use std::sync::Arc; - use std::thread; #[test] fn test_init_and_get() { diff --git a/compio-py/src/runtime.rs b/compio-py/src/runtime.rs index 15a8116..5690392 100644 --- a/compio-py/src/runtime.rs +++ b/compio-py/src/runtime.rs @@ -13,8 +13,7 @@ use std::{ Arc, atomic::{self, AtomicBool, AtomicUsize}, }, - task::Waker, - task::{Context, Poll}, + task::{Context, Poll, Waker}, time::{Duration, Instant}, }; @@ -28,7 +27,11 @@ use compio::{ }; use compio_executor::{Executor, JoinHandle}; use compio_log::*; -use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyDict, types::PyWeakrefReference}; +use pyo3::{ + exceptions::PyRuntimeError, + prelude::*, + types::{PyDict, PyWeakrefReference}, +}; /// Minimum number of _scheduled timer handles before cleanup of /// cancelled handles is performed. diff --git a/compio-py/src/send_wrapper.rs b/compio-py/src/send_wrapper.rs index 7eb2a41..9608895 100644 --- a/compio-py/src/send_wrapper.rs +++ b/compio-py/src/send_wrapper.rs @@ -96,7 +96,8 @@ impl Drop for SendWrapper { #[inline(never)] #[track_caller] fn invalid_drop() { - const DROP_ERROR: &'static str = "Dropped SendWrapper variable from a thread different to the one it has been created with."; + const DROP_ERROR: &'static str = "Dropped SendWrapper variable from a thread different to \ + the one it has been created with."; if !thread::panicking() { // panic because of dropping from wrong thread diff --git a/compio-py/src/thread.rs b/compio-py/src/thread.rs index 80e7475..e50e4ee 100644 --- a/compio-py/src/thread.rs +++ b/compio-py/src/thread.rs @@ -3,13 +3,14 @@ //! Thread ID utilities for atomic storage. //! -//! Provides a numeric representation of thread IDs that can be stored in atomic integers -//! for ownership tracking and cross-thread validation. +//! Provides a numeric representation of thread IDs that can be stored in atomic +//! integers for ownership tracking and cross-thread validation. //! //! # Implementation //! -//! This module uses a monotonic atomic counter to generate unique thread IDs, guaranteeing -//! that each thread receives a distinct non-zero identifier. This approach ensures: +//! This module uses a monotonic atomic counter to generate unique thread IDs, +//! guaranteeing that each thread receives a distinct non-zero identifier. This +//! approach ensures: //! - **Uniqueness**: No hash collisions possible //! - **Efficiency**: Thread-local caching via `thread_local!` //! - **Compatibility**: Works with atomic compare-exchange operations @@ -48,8 +49,9 @@ thread_local! { /// Returns a unique numeric identifier for the current thread. /// -/// This function is thread-safe and returns the same ID when called multiple times -/// from the same thread. Each thread is guaranteed to receive a distinct non-zero ID. +/// This function is thread-safe and returns the same ID when called multiple +/// times from the same thread. Each thread is guaranteed to receive a distinct +/// non-zero ID. /// /// # Returns /// @@ -68,17 +70,21 @@ thread_local! { /// # Performance /// /// This function is very fast as it simply reads from thread-local storage -/// after the first call. The initial call per thread performs one atomic increment. +/// after the first call. The initial call per thread performs one atomic +/// increment. pub fn get_current_thread_id() -> NonZero { CURRENT_THREAD_ID.with(|id| *id) } #[cfg(test)] mod tests { + use std::{ + collections::HashSet, + sync::{Arc, Mutex}, + thread, + }; + use super::*; - use std::collections::HashSet; - use std::sync::{Arc, Mutex}; - use std::thread; #[test] fn test_same_thread_returns_same_id() { diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..c45153d --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,19 @@ +unstable_features = true + +style_edition = "2024" + +group_imports = "StdExternalCrate" +imports_granularity = "Crate" +reorder_imports = true + +wrap_comments = true +normalize_comments = true + +reorder_impl_items = true +condense_wildcard_suffixes = true +enum_discrim_align_threshold = 20 +use_field_init_shorthand = true + +format_strings = true +format_code_in_doc_comments = true +format_macro_matchers = true