Skip to content

Commit b68a5d1

Browse files
refactor
1 parent e0bea0c commit b68a5d1

3 files changed

Lines changed: 27 additions & 29 deletions

File tree

cuda_core/cuda/core/experimental/_event.pyx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,20 @@ cdef class Event:
108108
self._ctx_handle = ctx_handle
109109
return self
110110

111-
cpdef safe_close(self, is_shutting_down=sys.is_finalizing):
112-
"""Destroy the event."""
113-
if self._handle is not None:
114-
if not is_shutting_down():
115-
err, = driver.cuEventDestroy(self._handle)
116-
raise_if_driver_error(err)
117-
118-
cpdef close(self):
119-
"""Destroy the event."""
111+
cdef _shutdown_safe_close(self, is_shutting_down=sys.is_finalizing):
112+
if is_shutting_down and is_shutting_down():
113+
return
120114
if self._handle is not None:
121115
err, = driver.cuEventDestroy(self._handle)
122116
self._handle = None
123117
raise_if_driver_error(err)
124118

119+
cpdef close(self):
120+
"""Destroy the event."""
121+
self._shutdown_safe_close(is_shutting_down=None)
122+
125123
def __del__(self):
126-
self.safe_close()
124+
self._shutdown_safe_close()
127125

128126
def __isub__(self, other):
129127
return NotImplemented

cuda_core/cuda/core/experimental/_memory.pyx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ cdef class Buffer:
7070
return self
7171

7272
def __del__(self):
73-
self.safe_close()
73+
self._shutdown_safe_close()
7474

75-
cpdef safe_close(self, stream: Stream = None, is_shutting_down=sys.is_finalizing):
75+
cdef _shutdown_safe_close(self, stream: Stream = None, is_shutting_down=sys.is_finalizing):
76+
if is_shutting_down and is_shutting_down():
77+
return
7678
if self._ptr and self._mr is not None:
77-
if not is_shutting_down():
78-
self._mr.deallocate(self._ptr, self._size, stream)
79+
self._mr.deallocate(self._ptr, self._size, stream)
80+
self._ptr = 0
81+
self._mr = None
82+
self._ptr_obj = None
7983

8084
cpdef close(self, stream: Stream = None):
8185
"""Deallocate this buffer asynchronously on the given stream.
@@ -89,11 +93,7 @@ cdef class Buffer:
8993
The stream object to use for asynchronous deallocation. If None,
9094
the behavior depends on the underlying memory resource.
9195
"""
92-
if self._ptr and self._mr is not None:
93-
self._mr.deallocate(self._ptr, self._size, stream)
94-
self._ptr = 0
95-
self._mr = None
96-
self._ptr_obj = None
96+
self._shutdown_safe_close(stream, is_shutting_down=None)
9797

9898
@property
9999
def handle(self) -> DevicePointerT:

cuda_core/cuda/core/experimental/_stream.pyx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,18 @@ cdef class Stream:
187187
return self
188188

189189
def __del__(self):
190-
self.safe_close()
190+
self._shutdown_safe_close()
191+
192+
cdef _shutdown_safe_close(self, is_shutting_down=sys.is_finalizing):
193+
if is_shutting_down and is_shutting_down():
194+
return
191195

192-
cpdef safe_close(self, is_shutting_down=sys.is_finalizing):
193196
if self._owner is None:
194197
if self._handle and not self._builtin:
195-
if not is_shutting_down():
196-
handle_return(driver.cuStreamDestroy(self._handle))
198+
handle_return(driver.cuStreamDestroy(self._handle))
199+
else:
200+
self._owner = None
201+
self._handle = None
197202

198203
cpdef close(self):
199204
"""Destroy the stream.
@@ -202,12 +207,7 @@ cdef class Stream:
202207
object will instead have their references released.
203208
204209
"""
205-
if self._owner is None:
206-
if self._handle and not self._builtin:
207-
handle_return(driver.cuStreamDestroy(self._handle))
208-
else:
209-
self._owner = None
210-
self._handle = None
210+
self._shutdown_safe_close(is_shutting_down=None)
211211

212212
def __cuda_stream__(self) -> tuple[int, int]:
213213
"""Return an instance of a __cuda_stream__ protocol."""

0 commit comments

Comments
 (0)