Skip to content

Commit

Permalink
Stylistic improvements for MutexPool. (#2556)
Browse files Browse the repository at this point in the history
  • Loading branch information
1uc authored Oct 2, 2023
1 parent cdc2e6d commit 5868cdd
Showing 1 changed file with 20 additions and 41 deletions.
61 changes: 20 additions & 41 deletions src/nrncvode/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ MutexPool<T>::MutexPool(long count, int mkmut) {
pool_ = new T[count_];
pool_size_ = count;
items_ = new T*[count_];
{
for (long i = 0; i < count_; ++i)
items_[i] = pool_ + i;
for (long i = 0; i < count_; ++i) {
items_[i] = pool_ + i;
}
MUTCONSTRUCT(mkmut)
}
Expand All @@ -62,22 +61,15 @@ void MutexPool<T>::grow() {
chain_ = p;
long newcnt = 2 * count_;
T** itms = new T*[newcnt];
long i, j;
put_ += count_;
{
for (i = 0; i < get_; ++i) {
itms[i] = items_[i];
}
for (long i = 0; i < get_; ++i) {
itms[i] = items_[i];
}
{
for (i = get_, j = 0; j < count_; ++i, ++j) {
itms[i] = p->items_[j];
}
for (long i = get_, j = 0; j < count_; ++i, ++j) {
itms[i] = p->items_[j];
}
{
for (i = put_, j = get_; j < count_; ++i, ++j) {
itms[i] = items_[j];
}
for (long i = put_, j = get_; j < count_; ++i, ++j) {
itms[i] = items_[j];
}
delete[] items_;
delete[] p->items_;
Expand All @@ -88,35 +80,24 @@ void MutexPool<T>::grow() {

template <typename T>
MutexPool<T>::~MutexPool() {
{
if (chain_) {
delete chain_;
}
}
delete chain_;
delete[] pool_;
{
if (items_) {
delete[] items_;
}
}
delete[] items_;
MUTDESTRUCT
}

template <typename T>
T* MutexPool<T>::alloc() {
MUTLOCK {
if (nget_ >= count_) {
grow();
}
MUTLOCK
if (nget_ >= count_) {
grow();
}
T* item = items_[get_];
get_ = (get_ + 1) % count_;
++nget_;
{
if (nget_ > maxget_) {
maxget_ = nget_;
}
}

maxget_ = std::max(nget_, maxget_);

MUTUNLOCK
return item;
}
Expand All @@ -139,12 +120,10 @@ void MutexPool<T>::free_all() {
nget_ = 0;
get_ = 0;
put_ = 0;
{
for (pp = this; pp; pp = pp->chain_) {
for (i = 0; i < pp->pool_size_; ++i) {
items_[put_++] = pp->pool_ + i;
pp->pool_[i].clear();
}
for (pp = this; pp; pp = pp->chain_) {
for (i = 0; i < pp->pool_size_; ++i) {
items_[put_++] = pp->pool_ + i;
pp->pool_[i].clear();
}
}
assert(put_ == count_);
Expand Down

0 comments on commit 5868cdd

Please sign in to comment.