Commit 643363d5 authored by Stefane Fermigier's avatar Stefane Fermigier

Merge branch '3.0a6-cypclass' of https://lab.nexedi.com/nexedi/cython into 3.0a6-cypclass

parents 49a8d8be 0071c4e2
......@@ -167,7 +167,7 @@ cdef cypclass cypdict[K, V]:
index = dereference(it).second
self._indices.erase(it)
if index < self._items.size() - 1:
self._items[index] = self._items[self._indices.size() - 1]
self._items[index] = self._items[self._items.size() - 1]
self._items.pop_back()
void update(self, const cypdict[K, V] other) except ~:
......
......@@ -19,8 +19,6 @@
#define CyObject_CONTENDING_WRITER_FLAG (1 << 0)
#define CyObject_CONTENDING_READER_FLAG (1 << 1)
#define CyObject_RAISE_ON_CONTENTION 0
#include <atomic>
#include <pthread.h>
......@@ -37,7 +35,6 @@
#include <type_traits>
class CyLock {
static pthread_mutex_t log_guard;
protected:
pthread_mutex_t guard;
pthread_cond_t readers_have_left;
......@@ -67,9 +64,6 @@
int tryrlock();
int trywlock();
};
#if CyObject_RAISE_ON_CONTENTION == 0
pthread_mutex_t CyLock::log_guard = PTHREAD_MUTEX_INITIALIZER;
#endif
struct CyPyObject {
PyObject_HEAD
......@@ -724,37 +718,6 @@ void CyLock::rlock(const char *context) {
pthread_mutex_lock(&this->guard);
if (this->write_count > 0) {
#if CyObject_RAISE_ON_CONTENTION
pid_t owner_id = this->owner_id;
std::ostringstream msg;
msg << "Data Race between [this] reader #" << caller_id
<< " and [other] writer #" << owner_id
<< " on lock " << this;
if (context != NULL) {
msg << std::endl << "In [this] context: " << context;
}
if (this->owner_context != NULL) {
msg << std::endl << "In [other] context: " << this->owner_context;
}
throw std::runtime_error(msg.str());
#else
pid_t owner_id = this->owner_id;
pthread_mutex_lock(&(CyLock::log_guard));
std::cerr
<< "Data Race between [this] reader #" << caller_id
<< " and [other] writer #" << owner_id
<< " on lock " << this << std::endl;
if (context != NULL) {
std::cerr << "In [this] context: " << context << std::endl;
}
if (this->owner_context != NULL) {
std::cerr << "In [other] context: " << this->owner_context << std::endl;
}
pthread_mutex_unlock(&(CyLock::log_guard));
#endif
}
while (this->write_count > 0) {
pthread_cond_wait(&this->writer_has_left, &this->guard);
}
......@@ -824,70 +787,10 @@ void CyLock::wlock(const char *context) {
// Since we use a reader-preferring approach, we wait first for all readers to leave, and then all writers.
// The other way around could result in several writers acquiring the lock.
if (this->readers_nb > 0) {
#if CyObject_RAISE_ON_CONTENTION
pid_t owner_id = this->owner_id;
std::ostringstream msg;
msg << "Data Race between [this] writer #" << caller_id
<< " and [other] reader #" << owner_id
<< " on lock " << this;
if (context != NULL) {
msg << std::endl << "In [this] context: " << context;
}
if (this->owner_context != NULL) {
msg << std::endl << "In [other] context: " << this->owner_context;
}
throw std::runtime_error(msg.str());
#else
pthread_mutex_lock(&(CyLock::log_guard));
std::cerr
<< "Data Race between [this] writer #" << caller_id
<< " and [other] reader #" << owner_id
<< " on lock " << this << std::endl;
if (context != NULL) {
std::cerr << "In [this] context: " << context << std::endl;
}
if (this->owner_context != NULL) {
std::cerr << "In [other] context: " << this->owner_context << std::endl;
}
pthread_mutex_unlock(&(CyLock::log_guard));
#endif
}
while (this->readers_nb > 0) {
pthread_cond_wait(&this->readers_have_left, &this->guard);
}
if (this->write_count > 0) {
#if CyObject_RAISE_ON_CONTENTION
pid_t owner_id = this->owner_id;
std::ostringstream msg;
msg << "Data Race between [this] writer #" << caller_id
<< " and [other] writer #" << owner_id
<< " on lock " << this;
if (context != NULL) {
msg << std::endl << "In [this] context: " << context;
}
if (this->owner_context != NULL) {
msg << std::endl << "In [other] context: " << this->owner_context;
}
throw std::runtime_error(msg.str());
#else
pthread_mutex_lock(&(CyLock::log_guard));
std::cerr
<< "Data Race between [this] writer #" << caller_id
<< " and [other] writer #" << owner_id
<< " on lock " << this << std::endl;
if (context != NULL) {
std::cerr << "In [this] context: " << context << std::endl;
}
if (this->owner_context != NULL) {
std::cerr << "In [other] context: " << this->owner_context << std::endl;
}
pthread_mutex_unlock(&(CyLock::log_guard));
#endif
}
while (this->write_count > 0) {
pthread_cond_wait(&this->writer_has_left, &this->guard);
}
......
......@@ -147,6 +147,39 @@ def test_len():
return -2
return 0
def test_delitem():
"""
>>> test_delitem()
(1, 10)
(2, 20)
(3, 30)
(4, 40)
(5, 50)
-------
(1, 10)
(2, 20)
(5, 50)
(4, 40)
-------
(4, 40)
(2, 20)
(5, 50)
"""
d = cypdict[int, int]()
for i in range(1, 7):
d[i] = i * 10
del d[6]
for item in d.items():
print(item)
print("-------")
del d[3]
for item in d.items():
print(item)
print("-------")
del d[1]
for item in d.items():
print(item)
def test_clear():
"""
>>> test_clear()
......
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