Skip to content

Commit

Permalink
Remove usage of mutex in TQueue and SelfEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Cornu committed Feb 26, 2024
1 parent 0cfc93d commit 12ca064
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/nrncvode/netcvode.cpp
Expand Up @@ -1034,7 +1034,7 @@ Object** NetCvode::netconlist() {
NetCvodeThreadData::NetCvodeThreadData() {
tpool_ = new TQItemPool(1000, 1);
// tqe_ accessed only by thread i so no locking
tqe_ = new TQueue(tpool_, 0);
tqe_ = new TQueue(tpool_);
sepool_ = new SelfEventPool(1000, 1);
selfqueue_ = nullptr;
psl_thr_ = nullptr;
Expand Down Expand Up @@ -2794,7 +2794,7 @@ void NetCvode::clear_events() {
d.ite_cnt_ = 0;
if (nrn_use_selfqueue_) {
if (!d.selfqueue_) {
d.selfqueue_ = new SelfQueue(d.tpool_, 0);
d.selfqueue_ = new SelfQueue(d.tpool_);
} else {
d.selfqueue_->remove_all();
}
Expand Down
32 changes: 2 additions & 30 deletions src/nrncvode/tqueue.cpp
Expand Up @@ -162,8 +162,7 @@ static void chk(TQItem* b, int level) {
}
}

TQueue::TQueue(TQItemPool* tp, int mkmut) {
MUTCONSTRUCT(mkmut)
TQueue::TQueue(TQItemPool* tp) {
tpool_ = tp;
nshift_ = 0;
sptree_ = new SPTREE<TQItem>;
Expand All @@ -188,35 +187,30 @@ TQueue::~TQueue() {
remove(q);
}
delete binq_;
MUTDESTRUCT
}

void TQueue::deleteitem(TQItem* i) {
tpool_->hpfree(i);
}

void TQueue::print() {
MUTLOCK
if (least_) {
prnt(least_, 0);
}
spscan(prnt, static_cast<TQItem*>(nullptr), sptree_);
for (TQItem* q = binq_->first(); q; q = binq_->next(q)) {
prnt(q, 0);
}
MUTUNLOCK
}

void TQueue::forall_callback(void (*f)(const TQItem*, int)) {
MUTLOCK
if (least_) {
f(least_, 0);
}
spscan(f, static_cast<TQItem*>(nullptr), sptree_);
for (TQItem* q = binq_->first(); q; q = binq_->next(q)) {
f(q, 0);
}
MUTUNLOCK
}

void TQueue::check(const char* mes) {}
Expand All @@ -233,9 +227,7 @@ TQItem* TQueue::second_least(double t) {
}

void TQueue::move_least(double tnew) {
MUTLOCK
move_least_nolock(tnew);
MUTUNLOCK
}

void TQueue::move_least_nolock(double tnew) {
Expand All @@ -253,7 +245,6 @@ void TQueue::move_least_nolock(double tnew) {
}

void TQueue::move(TQItem* i, double tnew) {
MUTLOCK
STAT(nmove)
if (i == least_) {
move_least_nolock(tnew);
Expand All @@ -267,7 +258,6 @@ void TQueue::move(TQItem* i, double tnew) {
i->t_ = tnew;
spenq(i, sptree_);
}
MUTUNLOCK
}

void TQueue::statistics() {
Expand Down Expand Up @@ -295,7 +285,6 @@ void TQueue::spike_stat(double* d) {
}

TQItem* TQueue::insert(double t, void* d) {
MUTLOCK
STAT(ninsert);
TQItem* i = tpool_->alloc();
i->data_ = d;
Expand All @@ -309,18 +298,15 @@ TQItem* TQueue::insert(double t, void* d) {
} else {
spenq(i, sptree_);
}
MUTUNLOCK
return i;
}

TQItem* TQueue::enqueue_bin(double td, void* d) {
MUTLOCK
STAT(ninsert);
TQItem* i = tpool_->alloc();
i->data_ = d;
i->t_ = td;
binq_->enqueue(td, i);
MUTUNLOCK
return i;
}

Expand All @@ -330,7 +316,6 @@ void TQueue::release(TQItem* q) {
}

void TQueue::remove(TQItem* q) {
MUTLOCK
STAT(nrem);
if (q) {
if (q == least_) {
Expand All @@ -346,12 +331,10 @@ void TQueue::remove(TQItem* q) {
}
tpool_->hpfree(q);
}
MUTUNLOCK
}

TQItem* TQueue::atomic_dq(double tt) {
TQItem* q = 0;
MUTLOCK
if (least_ && least_->t_ <= tt) {
q = least_;
STAT(nrem);
Expand All @@ -361,21 +344,18 @@ TQItem* TQueue::atomic_dq(double tt) {
least_ = nullptr;
}
}
MUTUNLOCK
return q;
}

TQItem* TQueue::find(double t) {
TQItem* q;
MUTLOCK
// search only in the splay tree. if this is a bug then fix it.
STAT(nfind)
if (t == least_t_nolock()) {
q = least();
} else {
q = splookup(t, sptree_);
}
MUTUNLOCK
return (q);
}

Expand Down Expand Up @@ -501,17 +481,14 @@ void BinQ::remove(TQItem* q) {
}
}

SelfQueue::SelfQueue(TQItemPool* tp, int mkmut) {
MUTCONSTRUCT(mkmut)
SelfQueue::SelfQueue(TQItemPool* tp) {
tpool_ = tp;
head_ = nullptr;
}
SelfQueue::~SelfQueue() {
remove_all();
MUTDESTRUCT
}
TQItem* SelfQueue::insert(void* d) {
MUTLOCK
TQItem* q = tpool_->alloc();
q->left_ = nullptr;
q->right_ = head_;
Expand All @@ -520,11 +497,9 @@ TQItem* SelfQueue::insert(void* d) {
}
head_ = q;
q->data_ = d;
MUTUNLOCK
return q;
}
void* SelfQueue::remove(TQItem* q) {
MUTLOCK
if (q->left_) {
q->left_->right_ = q->right_;
}
Expand All @@ -535,14 +510,11 @@ void* SelfQueue::remove(TQItem* q) {
head_ = q->right_;
}
tpool_->hpfree(q);
MUTUNLOCK
return q->data_;
}
void SelfQueue::remove_all() {
MUTLOCK
for (TQItem* q = first(); q; q = next(q)) {
tpool_->hpfree(q);
}
head_ = nullptr;
MUTUNLOCK
}
21 changes: 2 additions & 19 deletions src/nrncvode/tqueue.hpp
Expand Up @@ -4,7 +4,6 @@

#include <assert.h>

#include <nrnmutdec.h>
#include <pool.hpp>

class TQItem;
Expand Down Expand Up @@ -77,34 +76,20 @@ class BinQ {

class TQueue {
public:
TQueue(TQItemPool*, int mkmut = 0);
TQueue(TQItemPool*);
virtual ~TQueue();

TQItem* least() {
return least_;
}
TQItem* second_least(double t);
#if NRN_ENABLE_THREADS
double least_t() {
double tt;
MUTLOCK;
if (least_) {
tt = least_->t_;
} else {
tt = 1e15;
}
MUTUNLOCK;
return tt;
}
#else
double least_t() {
if (least_) {
return least_->t_;
} else {
return 1e15;
}
}
#endif
TQItem* atomic_dq(double til);
TQItem* insert(double t, void* data);
TQItem* enqueue_bin(double t, void* data);
Expand Down Expand Up @@ -152,7 +137,6 @@ class TQueue {
BinQ* binq_;
TQItem* least_;
TQItemPool* tpool_;
MUTDEC
#if COLLECT_TQueue_STATISTICS
unsigned long ninsert, nrem, nleast, nbal, ncmplxrem;
unsigned long ncompare, nleastsrch, nfind, nfindsrch, nmove, nfastmove;
Expand All @@ -161,7 +145,7 @@ class TQueue {

class SelfQueue { // not really a queue but a doubly linked list for fast
public: // insertion, deletion, iteration
SelfQueue(TQItemPool*, int mkmut = 0);
SelfQueue(TQItemPool*);
virtual ~SelfQueue();
TQItem* insert(void*);
void* remove(TQItem*);
Expand All @@ -176,5 +160,4 @@ class SelfQueue { // not really a queue but a doubly linked list for fast
private:
TQItem* head_;
TQItemPool* tpool_;
MUTDEC
};

0 comments on commit 12ca064

Please sign in to comment.