Skip to content

Commit aee88c6

Browse files
leofangclaude
andcommitted
Simplify Context to hold only ContextHandle
Remove the _h_green_ctx and _is_green fields from Context. The green context association is already encoded in the ContextHandle's ContextBox (which carries a GreenCtxHandle dependency). is_green now queries get_context_green_ctx() on demand. _from_green_ctx delegates to _from_handle after creating the context handle. close() only resets _h_context — the ContextBox destructor handles the green ctx cleanup. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 55d4516 commit aee88c6

2 files changed

Lines changed: 6 additions & 13 deletions

File tree

cuda_core/cuda/core/_context.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ cdef class Context:
1313

1414
cdef:
1515
ContextHandle _h_context
16-
GreenCtxHandle _h_green_ctx
1716
int _device_id
18-
bint _is_green
1917
object __weakref__
2018

2119
@staticmethod

cuda_core/cuda/core/_context.pyx

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,17 @@ cdef class Context:
4343
"""Create Context from existing ContextHandle (cdef-only factory)."""
4444
cdef Context ctx = cls.__new__(cls)
4545
ctx._h_context = h_context
46-
ctx._h_green_ctx = get_context_green_ctx(h_context)
4746
ctx._device_id = device_id
48-
ctx._is_green = ctx._h_green_ctx.get() != NULL
4947
return ctx
5048

5149
@staticmethod
5250
cdef Context _from_green_ctx(type cls, GreenCtxHandle h_green_ctx, int device_id):
5351
"""Create Context from an owning green context handle."""
54-
cdef Context ctx = cls.__new__(cls)
55-
ctx._h_green_ctx = h_green_ctx
56-
ctx._h_context = create_context_handle_from_green_ctx(h_green_ctx)
57-
if not ctx._h_context:
52+
cdef ContextHandle h_context = create_context_handle_from_green_ctx(h_green_ctx)
53+
if not h_context:
5854
HANDLE_RETURN(get_last_error())
5955
raise RuntimeError("Failed to create CUDA context view from green context")
60-
ctx._device_id = device_id
61-
ctx._is_green = True
62-
return ctx
56+
return Context._from_handle(cls, h_context, device_id)
6357

6458
@property
6559
def handle(self):
@@ -77,7 +71,9 @@ cdef class Context:
7771
@property
7872
def is_green(self) -> bool:
7973
"""True if this context was created from device resources."""
80-
return bool(self._is_green)
74+
if not self._h_context:
75+
return False
76+
return get_context_green_ctx(self._h_context).get() != NULL
8177

8278
cpdef close(self):
8379
"""Release this context wrapper's underlying CUDA handles."""
@@ -91,7 +87,6 @@ cdef class Context:
9187
"Restore a previous context before closing this context."
9288
)
9389
self._h_context.reset()
94-
self._h_green_ctx.reset()
9590

9691
def __eq__(self, other):
9792
if not isinstance(other, Context):

0 commit comments

Comments
 (0)