-
Notifications
You must be signed in to change notification settings - Fork 124
Make API globals thread safe using atomics #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
use libc::FILE; | ||
use pyo3::ffi::{self, PyObject, PyTypeObject}; | ||
use std::os::raw::*; | ||
use std::{cell::Cell, ptr}; | ||
use std::ptr::null_mut; | ||
use std::sync::atomic::{AtomicPtr, Ordering}; | ||
|
||
use crate::npyffi::*; | ||
|
||
|
@@ -12,7 +13,7 @@ const CAPSULE_NAME: &str = "_ARRAY_API"; | |
/// A global variable which stores a ['capsule'](https://docs.python.org/3/c-api/capsule.html) | ||
/// pointer to [Numpy Array API](https://numpy.org/doc/stable/reference/c-api/array.html). | ||
/// | ||
/// You can acceess raw c APIs via this variable and its Deref implementation. | ||
/// You can acceess raw C APIs via this variable. | ||
/// | ||
/// See [PyArrayAPI](struct.PyArrayAPI.html) for what methods you can use via this variable. | ||
/// | ||
|
@@ -31,28 +32,35 @@ pub static PY_ARRAY_API: PyArrayAPI = PyArrayAPI::new(); | |
|
||
/// See [PY_ARRAY_API] for more. | ||
pub struct PyArrayAPI { | ||
api: Cell<*const *const c_void>, | ||
api: AtomicPtr<*const c_void>, | ||
} | ||
|
||
impl PyArrayAPI { | ||
const fn new() -> Self { | ||
Self { | ||
api: Cell::new(ptr::null_mut()), | ||
api: AtomicPtr::new(null_mut()), | ||
} | ||
} | ||
fn get(&self, offset: isize) -> *const *const c_void { | ||
if self.api.get().is_null() { | ||
Python::with_gil(|py| { | ||
let api = get_numpy_api(py, MOD_NAME, CAPSULE_NAME); | ||
self.api.set(api); | ||
}); | ||
#[cold] | ||
fn init(&self) -> *const *const c_void { | ||
Python::with_gil(|py| { | ||
let mut api = self.api.load(Ordering::Relaxed) as *const *const c_void; | ||
if api.is_null() { | ||
api = get_numpy_api(py, MOD_NAME, CAPSULE_NAME); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a potential gotcha, can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As CPython's import implementation can be hooked, I think one cannot prevent this from happening in general. But I also think that multiple threads performing the initialization is only an issue of efficiency. If a hook is releasing the GIL for whatever reason, it needs to be reacquired and all threads will only progress back here with the GIL held and at most store the same capsule pointer redundantly. (Doing the double-checking here on my part was only motivated by efficiency, i.e. we already have to take the lock so why not use this to avoid redundant initialization as we are already on the slow path.) (If multiple threads importing the same module yields a different capsule and hence API pointer, I think all bets are off and we would need external synchronization like using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Well we could But having the global at all seems weird if we are expecting that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, I think most likely this code is fine as-is thanks to the global nature. In |
||
self.api.store(api as *mut _, Ordering::Release); | ||
} | ||
api | ||
}) | ||
} | ||
unsafe fn get(&self, offset: isize) -> *const *const c_void { | ||
let mut api = self.api.load(Ordering::Acquire) as *const *const c_void; | ||
if api.is_null() { | ||
api = self.init(); | ||
} | ||
unsafe { self.api.get().offset(offset) } | ||
api.offset(offset) | ||
} | ||
} | ||
|
||
unsafe impl Sync for PyArrayAPI {} | ||
|
||
impl PyArrayAPI { | ||
impl_api![0; PyArray_GetNDArrayCVersion() -> c_uint]; | ||
impl_api![40; PyArray_SetNumericOps(dict: *mut PyObject) -> c_int]; | ||
|
Uh oh!
There was an error while loading. Please reload this page.