Skip to content

Commit

Permalink
well that feels silly after the fact
Browse files Browse the repository at this point in the history
  • Loading branch information
kentslaney committed Dec 21, 2023
1 parent 9ce5327 commit 07964a8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 25 deletions.
28 changes: 6 additions & 22 deletions include/LockPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
namespace douban {
namespace mc {

#define tprintf(str, ...) printf("tid = %lu: " str, syscall(SYS_gettid), ##__VA_ARGS__)

// https://stackoverflow.com/a/14792685/3476782
class OrderedLock {
std::list<std::condition_variable*> m_fifo_locks;
Expand All @@ -34,30 +32,21 @@ class OrderedLock {
std::unique_lock<std::mutex> acquire(m_fifo_access);
if (m_locked) {
std::condition_variable signal;
tprintf("locked %lu already waiting signal %p\n", m_fifo_locks.size(), &signal);
m_fifo_locks.emplace_back(&signal);
signal.wait(acquire);
signal.notify_all();
tprintf("unlocked %lu left waiting signal %p\n", m_fifo_locks.size(), &signal);
m_fifo_locks.pop_front();
assert(acquire.owns_lock());
} else {
m_locked = true;
tprintf("passed through unlocked fifo\n");
}
return acquire;
}

void unlock(std::unique_lock<std::mutex>& acquire, int i = 0) {
void unlock() {
if (m_fifo_locks.empty()) {
tprintf("fifo already empty\n");
m_locked = false;
} else {
std::condition_variable* signal = m_fifo_locks.front();
auto it = m_fifo_locks.begin();
tprintf("unlocking with %d available %lu waiting lock %p\n", i, m_fifo_locks.size(), signal);
signal->notify_one();
signal->wait(acquire);
m_fifo_locks.erase(it);
m_fifo_locks.front()->notify_one();
}
}
};
Expand Down Expand Up @@ -91,37 +80,32 @@ class LockPool : public OrderedLock {
m_mux_mallocs.push_back(muxes);
for (size_t i = 0; i < n; i++) {
m_available.push_back(from + i);
tprintf("pushing available mux %lu\n", from + i);
m_muxes.push_back(&muxes[i]);
}
// static_cast needed for some versions of C++
std::transform(
muxes, muxes + n, std::back_inserter(m_thread_workers),
static_cast<std::mutex*(*)(std::mutex&)>(std::addressof<std::mutex>));
tprintf("available size is now %lu\n", m_available.size());
for (int i = n; i > 0; i--) {
unlock(growing_pool, m_available.size());
unlock();
}
}

int acquireWorker() {
tprintf("acquire called\n");
auto fifo_lock = lock();
const auto res = m_available.front();
m_available.pop_front();
if (!m_available.empty()) {
unlock(fifo_lock, m_available.size());
unlock();
}
tprintf("acquiring %lu; available size is now %lu\n", res, m_available.size());
assert(m_available.size() <= m_thread_workers.size());
return res;
}

void releaseWorker(int worker) {
std::unique_lock<std::mutex> growing_pool(m_fifo_access);
m_available.push_front(worker);
tprintf("releasing %d; available size is now %lu\n", worker, m_available.size());
unlock(growing_pool, m_available.size());
unlock();
}
};

Expand Down
2 changes: 0 additions & 2 deletions src/ClientPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ int ClientPool::growPool(size_t by) {
assert(by > 0);
std::lock_guard growing_pool(m_pool_lock);
size_t from = m_clients.size();
tprintf("growing from %lu by %lu\n", from, by);
m_clients.resize(from + by);
std::atomic<int> rv = 0;
//std::for_each(std::execution::par_unseq, irange(from), irange(from + by),
Expand All @@ -104,7 +103,6 @@ int ClientPool::growPool(size_t by) {
// adds workers with non-zero return values
// if changed, acquire should probably raise rather than hang
addWorkers(by);
tprintf("size is now %lu\n", m_clients.size());
return rv;
}

Expand Down
1 change: 0 additions & 1 deletion tests/test_client_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ TEST(test_client_pool, threaded_set_get) {
gen_random(key, data_size);
gen_random(value, data_size);
auto c = pool->_acquire();
tprintf("acquired client %d\n", c->index);
c->c.set(&keys, data_lens, flags, exptime, NULL, 0, &values, data_lens, 1, &m_results, &nResults);
c->c.destroyMessageResult();
c->c.get(&keys, data_lens, 1, &r_results, &nResults);
Expand Down

0 comments on commit 07964a8

Please sign in to comment.