]> granicus.if.org Git - python/commitdiff
Issue #17522: Add the PyGILState_Check() API.
authorKristján Valur Jónsson <sweskman@gmail.com>
Sat, 23 Mar 2013 10:36:16 +0000 (03:36 -0700)
committerKristján Valur Jónsson <sweskman@gmail.com>
Sat, 23 Mar 2013 10:36:16 +0000 (03:36 -0700)
Doc/c-api/init.rst
Include/pystate.h
Misc/NEWS
Python/pystate.c

index 95ff4ee285d02b8e1ad0db0719037244cb632b53..377724c623e1383a8c5f5a06d411b04aa55e8e05 100644 (file)
@@ -654,6 +654,18 @@ with sub-interpreters:
    made on the main thread.  This is mainly a helper/diagnostic function.
 
 
+.. c:function:: int PyGILState_Check()
+
+   Return 1 if the current thread is holding the GIL and 0 otherwise.
+   This function can be called from any thread at any time.
+   Only if it has had its Python thread state initialized and currently is
+   holding the GIL will it return 1.
+   This is mainly a helper/diagnostic function.  It can be useful
+   for example in callback contexts or memory allocation functions when
+   knowing that the GIL is locked can allow the caller to perform sensitive
+   actions or otherwise behave differently.
+
+
 The following macros are normally used without a trailing semicolon; look for
 example usage in the Python source distribution.
 
index 2017b0276ff3346bc8d14a7f0f73883289f16a4f..b29ce2a482f340032ef4b1bae3c9bb1d1701f782 100644 (file)
@@ -212,6 +212,11 @@ PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE);
 */
 PyAPI_FUNC(PyThreadState *) PyGILState_GetThisThreadState(void);
 
+/* Helper/diagnostic function - return 1 if the current thread
+ * currently holds the GIL, 0 otherwise
+ */
+PyAPI_FUNC(int) PyGILState_Check(void);
+
 #endif   /* #ifdef WITH_THREAD */
 
 /* The implementation of sys._current_frames()  Returns a dict mapping
index c7833829b44a7b972077550d75ce53789ad6e152..ffa48ba4017912709e419595303966d6088fee1c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@ What's New in Python 3.4.0 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #17522: Add the PyGILState_Check() API.
+
 - Issue #16475: Support object instancing, recursion and interned strings
   in marshal
 
index cfd61d00986dc740cc2249cfd5ff51d3dc4c88fa..70038936d6741c7f3332207ae9b0faaf5f5960af 100644 (file)
@@ -697,6 +697,15 @@ PyGILState_GetThisThreadState(void)
     return (PyThreadState *)PyThread_get_key_value(autoTLSkey);
 }
 
+int
+PyGILState_Check(void)
+{
+    /* can't use PyThreadState_Get() since it will assert that it has the GIL */
+    PyThreadState *tstate = (PyThreadState*)_Py_atomic_load_relaxed(
+        &_PyThreadState_Current);
+    return tstate && (tstate == PyGILState_GetThisThreadState());
+}
+
 PyGILState_STATE
 PyGILState_Ensure(void)
 {