Commit ae8afa92 authored by Xavier Thompson's avatar Xavier Thompson

Integrate cypclass lock implementation directly into CyObject

parent 1b4106fa
...@@ -20,21 +20,26 @@ ...@@ -20,21 +20,26 @@
using namespace std; using namespace std;
#define CyObject_ATOMIC_REFCOUNT_TYPE atomic_int #define HAS_WRITER 0xffffffff
#define RETRY_THRESHOLD 100
struct CyPyObject {
PyObject_HEAD
};
class CyLock { class CyObject : public CyPyObject {
protected: private:
const uint32_t HAS_WRITER = 0xffffffff; mutable std::atomic_int nogil_ob_refcnt;
const int RETRY_THRESHOLD = 100; mutable std::atomic<uint32_t> _readers;
std::atomic<uint32_t> _readers;
public: public:
CyLock() { CyObject(): nogil_ob_refcnt(1), _readers(0) {}
_readers = 0; virtual ~CyObject() {}
}
void rlock() { void CyObject_INCREF() const;
int CyObject_DECREF() const;
int CyObject_GETREF() const;
void CyObject_RLOCK() const {
int retry = 0; int retry = 0;
while (true) { while (true) {
uint32_t prev_readers = _readers; uint32_t prev_readers = _readers;
...@@ -53,7 +58,7 @@ ...@@ -53,7 +58,7 @@
} }
} }
int tryrlock() { int CyObject_TRYRLOCK() const {
int retry = 0; int retry = 0;
while (true) { while (true) {
uint32_t prev_readers = _readers; uint32_t prev_readers = _readers;
...@@ -73,7 +78,7 @@ ...@@ -73,7 +78,7 @@
} }
} }
void unrlock() { void CyObject_UNRLOCK() const {
int retry = 0; int retry = 0;
while (true) { while (true) {
uint32_t prev_readers = _readers; uint32_t prev_readers = _readers;
...@@ -95,7 +100,7 @@ ...@@ -95,7 +100,7 @@
} }
} }
void wlock() { void CyObject_WLOCK() const {
int retry = 0; int retry = 0;
while (true) { while (true) {
uint32_t prev_readers = _readers; uint32_t prev_readers = _readers;
...@@ -113,7 +118,7 @@ ...@@ -113,7 +118,7 @@
} }
} }
int trywlock() { int CyObject_TRYWLOCK() const {
uint32_t prev_readers = _readers; uint32_t prev_readers = _readers;
if (prev_readers == 0) { if (prev_readers == 0) {
return _readers.compare_exchange_weak(prev_readers, HAS_WRITER) - 1; return _readers.compare_exchange_weak(prev_readers, HAS_WRITER) - 1;
...@@ -121,34 +126,12 @@ ...@@ -121,34 +126,12 @@
return -1; return -1;
} }
void unwlock() { void CyObject_UNWLOCK() const {
uint32_t prev_readers = HAS_WRITER; uint32_t prev_readers = HAS_WRITER;
_readers.compare_exchange_weak(prev_readers, 0); _readers.compare_exchange_weak(prev_readers, 0);
} }
}; };
struct CyPyObject {
PyObject_HEAD
};
class CyObject : public CyPyObject {
private:
mutable CyObject_ATOMIC_REFCOUNT_TYPE nogil_ob_refcnt;
mutable CyLock ob_lock;
public:
CyObject(): nogil_ob_refcnt(1) {}
virtual ~CyObject() {}
void CyObject_INCREF() const;
int CyObject_DECREF() const;
int CyObject_GETREF() const;
void CyObject_RLOCK() const;
void CyObject_WLOCK() const;
void CyObject_UNRLOCK() const;
void CyObject_UNWLOCK() const;
int CyObject_TRYRLOCK() const;
int CyObject_TRYWLOCK() const;
};
template <typename T, typename = void> template <typename T, typename = void>
struct Cy_has_equality : std::false_type {}; struct Cy_has_equality : std::false_type {};
...@@ -616,35 +599,6 @@ int CyObject::CyObject_GETREF() const ...@@ -616,35 +599,6 @@ int CyObject::CyObject_GETREF() const
return this->nogil_ob_refcnt; return this->nogil_ob_refcnt;
} }
void CyObject::CyObject_RLOCK() const
{
this->ob_lock.rlock();
}
void CyObject::CyObject_WLOCK() const
{
this->ob_lock.wlock();
}
int CyObject::CyObject_TRYRLOCK() const
{
return this->ob_lock.tryrlock();
}
int CyObject::CyObject_TRYWLOCK() const
{
return this->ob_lock.trywlock();
}
void CyObject::CyObject_UNRLOCK() const
{
this->ob_lock.unrlock();
}
void CyObject::CyObject_UNWLOCK() const
{
this->ob_lock.unwlock();
}
ActhonMessageInterface::ActhonMessageInterface(ActhonSyncInterface* sync_method, ActhonMessageInterface::ActhonMessageInterface(ActhonSyncInterface* sync_method,
ActhonResultInterface* result_object) : _sync_method(sync_method), _result(result_object) ActhonResultInterface* result_object) : _sync_method(sync_method), _result(result_object)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment