python / python/cpython

Rename PyGILState_Check()

オープン
#131,264 コメント 8 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core topic-C-API type-feature
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Feature or enhancement

Proposal:

Nearly all of the C-API expects that the current thread holds the GIL when called. Mostly, users of the C-API had to know before calling the C-API. Beck in the day it wasn't obvious how. This was the motivation behind adding PyGILState_Check().

The key point in this issue is that the name PyGILState_Check() unnecessarily associates the capability with the PyGILState_* API. Users would benefit from a different function name. I'd think something like Py_HoldsGIL() would be better.


Why was the function added as PyGILState_Check()? Perhaps @kristjanvalur remembers. It was added in 2013, 10 years after the PEP 311 implementation, so it's not because it was part of the PEP. I'd guess it's mostly because it was implemented using PyGILState_GetThisThreadState().

It's also notable that a lot of things have changed in the runtime since 2013. At the time PyGILState_Check() was added:

  • you could use PyThreadState_Get() (or PyThreadState_GET()) to get the tstate active in the current thread, but only if it held the GIL
  • you could get the last active tstate in the current thread using PyGILState_GetThisThreadState()
  • internally there was a static PyThreadState_IsCurrent() that sort of merged the two

Here some of the developments since then:

  • every tstate now knows whether or not it holds the GIL
  • the runtime now keeps track of the last tstate to hold the GIL
  • the current tstate has moved from a global variable to a thread-local variable
  • each interpreter now has its own GIL

Here's the history in some detail:

(expand)
  • 1997 - thread state (& interp state) introduced (commit a027efa5bfa7911b5c4b522b6a0698749a6f2e4a)
    • current tstate stored in static global variable
    • PyThreadState_Get() exposes it
    • PyThreadState_Swap() modifies it
    • GIL must be held to call it, so that global effectively indicates which tstate (i.e. thread) holds the GIL
  • 1998 - added PyThreadState_GET() macro (commit 275ea67e6b272234c3bccfedf0085308f8969412)
    • current tstate global variable moved to "internal" C-API (commit 18bc7c227676c91a6cfe97cd571bd0a5a187e373)
  • 2003 - PEP 311 adds the PyGILState_* API (commit 8d98d2cb95a),
    • last tstate used in current OS thread stored in thread-specific storage (autoTLSkey)
    • PyGILState_GetThisThreadState() exposes it
    • it is set to NULL when a tstate is destroyed and it was the last one used in the current thread
    • adds static PyThreadState_IsCurrent() that indicates if that thread-local tstate holds the GIL
    • PyGILState_Check() does not exist yet
  • 2009 - new GIL impl (commit 074e5ed974be65fbcfe75a4c0529dbc53f13446f)
    • adds new "last_holder" and "held" state
  • 2013 - PyGILState_Check() added (gh-61724, commit 684cd0e64311)
    • effectively exposes PyThreadState_IsCurrent(PyGILState_GetThisThreadState())
  • 2017 - _PyRuntimeState added (commit 2ebc5ce42a8a9e047e790aefbf9a94811569b2b6)
    • moved "current" tstate from global var in pystate.c (_PyThreadState_Current) to _PyRuntime.gilstate.tstate_current
    • moved GILState-related global vars to _PyRuntime.gilstate
    • moved GIL from global vars to _PyRuntime.ceval.gil
  • 2018 - added _PyThreadState_GET(), without NULL check (commit 50b48572d9a90c5bb36e2bef6179548ea927a35a)
  • 2023 - moved "current" tstate from _PyRuntime.gilstate.tstate_current to _PyRuntime.tstate_current (commit 6036c3e856f033bf13e929536e7bf127fdd921c9)
  • 2023 - moved "current" tstate to a thread-local variable in pystate.c (commit f8abfa331421e2c28388c5162f37820495e3c2ee)
  • 2023 - added internal current_thread_holds_gil (commit 92d8bfffbf377e91d8b92666525cb8700bb1d5e8)
  • 2023 - moved GIL to the interpreter state (commit 5c9ee498c6f4b75e0e020f17b6860309c3b7e11e)
  • 2024 - added PyThreadState._status.holds_gil (commit be1dfccdf2c5c7671b8a549e969b8cf7d60d9936)
Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、C-API のエントリーポイントである PyGILState_Check() と、issue にまとめられた runtime の経緯を確認します。提案されている Py_HoldsGIL() という名前も含めて確認してください。必要な公開 API と互換性の変更を判断します。名前の変更について合意され、関連する API サーフェスとテストに一貫して反映された時点で作業完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, python
領域
api, backend-api-design
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。