Skip to content
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

Freethreading to nogil #6210

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Cython/Compiler/Code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2718,16 +2718,16 @@ def put_release_ensured_gil(self, variable=None):
variable = '__pyx_gilstate_save'
self.putln("__Pyx_PyGILState_Release(%s);" % variable)

def put_acquire_freethreading_lock(self):
def put_acquire_nogil_lock(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, seeing this now makes it very ambiguous with Cython's nogil features. That's even worse because it really gives the wrong context. We use "nogil" in Cython's sources all over the place.

A name like "…cpython_nogil…" doesn't seem clear either. Maybe "freethreading" in one word isn't bad after all? _CPYTHON_NOGIL for the target macro and *_freethreading_* in our code base?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree (now you point that out).

I wonder if we should use "freethreading" everywhere (since it's what Python has chosen to call the build). But I'd also be happy to leave the target macro unchanged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Let's change it everywhere, also in the target macro name. We're freshly introducing it for 3.1, so let's use something really unambiguous.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - that's at #6213

self.globalstate.use_utility_code(
UtilityCode.load_cached("AccessPyMutexForFreeThreading", "ModuleSetupCode.c"))
UtilityCode.load_cached("AccessPyMutexForNoGIL", "ModuleSetupCode.c"))
self.putln("#if CYTHON_COMPILING_IN_CPYTHON_NOGIL")
self.putln(f"PyMutex_Lock(&{Naming.parallel_freethreading_mutex});")
self.putln(f"PyMutex_Lock(&{Naming.parallel_nogil_mutex});")
self.putln("#endif")

def put_release_freethreading_lock(self):
def put_release_nogil_lock(self):
self.putln("#if CYTHON_COMPILING_IN_CPYTHON_NOGIL")
self.putln(f"PyMutex_Unlock(&{Naming.parallel_freethreading_mutex});")
self.putln(f"PyMutex_Unlock(&{Naming.parallel_nogil_mutex});")
self.putln("#endif")

def put_acquire_gil(self, variable=None, unknown_gil_state=True):
Expand Down
2 changes: 1 addition & 1 deletion Cython/Compiler/Naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
exc_tb_name = pyrex_prefix + "exc_tb"
exc_lineno_name = pyrex_prefix + "exc_lineno"

parallel_freethreading_mutex = pyrex_prefix + "parallel_freethreading_mutex"
parallel_nogil_mutex = pyrex_prefix + "parallel_nogil_mutex"
parallel_exc_type = pyrex_prefix + "parallel_exc_type"
parallel_exc_value = pyrex_prefix + "parallel_exc_value"
parallel_exc_tb = pyrex_prefix + "parallel_exc_tb"
Expand Down
14 changes: 7 additions & 7 deletions Cython/Compiler/Nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9462,9 +9462,9 @@ def privatize_temps(self, code, exclude_temps=()):
shared_vars.extend(self.parallel_exc)
c.globalstate.use_utility_code(
UtilityCode.load_cached(
"SharedInFreeThreading",
"SharedInNoGIL",
"ModuleSetupCode.c"))
c.put(f" __Pyx_shared_in_cpython_nogil({Naming.parallel_freethreading_mutex})")
c.put(f" __Pyx_shared_in_cpython_nogil({Naming.parallel_nogil_mutex})")
c.put(" private(%s, %s, %s)" % self.pos_info)

c.put(" shared(%s)" % ', '.join(shared_vars))
Expand Down Expand Up @@ -9706,7 +9706,7 @@ def fetch_parallel_exception(self, code):
"""
code.begin_block()
code.put_ensure_gil(declare_gilstate=True)
code.put_acquire_freethreading_lock()
code.put_acquire_nogil_lock()

code.putln_openmp("#pragma omp flush(%s)" % Naming.parallel_exc_type)
code.putln(
Expand All @@ -9721,22 +9721,22 @@ def fetch_parallel_exception(self, code):
code.putln(
"}")

code.put_release_freethreading_lock()
code.put_release_nogil_lock()
code.put_release_ensured_gil()
code.end_block()

def restore_parallel_exception(self, code):
"Re-raise a parallel exception"
code.begin_block()
code.put_ensure_gil(declare_gilstate=True)
code.put_acquire_freethreading_lock()
code.put_acquire_nogil_lock()

code.put_giveref(Naming.parallel_exc_type, py_object_type)
code.putln("__Pyx_ErrRestoreWithState(%s, %s, %s);" % self.parallel_exc)
pos_info = chain(*zip(self.pos_info, self.parallel_pos_info))
code.putln("%s = %s; %s = %s; %s = %s;" % tuple(pos_info))

code.put_release_freethreading_lock()
code.put_release_nogil_lock()
code.put_release_ensured_gil()
code.end_block()

Expand Down Expand Up @@ -9777,7 +9777,7 @@ def end_parallel_control_flow_block(
c.putln("const char *%s = NULL; int %s = 0, %s = 0;" % self.parallel_pos_info)
c.putln("PyObject *%s = NULL, *%s = NULL, *%s = NULL;" % self.parallel_exc)
c.putln("#if CYTHON_COMPILING_IN_CPYTHON_NOGIL")
c.putln(f"PyMutex {Naming.parallel_freethreading_mutex} = {{0}};")
c.putln(f"PyMutex {Naming.parallel_nogil_mutex} = {{0}};")
c.putln("#endif")

code.putln(
Expand Down
4 changes: 2 additions & 2 deletions Cython/Utility/ModuleSetupCode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,7 @@ static PyObject* __Pyx_PyCode_New(
return code_obj;
}

/////////////////////////// AccessPyMutexForFreeThreading.proto ////////////
/////////////////////////// AccessPyMutexForNoGIL.proto ////////////

#if CYTHON_COMPILING_IN_CPYTHON_NOGIL
// TODO - this is likely to get exposed properly at some point
Expand All @@ -2241,7 +2241,7 @@ static PyObject* __Pyx_PyCode_New(
#include "internal/pycore_lock.h"
#endif

////////////////////////// SharedInFreeThreading.proto //////////////////
////////////////////////// SharedInNoGIL.proto //////////////////

#if CYTHON_COMPILING_IN_CPYTHON_NOGIL
#define __Pyx_shared_in_cpython_nogil(x) shared(x)
Expand Down
Loading