Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
windows_link::link!("kernel32.dll" "system" fn CloseThreadpool(ptpp : PTP_POOL));
windows_link::link!("kernel32.dll" "system" fn CloseThreadpoolCleanupGroup(ptpcg : PTP_CLEANUP_GROUP));
windows_link::link!("kernel32.dll" "system" fn CloseThreadpoolCleanupGroupMembers(ptpcg : PTP_CLEANUP_GROUP, fcancelpendingcallbacks : BOOL, pvcleanupcontext : *mut core::ffi::c_void));
windows_link::link!("kernel32.dll" "system" fn CreateThreadpool(reserved : *const core::ffi::c_void) -> PTP_POOL);
windows_link::link!("kernel32.dll" "system" fn CreateThreadpoolCleanupGroup() -> PTP_CLEANUP_GROUP);
windows_link::link!("kernel32.dll" "system" fn GetCurrentThreadId() -> u32);
windows_link::link!("kernel32.dll" "system" fn SetThreadpoolThreadMaximum(ptpp : PTP_POOL, cthrdmost : u32));
windows_link::link!("kernel32.dll" "system" fn SetThreadpoolThreadMinimum(ptpp : PTP_POOL, cthrdmic : u32) -> BOOL);
windows_link::link!("kernel32.dll" "system" fn Sleep(dwmilliseconds : u32));
windows_link::link!("kernel32.dll" "system" fn TrySubmitThreadpoolCallback(pfns : PTP_SIMPLE_CALLBACK, pv : *mut core::ffi::c_void, pcbe : *const TP_CALLBACK_ENVIRON_V3) -> BOOL);
pub type BOOL = i32;
pub type PTP_CALLBACK_INSTANCE = isize;
pub type PTP_CLEANUP_GROUP = isize;
pub type PTP_CLEANUP_GROUP_CANCEL_CALLBACK = Option<
unsafe extern "system" fn(
objectcontext: *mut core::ffi::c_void,
cleanupcontext: *mut core::ffi::c_void,
),
>;
pub type PTP_POOL = isize;
pub type PTP_SIMPLE_CALLBACK = Option<
unsafe extern "system" fn(instance: PTP_CALLBACK_INSTANCE, context: *mut core::ffi::c_void),
>;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct TP_CALLBACK_ENVIRON_V3 {
pub Version: u32,
pub Pool: PTP_POOL,
pub CleanupGroup: PTP_CLEANUP_GROUP,
pub CleanupGroupCancelCallback: PTP_CLEANUP_GROUP_CANCEL_CALLBACK,
pub RaceDll: *mut core::ffi::c_void,
pub ActivationContext: isize,
pub FinalizationCallback: PTP_SIMPLE_CALLBACK,
pub u: TP_CALLBACK_ENVIRON_V3_0,
pub CallbackPriority: TP_CALLBACK_PRIORITY,
pub Size: u32,
}
impl Default for TP_CALLBACK_ENVIRON_V3 {
fn default() -> Self {
unsafe { core::mem::zeroed() }
}
}
#[repr(C)]
#[derive(Clone, Copy)]
pub union TP_CALLBACK_ENVIRON_V3_0 {
pub Flags: u32,
pub s: TP_CALLBACK_ENVIRON_V3_0_0,
}
impl Default for TP_CALLBACK_ENVIRON_V3_0 {
fn default() -> Self {
unsafe { core::mem::zeroed() }
}
}
#[repr(C)]
#[derive(Clone, Copy, Default)]
pub struct TP_CALLBACK_ENVIRON_V3_0_0 {
pub _bitfield: u32,
}
pub type TP_CALLBACK_PRIORITY = i32;
pub const TP_CALLBACK_PRIORITY_NORMAL: TP_CALLBACK_PRIORITY = 1i32;

85
vendor/windows-threading/src/lib.rs vendored Normal file
View File

@@ -0,0 +1,85 @@
#![doc = include_str!("../readme.md")]
#![cfg(windows)]
#![no_std]
#![expect(non_snake_case, non_camel_case_types, clippy::upper_case_acronyms)]
mod bindings;
use bindings::*;
mod pool;
pub use pool::*;
extern crate alloc;
use alloc::boxed::Box;
use core::ffi::c_void;
/// Submit the closure to the default thread pool.
///
/// * The closure must have `'static` lifetime as the thread may outlive the lifetime in which `submit` is called.
/// * The closure must be `Send` as it will be sent to another thread for execution.
pub fn submit<F: FnOnce() + Send + 'static>(f: F) {
// This is safe because the closure has `'static` lifetime.
unsafe {
try_submit(core::ptr::null(), f);
}
}
/// Calls the closure on each element of the iterator in parallel, waiting for all closures to finish.
///
/// * The closure does not require `'static` lifetime since the `for_each` function bounds the lifetime of all submitted closures.
/// * The closure must be `Sync` as multiple threads will refer to it.
/// * The iterator items must be `Send` as they will be sent from one thread to another.
pub fn for_each<I, F, T>(i: I, f: F)
where
I: Iterator<Item = T>,
F: Fn(T) + Sync,
T: Send,
{
Pool::with_scope(|pool| {
for item in i {
pool.submit(|| f(item));
}
});
}
/// The thread identifier of the calling thread.
pub fn thread_id() -> u32 {
unsafe { GetCurrentThreadId() }
}
/// Suspends the execution of the current thread until the time-out interval elapses.
pub fn sleep(milliseconds: u32) {
unsafe {
Sleep(milliseconds);
}
}
// When used correctly, the Windows thread pool APIs only fail when memory is exhausted. This function will cause such failures to `panic`.
fn check<D: Default + PartialEq>(result: D) -> D {
if result == D::default() {
panic!("allocation failed");
}
result
}
// This function is `unsafe` as it cannot ensure that the lifetime of the closure is sufficient or
// whether the `environment` pointer is valid.
unsafe fn try_submit<F: FnOnce() + Send>(environment: *const TP_CALLBACK_ENVIRON_V3, f: F) {
unsafe extern "system" fn callback<F: FnOnce() + Send>(
_: PTP_CALLBACK_INSTANCE,
callback: *mut c_void,
) {
unsafe {
Box::from_raw(callback as *mut F)();
}
}
unsafe {
check(TrySubmitThreadpoolCallback(
Some(callback::<F>),
Box::into_raw(Box::new(f)) as _,
environment,
));
}
}

