Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of mutex in TQueue and SelfEvent #2758

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
33 changes: 3 additions & 30 deletions src/nrncvode/tqueue.cpp
Expand Up @@ -51,8 +51,8 @@ static void prnt(const TQItem* b, int level) {
Printf("%g %c %d Q=%p D=%p\n", b->t_, b->data_ ? 'x' : 'o', b->cnt_, b, b->data_);
}

TQueue::TQueue(TQItemPool* tp, int mkmut) {
MUTCONSTRUCT(mkmut)

TQueue::TQueue(TQItemPool* tp) {
tpool_ = tp;
nshift_ = 0;
sptree_ = new SPTREE<TQItem>;
Expand All @@ -77,35 +77,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 @@ -122,9 +117,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 @@ -142,7 +135,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 @@ -156,7 +148,6 @@ void TQueue::move(TQItem* i, double tnew) {
i->t_ = tnew;
spenq(i, sptree_);
}
MUTUNLOCK
}

void TQueue::statistics() {
Expand Down Expand Up @@ -184,7 +175,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 @@ -198,18 +188,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 @@ -219,7 +206,6 @@ void TQueue::release(TQItem* q) {
}

void TQueue::remove(TQItem* q) {
MUTLOCK
STAT(nrem);
if (q) {
if (q == least_) {
Expand All @@ -235,12 +221,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 @@ -250,21 +234,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 @@ -390,17 +371,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 @@ -409,11 +387,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 @@ -424,14 +400,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>

#include "tqitem.hpp"
Expand Down Expand Up @@ -62,34 +61,20 @@ class BinQ {

class TQueue {
public:
TQueue(TQItemPool*, int mkmut = 0);
explicit 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 @@ -137,7 +122,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 @@ -146,7 +130,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);
explicit SelfQueue(TQItemPool*);
virtual ~SelfQueue();
TQItem* insert(void*);
void* remove(TQItem*);
Expand All @@ -161,5 +145,4 @@ class SelfQueue { // not really a queue but a doubly linked list for fast
private:
TQItem* head_;
TQItemPool* tpool_;
MUTDEC
};