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

python3 GIL locking solution #1675

Merged
merged 2 commits into from
Mar 27, 2024
Merged
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
4 changes: 0 additions & 4 deletions include/trick/IPPython.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#define IPPYTHON_HH

#include <string>
#include <pthread.h>
#include "trick/InputProcessor.hh"

namespace Trick {
Expand All @@ -36,9 +35,6 @@ namespace Trick {
/** Returned value from event condition evaluation.\n */
int return_val ; /**< trick_io(**) trick_units(--) */

/** Input processor mutex for protection for var server and event processing.\n */
pthread_mutex_t ip_mutex; /**< trick_io(**) trick_units(--) */

/**
@brief Constructor.
*/
Expand Down
31 changes: 19 additions & 12 deletions trick_source/sim_services/InputProcessor/IPPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include <sstream>
#include <vector>
#include <string>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include "trick/IPPython.hh"
#include "trick/MemoryManager.hh"
Expand All @@ -31,6 +31,10 @@ Trick::IPPython::IPPython() : Trick::InputProcessor::InputProcessor() , units_co
return ;
}


// Need to save the state of the main thread to allow child threads to run PyRun variants.
static PyThreadState *_save = NULL;

/**
@details
-# Loops through all of the memorymanager allocations testing if a name handle was given.
Expand Down Expand Up @@ -62,7 +66,9 @@ void Trick::IPPython::get_TMM_named_variables() {
ss << "trick.castAs" << user_type_name << "(int(" << alloc_info->start << "))" << std::endl ;
ss << "except AttributeError:" << std::endl ;
ss << " pass" << std::endl ;
PyGILState_STATE gstate = PyGILState_Ensure();
PyRun_SimpleString(ss.str().c_str()) ;
PyGILState_Release(gstate);
}
}
}
Expand All @@ -84,12 +90,6 @@ int Trick::IPPython::init() {
FILE *input_fp ;
int ret ;
std::string error_message ;
pthread_mutexattr_t m_attr ;

/* Initialize a mutex to protect python processing for var server and events. */
pthread_mutexattr_init(&m_attr) ;
pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE) ;
pthread_mutex_init(&ip_mutex , &m_attr) ;

// Run Py_Initialze first for python 2.x
#if PY_VERSION_HEX < 0x03000000
Expand All @@ -104,6 +104,7 @@ int Trick::IPPython::init() {
Py_Initialize();
#endif

// The following PyRun_ calls do not require the PyGILState guards because no threads are launched
/* Import simulation specific routines into interpreter. */
PyRun_SimpleString(
"import sys\n"
Expand Down Expand Up @@ -149,18 +150,21 @@ int Trick::IPPython::init() {
}

fclose(input_fp) ;
return(0) ;

// Release the GIL from the main thread.
Py_UNBLOCK_THREADS

return(0) ;
}

//Command to parse the given string.
int Trick::IPPython::parse(std::string in_string) {

int ret ;
pthread_mutex_lock(&ip_mutex);
in_string += "\n" ;
PyGILState_STATE gstate = PyGILState_Ensure();
ret = PyRun_SimpleString(in_string.c_str()) ;
pthread_mutex_unlock(&ip_mutex);
PyGILState_Release(gstate);

return ret ;

Expand All @@ -181,12 +185,12 @@ int Trick::IPPython::parse(std::string in_string) {
*/
int Trick::IPPython::parse_condition(std::string in_string, int & cond_return_val ) {

pthread_mutex_lock(&ip_mutex);
in_string = std::string("trick_ip.ip.return_val = ") + in_string + "\n" ;
// Running the simple string will set return_val.
PyGILState_STATE gstate = PyGILState_Ensure();
int py_ret = PyRun_SimpleString(in_string.c_str()) ;
PyGILState_Release(gstate);
cond_return_val = return_val ;
pthread_mutex_unlock(&ip_mutex);

return py_ret ;

Expand All @@ -200,7 +204,10 @@ int Trick::IPPython::restart() {
}

int Trick::IPPython::shutdown() {

if ( Py_IsInitialized() ) {
// Obtain the GIL so that we can shut down properly
Py_BLOCK_THREADS
Py_Finalize();
}
return(0) ;
Expand Down
Loading