Skip to content

Commit

Permalink
Lock aroud getting/setting variable context
Browse files Browse the repository at this point in the history
  • Loading branch information
lysnikolaou committed Dec 6, 2024
1 parent e9cea61 commit 513e247
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions py/src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ extern std::recursive_mutex global_lock;
#define RELEASE_GLOBAL_LOCK()
#endif

#ifndef Py_BEGIN_CRITICAL_SECTION
#define Py_BEGIN_CRITICAL_SECTION(op) {
#define Py_END_CRITICAL_SECTION() }
#endif

inline bool
convert_to_double( PyObject* obj, double& out )
{
Expand Down
24 changes: 22 additions & 2 deletions py/src/variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Variable_setName( Variable* self, PyObject* pystr )


PyObject*
Variable_context( Variable* self )
Variable_context_locked( Variable* self )
{
if( self->context )
return cppy::incref( self->context );
Expand All @@ -139,12 +139,32 @@ Variable_context( Variable* self )


PyObject*
Variable_setContext( Variable* self, PyObject* value )
Variable_context( Variable* self )
{
PyObject* context;
Py_BEGIN_CRITICAL_SECTION(self);
context = Variable_context_locked(self);
Py_END_CRITICAL_SECTION();
return context;
}


void
Variable_setContext_locked( Variable* self, PyObject* value )
{
if( value != self->context )
{
Py_XSETREF(self->context, cppy::incref( value ));
}
}


PyObject*
Variable_setContext( Variable* self, PyObject* value )
{
Py_BEGIN_CRITICAL_SECTION(self);
Variable_setContext_locked(self, value);
Py_END_CRITICAL_SECTION();
Py_RETURN_NONE;
}

Expand Down

0 comments on commit 513e247

Please sign in to comment.