Skip to content

Commit

Permalink
Event._finalize() approach with self._finalizer.Detach()
Browse files Browse the repository at this point in the history
  • Loading branch information
rwgk committed Nov 27, 2024
1 parent 3329a0a commit 74d2859
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions cuda_core/cuda/core/experimental/_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,19 @@ class Event:
"""

__slots__ = ("__weakref__", "_handle", "_timing_disabled", "_busy_waited")
__slots__ = ("__weakref__", "_finalizer", "_handle", "_timing_disabled", "_busy_waited")

def __init__(self):
self._handle = None
raise NotImplementedError(
"directly creating an Event object can be ambiguous. Please call call Stream.record()."
)

def _enable_finalize(self):
self._handle = None
weakref.finalize(self, self.close)

@staticmethod
def _init(options: Optional[EventOptions] = None):
self = Event.__new__(Event)
self._enable_finalize()
# minimal requirements for the destructor
self._handle = None

options = check_or_create_options(EventOptions, options, "Event options")
flags = 0x0
Expand All @@ -80,12 +78,18 @@ def _init(options: Optional[EventOptions] = None):
if options.support_ipc:
raise NotImplementedError("TODO")
self._handle = handle_return(cuda.cuEventCreate(flags))
self._finalizer = weakref.finalize(self, Event._finalize, self._handle)
return self

@staticmethod
def _finalize(self_handle):
handle_return(cuda.cuEventDestroy(self_handle))

def close(self):
"""Destroy the event."""
if self._handle:
handle_return(cuda.cuEventDestroy(self._handle))
self._finalizer.Detach()
Event._finalize(self._handle)
self._handle = None

@property
Expand Down

0 comments on commit 74d2859

Please sign in to comment.