1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::{
any::Any,
fmt::{self, Formatter},
mem, ptr,
result::Result as StdResult,
sync::Arc,
};
use singe_cuda_sys::runtime;
use super::{CaptureCleanup, PER_THREAD_STREAM, Stream, StreamIdentity, StreamInner};
use crate::{
context::Context,
error::{Error, Result},
try_ffi,
};
/// Release responsibility returned with a raw native stream.
pub enum RawStreamOwnership {
/// The caller must eventually destroy the returned native handle.
Owned,
/// The external owner token must remain live through every use of the native handle.
Borrowed(Arc<dyn Any + Send + Sync>),
}
impl fmt::Debug for RawStreamOwnership {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Owned => "Owned",
Self::Borrowed(_) => "Borrowed",
})
}
}
/// Raw ownership of a CUDA stream, its context, and any external owner.
///
/// Destroy owned handles explicitly, keep borrowed tokens alive through every use, or re-adopt the value with [`Stream::from_raw`].
#[derive(Debug)]
pub struct RawStream {
/// Native stream handle whose release responsibility is described by `ownership`.
pub handle: runtime::cudaStream_t,
/// Actual CUDA context retained for native stream operations.
pub context: Arc<Context>,
/// Native destruction responsibility or external owner token.
pub ownership: RawStreamOwnership,
}
pub(crate) struct ForeignOwner(Arc<dyn Any + Send + Sync>);
impl fmt::Debug for ForeignOwner {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("ForeignOwner")
.field("references", &Arc::strong_count(&self.0))
.finish_non_exhaustive()
}
}
impl Stream {
/// Adopts a raw stream with the release responsibility recorded in `raw.ownership`.
///
/// An owned handle is synchronized and destroyed when the last clone drops; a borrowed handle keeps its owner token instead.
/// Use [`Stream::shutdown`] to surface synchronization or destruction errors explicitly.
///
/// # Safety
///
/// `raw.handle` must be a valid CUDA stream of `raw.context`.
/// An owned handle must not be destroyed elsewhere; a borrowed handle must stay valid while its owner token lives.
///
/// # Errors
///
/// Returns `Error::NullHandle` if `raw.handle` is null.
pub unsafe fn from_raw(raw: RawStream) -> Result<Self> {
if raw.handle.is_null() {
return Err(Error::NullHandle);
}
let foreign_owner = match raw.ownership {
RawStreamOwnership::Owned => None,
RawStreamOwnership::Borrowed(owner) => Some(ForeignOwner(owner)),
};
Ok(Self {
inner: Arc::new(StreamInner {
handle: raw.handle,
capture_cleanup: CaptureCleanup::for_stream(StreamIdentity::new(
&raw.context,
raw.handle,
))?,
context: raw.context,
foreign_owner,
}),
})
}
/// Borrows a stream while retaining the library object that owns its native handle.
/// Default legacy streams are accepted; per-thread default streams cannot be shared across threads.
///
/// # Safety
///
/// The handle must belong to `context` and remain valid while `owner` is retained.
/// External work must obey this wrapper's access ordering and capture lifecycle.
///
/// # Errors
///
/// Returns `Error::ThreadLocalStreamUnsupported` for the per-thread default-stream sentinel.
/// Also propagates context binding errors.
/// Other handle validity and external ownership requirements remain the caller's responsibility.
pub unsafe fn borrow_with_owner(
handle: runtime::cudaStream_t,
context: Arc<Context>,
owner: Arc<dyn Any + Send + Sync>,
) -> Result<Self> {
if handle.addr() == PER_THREAD_STREAM {
return Err(Error::ThreadLocalStreamUnsupported);
}
context.bind()?;
Ok(Self {
inner: Arc::new(StreamInner {
handle,
capture_cleanup: CaptureCleanup::for_stream(StreamIdentity::new(&context, handle))?,
context,
foreign_owner: Some(ForeignOwner(owner)),
}),
})
}
#[cfg(feature = "interop_cudarc")]
pub(crate) fn release_foreign_owner<T: Any + Send + Sync>(&self, owner: Arc<T>) -> Result<()> {
use super::StreamCaptureStatus;
use crate::utility::lock_for_cleanup;
let mut cleanup = lock_for_cleanup(&self.inner.capture_cleanup);
cleanup.foreign.push(ForeignOwner(owner));
if self.capture_status()? == StreamCaptureStatus::None {
cleanup.flush_frees(self.as_raw())?;
}
drop(cleanup);
Ok(())
}
}
impl Stream {
/// Transfers the native handle, context and destruction responsibility to the caller.
/// Destroy owned handles explicitly; retain borrowed ownership tokens through every use.
/// Returns the unchanged stream if another clone or pending capture cleanup still retains it.
///
/// # Errors
///
/// Returns `Err(self)` if another stream clone retains the wrapper or shared capture cleanup still holds deferred owners, frees, or foreign tokens.
/// The returned wrapper remains usable and retains its release responsibility.
pub fn into_raw(self) -> StdResult<RawStream, Self> {
let mut inner = match Arc::try_unwrap(self.inner) {
Ok(inner) => inner,
Err(inner) => return Err(Self { inner }),
};
// A poisoned lifecycle may hold deferred work, so its stream stays owned.
if !inner
.capture_cleanup
.lock()
.is_ok_and(|cleanup| cleanup.is_empty())
{
return Err(Self {
inner: Arc::new(inner),
});
}
let handle = mem::replace(&mut inner.handle, ptr::null_mut());
let ownership = match inner.foreign_owner.take() {
Some(ForeignOwner(owner)) => RawStreamOwnership::Borrowed(owner),
None => RawStreamOwnership::Owned,
};
Ok(RawStream {
handle,
context: Arc::clone(&inner.context),
ownership,
})
}
pub(crate) fn destroy_handle(context: &Context, handle: runtime::cudaStream_t) -> Result<()> {
context.bind()?;
// SAFETY: the caller passes the owned stream it is destroying, live in `context`, which is bound above.
let sync_result = unsafe { try_ffi!(runtime::cudaStreamSynchronize(handle)) };
// SAFETY: the stream is owned here, nothing else destroys it, and the synchronization above drained its work.
let destroy_result = unsafe { try_ffi!(runtime::cudaStreamDestroy(handle)) };
match (sync_result, destroy_result) {
(Ok(()), Ok(())) => Ok(()),
(Err(error), _) | (Ok(()), Err(error)) => Err(error),
}
}
}