1111#include < mutex>
1212#include < stdexcept>
1313#include < unordered_map>
14- #include < utility>
1514#include < vector>
1615
1716#ifndef _WIN32
@@ -227,12 +226,8 @@ void clear_last_error() noexcept {
227226
228227namespace {
229228struct ContextBox {
230- mutable CUcontext resource;
229+ CUcontext resource;
231230 GreenCtxHandle h_green_ctx;
232- mutable std::mutex mutex;
233-
234- explicit ContextBox (CUcontext resource, GreenCtxHandle h_green_ctx = {})
235- : resource(resource), h_green_ctx(std::move(h_green_ctx)) {}
236231};
237232
238233struct GreenCtxBox {
@@ -262,7 +257,7 @@ ContextHandle create_context_handle_ref(CUcontext ctx) {
262257 return h;
263258 }
264259 auto box = std::shared_ptr<const ContextBox>(
265- new ContextBox ( ctx) ,
260+ new ContextBox{ ctx, {}} ,
266261 [](const ContextBox* b) {
267262 context_registry.unregister_handle (b->resource );
268263 delete b;
@@ -273,57 +268,31 @@ ContextHandle create_context_handle_ref(CUcontext ctx) {
273268 return h;
274269}
275270
276- static const GreenCtxBox* get_box (const GreenCtxHandle& h) noexcept {
277- const CUgreenCtx* p = h.get ();
278- return reinterpret_cast <const GreenCtxBox*>(
279- reinterpret_cast <const char *>(p) - offsetof (GreenCtxBox, resource)
280- );
281- }
282-
283271ContextHandle create_context_handle_from_green_ctx (const GreenCtxHandle& h_green_ctx) {
284272 if (!h_green_ctx) {
285273 return {};
286274 }
287- auto box = std::shared_ptr<const ContextBox>(
288- new ContextBox (nullptr , h_green_ctx),
289- [](const ContextBox* b) {
290- if (b->resource ) {
291- context_registry.unregister_handle (b->resource );
292- }
293- delete b;
294- }
295- );
296- return ContextHandle (box, &box->resource );
297- }
298-
299- CUcontext ensure_context_handle (const ContextHandle& h) noexcept {
300- if (!h) {
301- err = CUDA_ERROR_INVALID_CONTEXT;
302- return nullptr ;
303- }
304-
305- const ContextBox* box = get_box (h);
306- std::lock_guard<std::mutex> lock (box->mutex );
307- if (box->resource ) {
308- return box->resource ;
309- }
310- if (!box->h_green_ctx ) {
311- err = CUDA_ERROR_INVALID_CONTEXT;
312- return nullptr ;
313- }
314275 if (!p_cuCtxFromGreenCtx) {
315276 err = CUDA_ERROR_NOT_SUPPORTED;
316- return nullptr ;
277+ return {} ;
317278 }
318279
319280 GILReleaseGuard gil;
320281 CUcontext ctx = nullptr ;
321- if (CUDA_SUCCESS != (err = p_cuCtxFromGreenCtx (&ctx, as_cu (box-> h_green_ctx )))) {
322- return nullptr ;
282+ if (CUDA_SUCCESS != (err = p_cuCtxFromGreenCtx (&ctx, as_cu (h_green_ctx)))) {
283+ return {} ;
323284 }
324- box->resource = ctx;
285+
286+ auto box = std::shared_ptr<const ContextBox>(
287+ new ContextBox{ctx, h_green_ctx},
288+ [](const ContextBox* b) {
289+ context_registry.unregister_handle (b->resource );
290+ delete b;
291+ }
292+ );
293+ ContextHandle h (box, &box->resource );
325294 context_registry.register_handle (ctx, h);
326- return ctx ;
295+ return h ;
327296}
328297
329298GreenCtxHandle get_context_green_ctx (const ContextHandle& h) noexcept {
@@ -399,7 +368,7 @@ ContextHandle get_primary_context(int device_id) {
399368 }
400369
401370 auto box = std::shared_ptr<const ContextBox>(
402- new ContextBox ( ctx) ,
371+ new ContextBox{ ctx, {}} ,
403372 [device_id](const ContextBox* b) {
404373 context_registry.unregister_handle (b->resource );
405374 GILReleaseGuard gil;
0 commit comments