@@ -37,7 +37,8 @@ PyObject* get_max_grid_size(PyObject *self, PyObject *args) {
3737 if (!PyArg_ParseTuple (args, " i" , &device_id))
3838 return nullptr ;
3939
40- Result<const DriverApi*> driver = get_driver_api ();
40+ GlobalLock lock;
41+ Result<const DriverApi*> driver = get_driver_api (lock);
4142 if (!driver.is_ok ()) return nullptr ;
4243
4344 CUdevice dev;
@@ -123,7 +124,8 @@ PyObject* get_compute_capability(PyObject *self, PyObject *args) {
123124 int device_id = 0 ;
124125 if (!PyArg_ParseTuple (args, " |i" , &device_id)) return nullptr ;
125126
126- Result<const DriverApi*> driver_result = get_driver_api ();
127+ GlobalLock lock;
128+ Result<const DriverApi*> driver_result = get_driver_api (lock);
127129 if (!driver_result.is_ok ()) return nullptr ;
128130
129131 Result<ComputeCapability> computeCapability =
@@ -135,7 +137,8 @@ PyObject* get_compute_capability(PyObject *self, PyObject *args) {
135137PyObject* get_driver_version (PyObject *self, PyObject *Py_UNUSED (ignored)) {
136138 int major, minor;
137139
138- Result<const DriverApi*> driver_result = get_driver_api ();
140+ GlobalLock lock;
141+ Result<const DriverApi*> driver_result = get_driver_api (lock);
139142 if (!driver_result.is_ok ()) return nullptr ;
140143 const DriverApi* d = *driver_result;
141144
@@ -152,7 +155,8 @@ PyObject* get_driver_version(PyObject *self, PyObject *Py_UNUSED(ignored)) {
152155// ========== Context helpers ==========
153156
154157PyObject* synchronize_context (PyObject* self, PyObject* Py_UNUSED (ignored)) {
155- Result<const DriverApi*> driver_result = get_driver_api ();
158+ GlobalLock lock;
159+ Result<const DriverApi*> driver_result = get_driver_api (lock);
156160 if (!driver_result.is_ok ()) return nullptr ;
157161 const DriverApi* d = *driver_result;
158162
@@ -167,7 +171,8 @@ PyObject* synchronize_context(PyObject* self, PyObject* Py_UNUSED(ignored)) {
167171// ========== Stream helpers ==========
168172
169173PyObject* create_stream (PyObject* self, PyObject* Py_UNUSED (ignored)) {
170- Result<const DriverApi*> driver_result = get_driver_api ();
174+ GlobalLock lock;
175+ Result<const DriverApi*> driver_result = get_driver_api (lock);
171176 if (!driver_result.is_ok ()) return nullptr ;
172177 const DriverApi* d = *driver_result;
173178
@@ -181,10 +186,11 @@ PyObject* create_stream(PyObject* self, PyObject* Py_UNUSED(ignored)) {
181186}
182187
183188PyObject* destroy_stream (PyObject* self, PyObject* arg) {
189+ GlobalLock lock;
184190 CUstream stream = static_cast <CUstream>(PyLong_AsVoidPtr (arg));
185191 if (PyErr_Occurred ()) return nullptr ;
186192
187- Result<const DriverApi*> driver_result = get_driver_api ();
193+ Result<const DriverApi*> driver_result = get_driver_api (lock );
188194 if (!driver_result.is_ok ()) return nullptr ;
189195 const DriverApi* d = *driver_result;
190196
@@ -226,15 +232,14 @@ static CUresult shim_cuLaunchKernelEx(
226232}
227233
228234static PyObject* spy_on_cuLaunchKernel_begin (PyObject* self, PyObject* arg) {
229- #ifdef Py_GIL_DISABLED
230- PyCriticalSectionGuard guard (&g_spy_mutex);
231- #endif
235+ GlobalLock lock;
236+
232237 if (g_real_cuLaunchKernelEx) {
233238 raise (PyExc_RuntimeError, " Already spying" );
234239 return nullptr ;
235240 }
236241
237- Result<const DriverApi*> driver_result = get_driver_api ();
242+ Result<const DriverApi*> driver_result = get_driver_api (lock );
238243 if (!driver_result.is_ok ()) return nullptr ;
239244
240245 DriverApi* api = const_cast <DriverApi*>(*driver_result);
@@ -245,15 +250,14 @@ static PyObject* spy_on_cuLaunchKernel_begin(PyObject* self, PyObject* arg) {
245250}
246251
247252static PyObject* spy_on_cuLaunchKernel_end (PyObject* self, PyObject* arg) {
248- #ifdef Py_GIL_DISABLED
249- PyCriticalSectionGuard guard (&g_spy_mutex);
250- #endif
253+ GlobalLock lock;
254+
251255 if (!g_real_cuLaunchKernelEx) {
252256 raise (PyExc_RuntimeError, " Not spying" );
253257 return nullptr ;
254258 }
255259
256- Result<const DriverApi*> driver_result = get_driver_api ();
260+ Result<const DriverApi*> driver_result = get_driver_api (lock );
257261 if (!driver_result.is_ok ()) return nullptr ;
258262
259263 DriverApi* api = const_cast <DriverApi*>(*driver_result);
0 commit comments