139
vendor/windows-threading/src/pool.rs vendored Normal file
View File

@@ -0,0 +1,139 @@
use super::*;
use core::{marker::PhantomData, ops::Deref};
/// A `Pool` object represents a private thread pool with its own thread limits.
///
/// This is in contrast to the default, or shared, thread pool used by the crate's `submit` function
/// as well as other code within the same process.
pub struct Pool(Box<TP_CALLBACK_ENVIRON_V3>);
impl Pool {
/// Creates a new `Pool` object.
pub fn new() -> Self {
let mut e = TP_CALLBACK_ENVIRON_V3 {
Version: 3,
CallbackPriority: TP_CALLBACK_PRIORITY_NORMAL,
Size: core::mem::size_of::<TP_CALLBACK_ENVIRON_V3>() as u32,
..Default::default()
};
unsafe {
e.Pool = check(CreateThreadpool(core::ptr::null()));
e.CleanupGroup = check(CreateThreadpoolCleanupGroup());
}
// The `TP_CALLBACK_ENVIRON_V3` is boxed to ensure its memory address remains stable for the life of the `Pool` object.
Self(Box::new(e))
}
/// Convenience function for creating a new pool and calling [`scope`][Self::scope].
pub fn with_scope<'env, F>(f: F)
where
F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>),
{
let pool = Pool::new();
pool.scope(f);
}
/// Sets the thread limits for the `Pool` object.
pub fn set_thread_limits(&self, min: u32, max: u32) {
unsafe {
check(SetThreadpoolThreadMinimum(self.0.Pool, min));
SetThreadpoolThreadMaximum(self.0.Pool, max);
}
}
/// Submit the closure to the thread pool.
///
/// * The closure must have `'static` lifetime as the thread may outlive the lifetime in which `submit` is called.
/// * The closure must be `Send` as it will be sent to another thread for execution.
pub fn submit<F: FnOnce() + Send + 'static>(&self, f: F) {
// This is safe because the closure has a `'static` lifetime.
unsafe {
try_submit(&*self.0, f);
}
}
/// Create a scope for submitting closures.
///
/// Within this scope local variables can be sent to the pool thread for execution.
/// This is possible because `scope` will wait for all submitted closures to finish before returning,
/// Note however that it will also wait for closures that were submitted from other threads.
pub fn scope<'env, F>(&self, f: F)
where
F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>),
{
struct DropGuard<'a>(&'a Pool);
impl Drop for DropGuard<'_> {
fn drop(&mut self) {
self.0.join();
}
}
// Ensure that we always join the pool before returning.
let _guard = DropGuard(self);
let scope = Scope {
pool: self,
env: PhantomData,
scope: PhantomData,
};
f(&scope);
}
/// Waits for all submissions to finish.
///
/// Dropping the `Pool` will also wait for all submissions to finish.
pub fn join(&self) {
unsafe {
CloseThreadpoolCleanupGroupMembers(self.0.CleanupGroup, 0, core::ptr::null_mut());
}
}
}
impl Default for Pool {
fn default() -> Self {
Self::new()
}
}
unsafe impl Sync for Pool {}
unsafe impl Send for Pool {}
impl Drop for Pool {
fn drop(&mut self) {
// The `Pool` object cannot be dropped without waiting for all closures to complete, as their
// lifetimes are only guaranteed to be as long as the `Pool` object.
self.join();
unsafe {
CloseThreadpoolCleanupGroup(self.0.CleanupGroup);
CloseThreadpool(self.0.Pool);
}
}
}
/// A scope to submit closures in.
///
/// See [`scope`][Pool::scope] for details.
pub struct Scope<'scope, 'env: 'scope> {
pool: &'scope Pool,
scope: PhantomData<&'scope mut &'scope ()>,
env: PhantomData<&'env mut &'env ()>,
}
impl<'scope, 'env> Scope<'scope, 'env> {
/// Submits the closure to run on the `Pool`.
///
/// The closure cannot outlive the `Scope` it's run in.
pub fn submit<F: FnOnce() + Send + 'scope>(&'scope self, f: F) {
unsafe {
try_submit(&*self.pool.0, f);
}
}
}
impl Deref for Scope<'_, '_> {
type Target = Pool;
fn deref(&self) -> &Self::Target {
self.pool
}
}