@@ -22,14 +22,14 @@ namespace cuda_core {
2222
2323namespace {
2424
25- static std::once_flag driver_load_once;
26- static std::atomic<bool > driver_loaded{false };
25+ std::once_flag driver_load_once;
26+ std::atomic<bool > driver_loaded{false };
2727
2828#if PY_VERSION_HEX < 0x030D0000
2929extern " C" int _Py_IsFinalizing (void );
3030#endif
3131
32- static inline bool py_is_finalizing () noexcept {
32+ inline bool py_is_finalizing () noexcept {
3333#if PY_VERSION_HEX >= 0x030D0000
3434 return Py_IsFinalizing ();
3535#else
@@ -60,6 +60,9 @@ class GILReleaseGuard {
6060 tstate_ = PyEval_SaveThread ();
6161 released_ = true ;
6262 }
63+ // Note: If the GIL is not released (finalizing, or not held):
64+ // - Reduces parallelism (other Python threads remain blocked)
65+ // - No deadlock risk as long as the guarded code doesn't call back into Python
6366 }
6467
6568 ~GILReleaseGuard () {
@@ -108,7 +111,7 @@ class GILAcquireGuard {
108111};
109112
110113
111- #define DECLARE_DRIVER_FN (name ) using name##_t = decltype (&name); static name##_t p_##name = nullptr
114+ #define DECLARE_DRIVER_FN (name ) using name##_t = decltype (&name); name##_t p_##name = nullptr
112115
113116DECLARE_DRIVER_FN (cuDevicePrimaryCtxRetain);
114117DECLARE_DRIVER_FN (cuDevicePrimaryCtxRelease);
@@ -142,7 +145,7 @@ DECLARE_DRIVER_FN(cuMemPoolImportPointer);
142145
143146#undef DECLARE_DRIVER_FN
144147
145- static bool load_driver_api () noexcept {
148+ bool load_driver_api () noexcept {
146149 struct CudaDriverApiV1 {
147150 std::uint32_t abi_version;
148151 std::uint32_t struct_size;
@@ -261,7 +264,7 @@ static bool load_driver_api() noexcept {
261264 return true ;
262265}
263266
264- static bool ensure_driver_loaded () noexcept {
267+ bool ensure_driver_loaded () noexcept {
265268 // Fast path: already loaded (no locking needed)
266269 if (driver_loaded.load (std::memory_order_acquire)) {
267270 return true ;
@@ -286,7 +289,7 @@ static bool ensure_driver_loaded() noexcept {
286289// ============================================================================
287290
288291// Thread-local status of the most recent CUDA API call in this module.
289- thread_local CUresult err = CUDA_SUCCESS ;
292+ static thread_local CUresult err = CUDA_SUCCESS ;
290293
291294CUresult get_last_error () noexcept {
292295 CUresult e = err;
@@ -306,17 +309,19 @@ void clear_last_error() noexcept {
306309// Context Handles
307310// ============================================================================
308311
312+ namespace {
309313struct ContextBox {
310314 CUcontext resource;
311315};
316+ } // namespace
312317
313318ContextHandle create_context_handle_ref (CUcontext ctx) {
314319 auto box = std::make_shared<const ContextBox>(ContextBox{ctx});
315320 return ContextHandle (box, &box->resource );
316321}
317322
318323// Thread-local cache of primary contexts indexed by device ID
319- thread_local std::vector<ContextHandle> primary_context_cache;
324+ static thread_local std::vector<ContextHandle> primary_context_cache;
320325
321326ContextHandle get_primary_context (int device_id) noexcept {
322327 if (!ensure_driver_loaded ()) {
@@ -375,9 +380,11 @@ ContextHandle get_current_context() noexcept {
375380// Stream Handles
376381// ============================================================================
377382
383+ namespace {
378384struct StreamBox {
379385 CUstream resource;
380386};
387+ } // namespace
381388
382389StreamHandle create_stream_handle (ContextHandle h_ctx, unsigned int flags, int priority) {
383390 if (!ensure_driver_loaded ()) {
@@ -435,9 +442,11 @@ StreamHandle get_per_thread_stream() noexcept {
435442// Event Handles
436443// ============================================================================
437444
445+ namespace {
438446struct EventBox {
439447 CUevent resource;
440448};
449+ } // namespace
441450
442451EventHandle create_event_handle (ContextHandle h_ctx, unsigned int flags) {
443452 if (!ensure_driver_loaded ()) {
@@ -491,9 +500,11 @@ EventHandle create_event_handle_ipc(const CUipcEventHandle& ipc_handle) {
491500// Memory Pool Handles
492501// ============================================================================
493502
503+ namespace {
494504struct MemoryPoolBox {
495505 CUmemoryPool resource;
496506};
507+ } // namespace
497508
498509// Helper to clear peer access before destroying a memory pool.
499510// Works around nvbug 5698116: recycled pool handles inherit peer access state.
@@ -574,11 +585,21 @@ MemoryPoolHandle create_mempool_handle_ipc(int fd, CUmemAllocationHandleType han
574585// Device Pointer Handles
575586// ============================================================================
576587
588+ namespace {
577589struct DevicePtrBox {
578590 CUdeviceptr resource;
591+ // Mutable to allow set_deallocation_stream() to update the stream
592+ // through a const DevicePtrHandle. The stream can be changed after
593+ // allocation (e.g., to synchronize deallocation with a different stream).
579594 mutable StreamHandle h_stream;
580595};
596+ } // namespace
581597
598+ // Recovers the owning DevicePtrBox from the aliased CUdeviceptr pointer.
599+ // This works because DevicePtrHandle is a shared_ptr alias pointing to
600+ // &box->resource, so we can compute the containing struct using offsetof.
601+ // The const_cast is safe because we only use this to access the mutable
602+ // h_stream member or in the deleter (where the box is being destroyed).
582603static DevicePtrBox* get_box (const DevicePtrHandle& h) {
583604 const CUdeviceptr* p = h.get ();
584605 return reinterpret_cast <DevicePtrBox*>(
@@ -728,13 +749,15 @@ DevicePtrHandle deviceptr_create_with_owner(CUdeviceptr ptr, PyObject* owner) {
728749// The cache key is the export_data bytes (CUmemPoolPtrExportData), not the
729750// returned pointer, because we must check before calling the driver API.
730751
752+
731753// TODO: When driver fix for nvbug 5570902 is available, consider whether
732754// the cache is still needed for memory pool allocations (it will still be
733755// needed for pinned memory).
734756static bool use_ipc_ptr_cache () {
735757 return true ;
736758}
737759
760+ namespace {
738761// Wrapper for CUmemPoolPtrExportData to use as map key
739762struct ExportDataKey {
740763 CUmemPoolPtrExportData data;
@@ -756,6 +779,8 @@ struct ExportDataKeyHash {
756779 }
757780};
758781
782+ }
783+
759784static std::mutex ipc_ptr_cache_mutex;
760785static std::unordered_map<ExportDataKey, std::weak_ptr<DevicePtrBox>, ExportDataKeyHash> ipc_ptr_cache;
761786
0 commit comments