Skip to content

Commit

Permalink
redundancies
Browse files Browse the repository at this point in the history
  • Loading branch information
kentslaney committed Dec 21, 2023
1 parent 07964a8 commit 550ef71
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
22 changes: 4 additions & 18 deletions include/LockPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@
#include <queue>
#include <deque>
#include <vector>
#include <atomic>
#include <memory>
#include <list>
#include <algorithm>

#include <cstdarg>
#include <unistd.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <assert.h>

namespace douban {
namespace mc {

// https://stackoverflow.com/a/14792685/3476782
class OrderedLock {
std::list<std::condition_variable*> m_fifo_locks;
std::queue<std::condition_variable*> m_fifo_locks;
protected:
std::mutex m_fifo_access;
bool m_locked;
Expand All @@ -32,10 +22,9 @@ class OrderedLock {
std::unique_lock<std::mutex> acquire(m_fifo_access);
if (m_locked) {
std::condition_variable signal;
m_fifo_locks.emplace_back(&signal);
m_fifo_locks.emplace(&signal);
signal.wait(acquire);
m_fifo_locks.pop_front();
assert(acquire.owns_lock());
m_fifo_locks.pop();
} else {
m_locked = true;
}
Expand Down Expand Up @@ -86,9 +75,7 @@ class LockPool : public OrderedLock {
std::transform(
muxes, muxes + n, std::back_inserter(m_thread_workers),
static_cast<std::mutex*(*)(std::mutex&)>(std::addressof<std::mutex>));
for (int i = n; i > 0; i--) {
unlock();
}
unlock();
}

int acquireWorker() {
Expand All @@ -98,7 +85,6 @@ class LockPool : public OrderedLock {
if (!m_available.empty()) {
unlock();
}
assert(m_available.size() <= m_thread_workers.size());
return res;
}

Expand Down
1 change: 1 addition & 0 deletions src/ClientPool.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//#include <execution>
//#include <thread>
#include <atomic>
#include <array>
#include "ClientPool.h"

Expand Down
24 changes: 12 additions & 12 deletions tests/test_client_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ TEST(test_client_pool, simple_set_get) {
for (unsigned int j = 0; j < n_ops; j++) {
gen_random(key, data_size);
gen_random(value, data_size);
auto c = pool->_acquire();
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);
auto c = pool->acquire();
c->set(&keys, data_lens, flags, exptime, NULL, 0, &values, data_lens, 1, &m_results, &nResults);
c->destroyMessageResult();
c->get(&keys, data_lens, 1, &r_results, &nResults);
ASSERT_N_STREQ(r_results[0]->data_block, values, data_size);
c->c.destroyRetrievalResult();
pool->_release(c);
c->destroyRetrievalResult();
pool->release(c);
}

delete pool;
Expand Down Expand Up @@ -81,13 +81,13 @@ TEST(test_client_pool, threaded_set_get) {
for (unsigned int j = 0; j < n_ops; j++) {
gen_random(key, data_size);
gen_random(value, data_size);
auto c = pool->_acquire();
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);
auto c = pool->acquire();
c->set(&keys, data_lens, flags, exptime, NULL, 0, &values, data_lens, 1, &m_results, &nResults);
c->destroyMessageResult();
c->get(&keys, data_lens, 1, &r_results, &nResults);
ASSERT_N_STREQ(r_results[0]->data_block, values, data_size);
c->c.destroyRetrievalResult();
pool->_release(c);
c->destroyRetrievalResult();
pool->release(c);
}
});
}
Expand Down

0 comments on commit 550ef71

Please sign in to comment.