Skip to content

Commit

Permalink
add py_allow_threads() function
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kalinowski committed Aug 7, 2024
1 parent da89b64 commit 56b74ec
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ py_iterate <- function(x, f, simplify = TRUE) {
.Call(`_reticulate_py_iterate`, x, f, simplify)
}

py_allow_threads_impl <- function(allow) {
.Call(`_reticulate_py_allow_threads_impl`, allow)
}

readline <- function(prompt) {
.Call(`_reticulate_readline`, prompt)
}
Expand Down
16 changes: 16 additions & 0 deletions R/threads.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


py_allow_threads <- function(allow = TRUE) {
if (allow) {
reticulate_ns <- environment(sys.function())
for (f in sys.frames()) {
if (identical(parent.env(f), reticulate_ns) &&
!identical(f, environment()))
# Can't release the gil as unlocked while we're holding it
# elsewhere on the callstack.
stop("Unblocking python threads only allowed from as a top-level reticulate call")
}
}
invisible(py_allow_threads_impl(allow))
}

12 changes: 12 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,17 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// py_allow_threads_impl
bool py_allow_threads_impl(bool allow);
RcppExport SEXP _reticulate_py_allow_threads_impl(SEXP allowSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< bool >::type allow(allowSEXP);
rcpp_result_gen = Rcpp::wrap(py_allow_threads_impl(allow));
return rcpp_result_gen;
END_RCPP
}
// readline
SEXP readline(const std::string& prompt);
RcppExport SEXP _reticulate_readline(SEXP promptSEXP) {
Expand Down Expand Up @@ -895,6 +906,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_reticulate_as_iterator", (DL_FUNC) &_reticulate_as_iterator, 1},
{"_reticulate_py_iter_next", (DL_FUNC) &_reticulate_py_iter_next, 2},
{"_reticulate_py_iterate", (DL_FUNC) &_reticulate_py_iterate, 3},
{"_reticulate_py_allow_threads_impl", (DL_FUNC) &_reticulate_py_allow_threads_impl, 1},
{"_reticulate_readline", (DL_FUNC) &_reticulate_readline, 1},
{NULL, NULL, 0}
};
Expand Down
16 changes: 15 additions & 1 deletion src/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2868,7 +2868,7 @@ void py_initialize(const std::string& python,

s_main_thread = tthread::this_thread::get_id();
s_is_python_initialized = true;
GILScope scope(/*restore=*/ PyGILState_UNLOCKED);
GILScope _gil;

// initialize type objects
initialize_type_objects(is_python3());
Expand Down Expand Up @@ -4477,3 +4477,17 @@ SEXP py_exception_as_condition(PyObject* object, SEXP refenv) {
UNPROTECT(1);
return out;
}


// [[Rcpp::export]]
bool py_allow_threads_impl(bool allow) {
PyGILState_STATE gstate = PyGILState_Ensure();
if (allow) {
PyGILState_Release(PyGILState_UNLOCKED);
} else {
PyGILState_Release(PyGILState_LOCKED);
}
return gstate == PyGILState_UNLOCKED;
}


5 changes: 0 additions & 5 deletions src/reticulate_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,6 @@ class GILScope {
gstate = PyGILState_Ensure();
}

GILScope(PyGILState_STATE restore_state) {
PyGILState_Ensure();
gstate = restore_state;
}

~GILScope() {
PyGILState_Release(gstate);
}
Expand Down

0 comments on commit 56b74ec

Please sign in to comment.