Skip to content

Commit

Permalink
lab 4 - ok
Browse files Browse the repository at this point in the history
  • Loading branch information
phlalx committed May 21, 2017
1 parent 709e34a commit b401267
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
38 changes: 32 additions & 6 deletions lock_client_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@
#include "tprintf.h"
#include "jsl_log.h"

const char *
lock_client_cache::lock_info::to_string() {
switch(st){
case ACQUIRING: return "ACQUIRING";
case RELEASING: return "RELEASING";
case FREE: return "FREE";
case NONE: return "NONE";
case LOCKED: return "LOCKED";
}
return "--";
}

void
lock_client_cache::lock_info::set(lock_client_cache *lcc, lock_protocol::lockid_t lid, State new_st) {
jsl_log(JSL_DBG_ME, "lock_client_cache %s %lud %llu: ", lcc->id.c_str(), pthread_self(), lid);
jsl_log(JSL_DBG_ME, "%s -> ", to_string());
st = new_st;
jsl_log(JSL_DBG_ME, "%s (# = %d)\n", to_string(), number_waiting);
if (st == FREE || st == NONE) {
is_revoked = false;
is_retried = false;
}
}

lock_client_cache::lock_client_cache(std::string xdst,
class lock_release_user *_lu)
: lock_client(xdst), lu(_lu)
Expand All @@ -33,16 +57,15 @@ lock_client_cache::acquire(lock_protocol::lockid_t lid)
jsl_log(JSL_DBG_ME, "lock_client_cache %s %lud %llu: acquire\n", id.c_str(), pthread_self(), lid);
lock_info &li = locks[lid];

while (li.st == ACQUIRING || li.st == RELEASING || li.st == LOCKED) {
pthread_cond_wait(&li.waiting_local, &mutex);
}

if (li.st == FREE) {
li.set(this, lid, LOCKED);
return ret;
}

if (li.st != NONE) {
// For now, suppose there's only one client thread
// meaning li.st == NONE or FREE
VERIFY(0);
}
VERIFY(li.st == NONE);
li.set(this, lid, ACQUIRING);
}

Expand Down Expand Up @@ -83,6 +106,9 @@ lock_client_cache::release(lock_protocol::lockid_t lid)
ScopedLock ml(&mutex);
jsl_log(JSL_DBG_ME, "lock_client_cache %s %lud %llu: release\n", id.c_str(), pthread_self(), lid);
lock_info &li = locks[lid];

pthread_cond_signal(&li.waiting_local); // not always necessary

if (li.st == LOCKED) {
li.set(this, lid, FREE);
return ret;
Expand Down
29 changes: 5 additions & 24 deletions lock_client_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,21 @@ class lock_client_cache : public lock_client {
struct lock_info {

State st;
pthread_cond_t waiting_from_server; // acquire thread
pthread_cond_t waiting_local; // acquire thread
pthread_cond_t waiting_retry; // client thread

bool is_revoked;
bool is_retried;

const char *to_string() {
switch(st){
case ACQUIRING: return "ACQUIRING";
case RELEASING: return "RELEASING";
case FREE: return "FREE";
case NONE: return "NONE";
case LOCKED: return "LOCKED";
}
return "--";
}

void set(lock_client_cache *lcc, lock_protocol::lockid_t lid, State new_st) {
jsl_log(JSL_DBG_ME, "lock_client_cache %s %lud %llu: ", lcc->id.c_str(), pthread_self(), lid);
jsl_log(JSL_DBG_ME, "%s -> ", to_string());
st = new_st;
jsl_log(JSL_DBG_ME, "%s (# = %d)\n", to_string(), number_waiting);
if (st == FREE || st == NONE) {
is_revoked = false;
is_retried = false;
}
}

int number_waiting;

const char * to_string();
void set(lock_client_cache *lcc, lock_protocol::lockid_t lid, State new_st);

lock_info() {
number_waiting = 0;
st = NONE;
waiting_from_server = PTHREAD_COND_INITIALIZER;
waiting_local = PTHREAD_COND_INITIALIZER;
waiting_retry = PTHREAD_COND_INITIALIZER;
is_revoked = false;
is_retried = false;
Expand Down

0 comments on commit b401267

Please sign in to comment.