#include <arpa/inet.h>
void lock_state::wait(lock & mutex_lock) {
- auto self = this_thread::get_id();
+ auto self = std::this_thread::get_id();
c[self].wait(mutex_lock);
c.erase(self);
}
c[who].notify_one();
}
-typedef map<lock_protocol::lockid_t, lock_state> lock_map;
-
in_port_t lock_client::last_port = 0;
lock_state & lock_client::get_lock_state(lock_protocol::lockid_t lid) {
srandom((uint32_t)time(NULL)^last_port);
rlock_port = ((random()%32000) | (0x1 << 10));
- id = "127.0.0.1:" + to_string(rlock_port);
+ id = "127.0.0.1:" + std::to_string(rlock_port);
last_port = rlock_port;
rlsrpc = unique_ptr<rpcs>(new rpcs(rlock_port));
rlsrpc->reg(rlock_protocol::revoke, &lock_client::revoke_handler, this);
rlsrpc->start();
}
+lock_client::~lock_client() {
+ release_fifo.enq(nothing<lock_protocol::lockid_t>());
+ releaser_thread.join();
+}
+
void lock_client::releaser() {
while (1) {
- lock_protocol::lockid_t lid;
- release_fifo.deq(&lid);
+ maybe<lock_protocol::lockid_t> mlid;
+ release_fifo.deq(&mlid);
+
+ if (!mlid) {
+ LOG << "Releaser stopping";
+ break;
+ }
+
+ lock_protocol::lockid_t lid = mlid;
LOG << "Releaser: " << lid;
lock_state & st = get_lock_state(lid);
lock_protocol::status lock_client::acquire(lock_protocol::lockid_t lid) {
lock_state & st = get_lock_state(lid);
lock sl(st.m);
- auto self = this_thread::get_id();
+ auto self = std::this_thread::get_id();
// check for reentrancy
VERIFY(st.state != lock_state::locked || st.held_by != self);
st.state = lock_state::locked;
st.held_by = releaser_thread.get_id();
LOG << "Queuing " << lid << " for release";
- release_fifo.enq(lid);
+ release_fifo.enq(just(lid));
} else if (front == self) {
st.wanted_by.pop_front();
st.state = lock_state::locked;
lock_protocol::status lock_client::release(lock_protocol::lockid_t lid) {
lock_state & st = get_lock_state(lid);
lock sl(st.m);
- auto self = this_thread::get_id();
+ auto self = std::this_thread::get_id();
VERIFY(st.state == lock_state::locked && st.held_by == self);
st.state = lock_state::free;
LOG << "Lock " << lid << ": free";
st.held_by = releaser_thread.get_id();
st.wanted_by.pop_front();
LOG << "Queuing " << lid << " for release";
- release_fifo.enq(lid);
+ release_fifo.enq(just(lid));
} else
st.signal(front);
}
st.held_by = releaser_thread.get_id();
if (st.wanted_by.size())
st.wanted_by.pop_front();
- release_fifo.enq(lid);
+ release_fifo.enq(just(lid));
} else {
// get in line
st.wanted_by.push_back(releaser_thread.get_id());