-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_server.cc
59 lines (53 loc) · 1.46 KB
/
lock_server.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// the lock server implementation
#include "lock_server.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
lock_server::lock_server():
nacquire (0)
{
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
}
lock_protocol::status
lock_server::stat(int clt, lock_protocol::lockid_t lid, int &r)
{
lock_protocol::status ret = lock_protocol::OK;
printf("stat request from clt %d\n", clt);
r = nacquire;
return ret;
}
lock_protocol::status lock_server::acquire(int clt, lock_protocol::lockid_t lid, int &r)
{
lock_protocol::status ret = lock_protocol::OK;
// Your lab4 code goes here
pthread_mutex_lock(&mutex);
if (locktable.find(lid) != locktable.end()) {
while(locktable[lid] == true){
pthread_cond_wait(&cond, &mutex);
}
locktable[lid] = true;
}
else {
locktable.insert(std::pair<lock_protocol::lockid_t, bool>(lid, true));
}
pthread_mutex_unlock(&mutex);
return ret;
}
lock_protocol::status lock_server::release(int clt, lock_protocol::lockid_t lid, int &r)
{
lock_protocol::status ret = lock_protocol::OK;
pthread_mutex_lock(&mutex);
// Your lab4 code goes here
if (locktable.find(lid) != locktable.end()) {
locktable[lid] = false;
pthread_cond_signal(&cond);
}
else {
pthread_mutex_unlock(&mutex);
return lock_protocol::NOENT;
}
pthread_mutex_unlock(&mutex);
return ret;
}