@@ -51,19 +51,29 @@ class Event:
51
51
52
52
"""
53
53
54
- __slots__ = ("__weakref__" , "_finalizer" , "_handle" , "_timing_disabled" , "_busy_waited" )
54
+ class _MembersNeededForFinalize :
55
+ __slots__ = ("handle" ,)
56
+
57
+ def __init__ (self , event_obj , handle ):
58
+ self .handle = handle
59
+ weakref .finalize (event_obj , self .close )
60
+
61
+ def close (self ):
62
+ if self .handle is not None :
63
+ handle_return (cuda .cuEventDestroy (self .handle ))
64
+ self .handle = None
65
+
66
+ __slots__ = ("__weakref__" , "_mnff" , "_timing_disabled" , "_busy_waited" )
55
67
56
68
def __init__ (self ):
57
- self ._handle = None
58
69
raise NotImplementedError (
59
70
"directly creating an Event object can be ambiguous. Please call call Stream.record()."
60
71
)
61
72
62
73
@staticmethod
63
74
def _init (options : Optional [EventOptions ] = None ):
64
75
self = Event .__new__ (Event )
65
- # minimal requirements for the destructor
66
- self ._handle = None
76
+ self ._mnff = Event ._MembersNeededForFinalize (self , None )
67
77
68
78
options = check_or_create_options (EventOptions , options , "Event options" )
69
79
flags = 0x0
@@ -77,20 +87,12 @@ def _init(options: Optional[EventOptions] = None):
77
87
self ._busy_waited = True
78
88
if options .support_ipc :
79
89
raise NotImplementedError ("TODO" )
80
- self ._handle = handle_return (cuda .cuEventCreate (flags ))
81
- self ._finalizer = weakref .finalize (self , Event ._finalize , self ._handle )
90
+ self ._mnff .handle = handle_return (cuda .cuEventCreate (flags ))
82
91
return self
83
92
84
- @staticmethod
85
- def _finalize (self_handle ):
86
- handle_return (cuda .cuEventDestroy (self_handle ))
87
-
88
93
def close (self ):
89
94
"""Destroy the event."""
90
- if self ._handle :
91
- self ._finalizer .Detach ()
92
- Event ._finalize (self ._handle )
93
- self ._handle = None
95
+ self ._mnff .close ()
94
96
95
97
@property
96
98
def is_timing_disabled (self ) -> bool :
@@ -117,12 +119,12 @@ def sync(self):
117
119
has been completed.
118
120
119
121
"""
120
- handle_return (cuda .cuEventSynchronize (self ._handle ))
122
+ handle_return (cuda .cuEventSynchronize (self ._mnff . handle ))
121
123
122
124
@property
123
125
def is_done (self ) -> bool :
124
126
"""Return True if all captured works have been completed, otherwise False."""
125
- (result ,) = cuda .cuEventQuery (self ._handle )
127
+ (result ,) = cuda .cuEventQuery (self ._mnff . handle )
126
128
if result == cuda .CUresult .CUDA_SUCCESS :
127
129
return True
128
130
elif result == cuda .CUresult .CUDA_ERROR_NOT_READY :
@@ -133,4 +135,4 @@ def is_done(self) -> bool:
133
135
@property
134
136
def handle (self ) -> int :
135
137
"""Return the underlying cudaEvent_t pointer address as Python int."""
136
- return int (self ._handle )
138
+ return int (self ._mnff . handle )
0 commit comments