Skip to content

Commit

Permalink
Attempt to add critical section
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrameos committed Sep 15, 2024
1 parent bde9e6b commit 260bcf5
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions native/python/pyjp_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
#include "pyjp.h"
#include "jp_stringtype.h"
#include <Python.h>
#include <mutex>

#ifdef __cplusplus
extern "C"
{
#endif

std::mutex mtx;

Check notice

Code scanning / CodeQL

Short global name Note

Poor global variable name 'mtx'. Prefer longer, descriptive names for globals (eg. kMyGlobalConstant, not foo).

// Create a dummy type which we will use only for allocation
PyTypeObject* PyJPAlloc_Type = nullptr;

Expand Down Expand Up @@ -54,14 +57,18 @@ PyObject* PyJPValue_alloc(PyTypeObject* type, Py_ssize_t nitems)
return 0;
}
#endif

// Mutate the allocator type
PyJPAlloc_Type->tp_flags = type->tp_flags;
PyJPAlloc_Type->tp_basicsize = type->tp_basicsize + sizeof (JPValue);
PyJPAlloc_Type->tp_itemsize = type->tp_itemsize;

// Create a new allocation for the dummy type
PyObject* obj = PyType_GenericAlloc(PyJPAlloc_Type, nitems);

PyObject* obj = nullptr;
{
std::lock_guard<std::mutex> lock(mtx);
// Mutate the allocator type
PyJPAlloc_Type->tp_flags = type->tp_flags;
PyJPAlloc_Type->tp_basicsize = type->tp_basicsize + sizeof (JPValue);
PyJPAlloc_Type->tp_itemsize = type->tp_itemsize;

// Create a new allocation for the dummy type
obj = PyType_GenericAlloc(PyJPAlloc_Type, nitems);
}

// Watch for memory errors
if (obj == nullptr)
Expand Down

0 comments on commit 260bcf5

Please sign in to comment.