Skip to content

Commit a8f63df

Browse files
committed
[Refactor][NFC] Vendor-in compiler_lock for future CUDA-specific changes
1 parent e0f289f commit a8f63df

File tree

6 files changed

+64
-5
lines changed

6 files changed

+64
-5
lines changed

numba_cuda/numba/cuda/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from numba.cuda.core import bytecode
1111
from numba.cuda import types
1212
from numba.cuda.core.options import ParallelOptions
13-
from numba.core.compiler_lock import global_compiler_lock
13+
from numba.cuda.core.compiler_lock import global_compiler_lock
1414
from numba.cuda.core.errors import NumbaWarning, NumbaInvalidConfigWarning
1515
from numba.cuda.core.interpreter import Interpreter
1616

numba_cuda/numba/cuda/core/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from numba.cuda.core import imputils, targetconfig, funcdesc
1616
from numba.cuda import cgutils, debuginfo, types, utils, datamodel, config
1717
from numba.core import errors
18-
from numba.core.compiler_lock import global_compiler_lock
18+
from numba.cuda.core.compiler_lock import global_compiler_lock
1919
from numba.cuda.core.pythonapi import PythonAPI
2020
from numba.cuda.core.imputils import (
2121
user_function,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: BSD-2-Clause
3+
4+
import threading
5+
import functools
6+
import numba.cuda.core.event as ev
7+
8+
9+
# Lock for the preventing multiple compiler execution
10+
class _CompilerLock(object):
11+
def __init__(self):
12+
self._lock = threading.RLock()
13+
14+
def acquire(self):
15+
ev.start_event("numba.cuda:compiler_lock")
16+
self._lock.acquire()
17+
18+
def release(self):
19+
self._lock.release()
20+
ev.end_event("numba.cuda:compiler_lock")
21+
22+
def __enter__(self):
23+
self.acquire()
24+
25+
def __exit__(self, exc_val, exc_type, traceback):
26+
self.release()
27+
28+
def is_locked(self):
29+
is_owned = getattr(self._lock, "_is_owned")
30+
if not callable(is_owned):
31+
is_owned = self._is_owned
32+
return is_owned()
33+
34+
def __call__(self, func):
35+
@functools.wraps(func)
36+
def _acquire_compile_lock(*args, **kwargs):
37+
with self:
38+
return func(*args, **kwargs)
39+
40+
return _acquire_compile_lock
41+
42+
def _is_owned(self):
43+
# This method is borrowed from threading.Condition.
44+
# Return True if lock is owned by current_thread.
45+
# This method is called only if _lock doesn't have _is_owned().
46+
if self._lock.acquire(0):
47+
self._lock.release()
48+
return False
49+
else:
50+
return True
51+
52+
53+
global_compiler_lock = _CompilerLock()
54+
55+
56+
def require_global_compiler_lock():
57+
"""Sentry that checks the global_compiler_lock is acquired."""
58+
# Use assert to allow turning off this check
59+
assert global_compiler_lock.is_locked()

numba_cuda/numba/cuda/core/compiler_machinery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import inspect
88

99

10-
from numba.core.compiler_lock import global_compiler_lock
10+
from numba.cuda.core.compiler_lock import global_compiler_lock
1111
from numba.core import errors
1212
from numba.cuda.core import config
1313
from numba.cuda import utils

numba_cuda/numba/cuda/dispatcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from numba.cuda import serialize, utils
2020
from numba import cuda
2121

22-
from numba.core.compiler_lock import global_compiler_lock
22+
from numba.cuda.core.compiler_lock import global_compiler_lock
2323
from numba.cuda.typeconv.rules import default_type_manager
2424
from numba.cuda.typing.templates import fold_arguments
2525
from numba.cuda.typing.typeof import Purpose, typeof

numba_cuda/numba/cuda/target.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from numba.cuda import types
1212

13-
from numba.core.compiler_lock import global_compiler_lock
13+
from numba.cuda.core.compiler_lock import global_compiler_lock
1414
from numba.cuda.core.errors import NumbaWarning
1515
from numba.cuda.core.base import BaseContext
1616
from numba.cuda.typing import cmathdecl

0 commit comments

Comments
 (0)