Commit Graph

636 Commits

Author SHA1 Message Date
Sam Gross 5716cc3529
gh-100240: Use a consistent implementation for freelists (#121934)
This combines and updates our freelist handling to use a consistent
implementation. Objects in the freelist are linked together using the
first word of memory block.

If configured with freelists disabled, these operations are essentially
no-ops.
2024-07-22 12:08:27 -04:00
Victor Stinner c5a6b9afd8
gh-121266: Remove Py_ALWAYS_INLINE in dictobject.c (#121493)
compare_unicode_generic(), compare_unicode_unicode() and
compare_generic() are callbacks used by do_lookup(). When enabling
assertions, it's not possible to inline these functions.
2024-07-18 08:19:32 +02:00
Victor Stinner 51da3dfbf3
gh-121266: Change dict check_lookup() return type to int (#121581) 2024-07-17 21:52:19 +02:00
Sam Gross 162b41f577
gh-121860: Fix crash when materializing managed dict (#121866)
The object's inline values may be marked invalid if the materialized
dict was already initialized and then deleted.
2024-07-16 14:58:36 -04:00
Sam Gross 3ec719fabf
gh-117657: Fix TSan race in _PyDict_CheckConsistency (#121551)
The only remaining race in dictobject.c was in _PyDict_CheckConsistency
when the dictionary has shared keys.
2024-07-10 14:04:12 -04:00
Ken Jin 3bfc9c831a
gh-120198: Stop the world when setting __class__ on free-threaded build (GH-120672) 2024-07-11 02:02:08 +08:00
AN Long 294e724964
gh-117657: Fix data races reported by TSAN in some set methods (#120914)
Refactor the fast Unicode hash check into `_PyObject_HashFast` and use relaxed
atomic loads in the free-threaded build.

After this change, the TSAN doesn't report data races for this method.
2024-07-01 15:11:39 -04:00
Sam Gross 375b723d58
gh-120858: PyDict_Next should not lock the dict (#120859)
PyDict_Next no longer locks the dictionary in the free-threaded build. Locking
around individual PyDict_Next calls is not sufficient because the function
returns borrowed references and because it allows concurrent modifications
during the iteraiton loop.

The internal locking also interferes with correct external synchronization
because it may suspend outer critical sections created by the caller.
2024-06-24 14:15:15 -04:00
Xie Yanbo 0153fd0940
Fix typos in comments (#120821) 2024-06-24 19:47:00 +02:00
Sam Gross 8f17d69b7b
gh-119344: Make critical section API public (#119353)
This makes the following macros public as part of the non-limited C-API for
locking a single object or two objects at once.

* `Py_BEGIN_CRITICAL_SECTION(op)` / `Py_END_CRITICAL_SECTION()`
* `Py_BEGIN_CRITICAL_SECTION2(a, b)` / `Py_END_CRITICAL_SECTION2()`

The supporting functions and structs used by the macros are also exposed for
cases where C macros are not available.
2024-06-21 15:50:18 -04:00
Petr Viktorin 6f1d448bc1
gh-113993: Allow interned strings to be mortal, and fix related issues (GH-120520)
* Add an InternalDocs file describing how interning should work and how to use it.

* Add internal functions to *explicitly* request what kind of interning is done:
  - `_PyUnicode_InternMortal`
  - `_PyUnicode_InternImmortal`
  - `_PyUnicode_InternStatic`

* Switch uses of `PyUnicode_InternInPlace` to those.

* Disallow using `_Py_SetImmortal` on strings directly.
  You should use `_PyUnicode_InternImmortal` instead:
  - Strings should be interned before immortalization, otherwise you're possibly
    interning a immortalizing copy.
  - `_Py_SetImmortal` doesn't handle the `SSTATE_INTERNED_MORTAL` to
    `SSTATE_INTERNED_IMMORTAL` update, and those flags can't be changed in
    backports, as they are now part of public API and version-specific ABI.

* Add private `_only_immortal` argument for `sys.getunicodeinternedsize`, used in refleak test machinery.

* Make sure the statically allocated string singletons are unique. This means these sets are now disjoint:
  - `_Py_ID`
  - `_Py_STR` (including the empty string)
  - one-character latin-1 singletons

  Now, when you intern a singleton, that exact singleton will be interned.

* Add a `_Py_LATIN1_CHR` macro, use it instead of `_Py_ID`/`_Py_STR` for one-character latin-1 singletons everywhere (including Clinic).

* Intern `_Py_STR` singletons at startup.

* For free-threaded builds, intern `_Py_LATIN1_CHR` singletons at startup.

* Beef up the tests. Cover internal details (marked with `@cpython_only`).

* Add lots of assertions

Co-Authored-By: Eric Snow <ericsnowcurrently@gmail.com>
2024-06-21 17:19:31 +02:00
Victor Stinner c2d5df5787
gh-83754: Use the Py_TYPE() macro (#120599)
Don't access directly PyObject.ob_type, but use the Py_TYPE() macro
instead.
2024-06-17 10:34:29 +02:00
Germán Méndez Bravo f0ed1863bd
gh-112075: Fix dict thread safety issues (#119288)
Fix dict thread safety issues
2024-05-24 10:39:06 -07:00
Dino Viehland 2b3fb767be
gh-117657: Fix missing atomic in dict_resize (#119312)
Fix missing atomic in dict_resize
2024-05-22 10:38:56 -07:00
Nikita Sobolev 82abe75e77
gh-118849: Fix "code will never be executed" warning in `dictobject.c` (#118850) 2024-05-09 20:15:14 +03:00
Dino Viehland 636b8d94c9
gh-112075: Fix race in constructing dict for instance (#118499) 2024-05-06 23:31:09 +00:00
Dino Viehland 5a1618a2c8
gh-118362: Fix thread safety around lookups from the type cache in the face of concurrent mutators (#118454)
Add _PyType_LookupRef and use incref before setting attribute on type
Makes setting an attribute on a class and signaling type modified atomic
Avoid adding re-entrancy exposing the type cache in an inconsistent state by decrefing after type is updated
2024-05-06 10:50:35 -07:00
Sam Gross 79688b5b0e
gh-118331: Handle errors in _PyObject_SetManagedDict (#118334)
When detaching a dict, the `copy_values` call may fail due to
out-of-memory errors. This can be triggered by test_no_memory in
test_repl.
2024-04-29 15:49:01 -04:00
Dino Viehland d5df25268b
gh-112075: _Py_dict_lookup needs to lock shared keys (#117528)
Lock shared keys in `Py_dict_lookup` and use thread-safe lookup in `insertdict`

Co-authored-by: Sam Gross <colesbury@gmail.com>
2024-04-25 15:34:05 -07:00
Dino Viehland 5da0280648
gh-117657: Fixes a few small TSAN issues in dictobject (#118200)
Fixup TSAN errors for dict
2024-04-25 08:53:29 -07:00
Dino Viehland 8b541c017e
gh-112075: Make instance attributes stored in inline "dict" thread safe (#114742)
Make instance attributes stored in inline "dict" thread safe on free-threaded builds
2024-04-21 22:57:05 -07:00
Noah Kim b624490dee
Fix a typo in dictobject.c documentation (#117515) 2024-04-19 19:19:12 +00:00
Dino Viehland 1e4a4c4897
gh-117657: use relaxed loads for checking dict keys immortality (#118067)
Use relaxed load to check if dictkeys are immortal
2024-04-19 09:25:08 -07:00
Donghee Na 94444ea45a
gh-112069: Add _PySet_NextEntryRef to be thread-safe. (gh-117990) 2024-04-19 00:18:22 +09:00
Mark Shannon 784e076a10
GH-117750: When clearing object's dict, clear inline values but leave dict attached (GH-117808) 2024-04-15 14:45:05 +01:00
Donghee Na 7bcc257e97
gh-117826: Remove lookdict_index from delitemif_lock_held (gh-117869) 2024-04-15 14:26:59 +09:00
Sam Gross 1a6594f661
gh-117439: Make refleak checking thread-safe without the GIL (#117469)
This keeps track of the per-thread total reference count operations in
PyThreadState in the free-threaded builds. The count is merged into the
interpreter's total when the thread exits.
2024-04-08 12:11:36 -04:00
mpage df73179048
gh-111926: Make weakrefs thread-safe in free-threaded builds (#117168)
Most mutable data is protected by a striped lock that is keyed on the
referenced object's address. The weakref's hash is protected using the
weakref's per-object lock.
 
Note that this only affects free-threaded builds. Apart from some minor
refactoring, the added code is all either gated by `ifdef`s or is a no-op
(e.g. `Py_BEGIN_CRITICAL_SECTION`).
2024-04-08 10:58:38 -04:00
Dino Viehland 434bc593df
gh-112075: Make _PyDict_LoadGlobal thread safe (#117529)
Make _PyDict_LoadGlobal threadsafe
2024-04-04 12:26:07 -07:00
Mark Shannon c32dc47aca
GH-115776: Embed the values array into the object, for "normal" Python objects. (GH-116115) 2024-04-02 11:59:21 +01:00
Dino Viehland ffcc450a9b
gh-112075: Enable freeing with qsbr and fallback to lock on key changed (GH-116336) 2024-03-05 09:08:18 +00:00
Sam Gross 58c7919d05
gh-116029: Fix unused function warning on macOS (#116340) 2024-03-04 22:06:21 -05:00
Humbulani 5e0c7bc1d3
gh-115941: fixes in dictobject.c doc block(#116196) 2024-03-01 12:48:26 -07:00
Dino Viehland 556749c3e3
gh-112075: Avoid locking shared keys on every assignment (#116087) 2024-02-29 13:38:50 -08:00
Dino Viehland 81c79961d2
gh-112075: Use relaxed stores for places where we may race with when reading lock-free (#115786) 2024-02-28 14:53:19 -08:00
Donghee Na f58f8cef74
gh-112075: Remove compiler warning from apple clang (gh-115855) 2024-02-29 02:51:59 +09:00
Dino Viehland 1002fbe12e
gh-112075: Iterating a dict shouldn't require locks (#115108)
Makes iteration of a dict be lock free for the forward iteration case.
2024-02-22 12:02:39 -08:00
Dino Viehland 54071460d7
gh-112075: Accessing a single element should optimistically avoid locking (#115109)
Makes accessing a single element thread safe and typically lock free
2024-02-20 17:08:14 -08:00
Dino Viehland 176df09adb
gh-112075: Make PyDictKeysObject thread-safe (#114741)
Adds locking for shared PyDictKeysObject's for dictionaries
2024-02-20 16:40:37 -08:00
Donghee Na 321d13fd2b
gh-111968: Split _Py_dictkeys_freelist out of _Py_dict_freelist (gh-115505) 2024-02-16 01:01:36 +00:00
Donghee Na f15795c9a0
gh-111968: Rename freelist related struct names to Eric's suggestion (gh-115329) 2024-02-14 00:32:51 +00:00
Sam Gross a3af3cb4f4
gh-110481: Implement inter-thread queue for biased reference counting (#114824)
Biased reference counting maintains two refcount fields in each object:
`ob_ref_local` and `ob_ref_shared`. The true refcount is the sum of these two
fields. In some cases, when refcounting operations are split across threads,
the ob_ref_shared field can be negative (although the total refcount must be
at least zero). In this case, the thread that decremented the refcount
requests that the owning thread give up ownership and merge the refcount
fields.
2024-02-09 17:08:32 -05:00
Dino Viehland 92abb01240
gh-112075: Add critical sections for most dict APIs (#114508)
Starts adding thread safety to dict objects.


Use @critical_section for APIs which are exposed via argument clinic and don't directly correlate with a public C API which needs to acquire the lock
Use a _lock_held suffix for keeping changes to complicated functions simple and just wrapping them with a critical section
Acquire and release the lock in an existing function where it won't be overly disruptive to the existing logic
2024-02-06 14:03:43 -08:00
Sam Gross de61d4bd4d
gh-112066: Add `PyDict_SetDefaultRef` function. (#112123)
The `PyDict_SetDefaultRef` function is similar to `PyDict_SetDefault`,
but returns a strong reference through the optional `**result` pointer
instead of a borrowed reference.

Co-authored-by: Petr Viktorin <encukou@gmail.com>
2024-02-06 11:36:23 -05:00
Mark Shannon 0e71a295e9
GH-113710: Add a "globals to constants" pass (GH-114592)
Converts specializations of `LOAD_GLOBAL` into constants during tier 2 optimization.
2024-02-02 12:14:34 +00:00
Donghee Na 13907968d7
gh-111968: Use per-thread freelists for dict in free-threading (gh-114323) 2024-02-01 20:53:53 +00:00
Dino Viehland 0990d55725
gh-112075: refactor dictionary lookup functions for better re-usability (#114629)
Refactor dict lookup functions to use force inline helpers
2024-01-30 09:33:36 -08:00
Dino Viehland 3d716655d2
gh-112075: Use PyMem_* for allocating dict keys objects (#114543)
Use PyMem_* for keys allocation
2024-01-29 09:38:03 -08:00
Dino Viehland afe8f376c0
gh-112075: Adapt more dict methods to Argument Clinic (#114256)
* Move more dict objects to argument clinic

* Improve doc strings

* More doc string improvements

* Update Objects/dictobject.c

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>

* Update Objects/dictobject.c

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>

* Update Objects/dictobject.c

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>

* Update Objects/dictobject.c

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>

* Update Objects/dictobject.c

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>

* Update Objects/dictobject.c

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>

* Update Objects/dictobject.c

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>

---------

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
2024-01-23 22:10:04 +00:00
Christopher Chavez a1eea1d032
gh-111178: Avoid calling functions from incompatible pointer types in dictobject.c (#112892)
Fix undefined behavior warnings (UBSan  -fsanitize=function).
2024-01-02 15:32:37 +01:00