| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
There was a problem hiding this comment.
I think we want to avoid expensive atomic operations, like atomic compare exchange and atomic increments, as well as avoiding locking.
This means giving up some atomicity for list and dict iterators compared to the GIL behavior. We should still avoid crashes/memory corruption, but I think it's okay for concurrent calls to next(it) on the same iterator object to return the same object. These iterators are almost always used by only a single thread and the performance cost of making the next atomic is relatively high.
See #114843 for the list iterator changes.
Sorry, something went wrong.
I was worried some crazy person might be using these things to distribute work across threads :P. I'm happy to make relax the guarantees and see how that goes. I suppose if that ever becomes an issue we can deal with it then :) |
Sorry, something went wrong.
| #endif | ||
|
|
||
| #ifndef Py_GIL_DISABLED |
There was a problem hiding this comment.
| #endif | |
| #ifndef Py_GIL_DISABLED | |
| #else /* Py_GIL_DISABLED */ |
Sorry, something went wrong.
| return NULL; | ||
| } | ||
|
|
||
| #endif |
There was a problem hiding this comment.
| #endif | |
| #endif /* Py_GIL_DISABLED */ |
Sorry, something went wrong.
There was a problem hiding this comment.
A few code style comments below
Sorry, something went wrong.
| #ifdef Py_GIL_DISABLED | ||
| if (has_unique_reference(result)) { | ||
| #else | ||
| if (Py_REFCNT(result) == 1) { | ||
| #endif |
There was a problem hiding this comment.
has_unique_reference already has special cases for Py_GIL_DISABLED and the default build:
| #ifdef Py_GIL_DISABLED | |
| if (has_unique_reference(result)) { | |
| #else | |
| if (Py_REFCNT(result) == 1) { | |
| #endif | |
| if (has_unique_reference(result)) { |
Sorry, something went wrong.
| if (values == NULL) | ||
| goto concurrent_modification; |
There was a problem hiding this comment.
I think the preferred style is to always include braces in new C code
Sorry, something went wrong.
| if (i >= used) | ||
| goto fail; |
There was a problem hiding this comment.
Same here
Sorry, something went wrong.
| // Even though we hold the lock here we may still lose a race against | ||
| // a lock-free iterator, therefore we may end up retrying our iteration. | ||
| retry: | ||
| start_pos = i = _Py_atomic_load_ssize_relaxed(&di->di_pos); |
There was a problem hiding this comment.
Maybe use the wrappers from pycore_pyatomic_ft_wrappers.h to reduce the number of #ifdef statements.
Sorry, something went wrong.
There was a problem hiding this comment.
This looks good, but I think it'd be better if acquire_key_value follows the -1/0 convention. The comment above acquire_key_value would need to be updated too.
Sorry, something went wrong.
| static int | ||
| acquire_key_value(PyObject **key_loc, PyObject *value, PyObject **value_loc, |
There was a problem hiding this comment.
Also here: -1 for error and 0 for success
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Makes iteration of a dict be lock free for the forward iteration case.
Handles races against the dict as well as allowing the iterator to be used from multiple threads simultaneously.
Includes some of the shared object marking from #115109