-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_server.cc
102 lines (73 loc) · 1.95 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// the lock server implementation
#include "lock_server.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
// Implementation of lock_t class
lock_t::lock_t()
: locked(false)
{
// initialize condition var
pthread_cond_init(&okToLock, NULL);
// initialize mutex
pthread_mutex_init(&mutex, NULL);
}
void lock_t::acquire()
{
pthread_mutex_lock(&mutex);
// wait until lock releases
while (locked)
pthread_cond_wait(&okToLock, &mutex);
// lock it again
locked = true;
pthread_mutex_unlock(&mutex);
}
void lock_t::release()
{
pthread_mutex_lock(&mutex);
// release current lock
locked = false;
pthread_mutex_unlock(&mutex);
// notify one of the waiting threads
pthread_cond_signal(&okToLock);
}
bool lock_t::isLocked()
{
pthread_mutex_lock(&mutex);
// copy lock status into a local var
bool isLocked = locked;
pthread_mutex_unlock(&mutex);
// return lock status
return isLocked;
}
// Implementation of lock_server class
lock_server::lock_server():
nacquire (0)
{
}
lock_protocol::status
lock_server::stat(int clt, lock_protocol::lockid_t lid, int &r)
{
printf("server: requested status of lock %llu\n", lid);
// locks[lid] is created automatically when accessing first time
// return should be 1 (locked) or 0 (unlocked)
r = locks[lid].isLocked() ? 1 : 0;
return lock_protocol::OK;
}
lock_protocol::status
lock_server::acquire(int clt, lock_protocol::lockid_t lid, int &)
{
printf("server: acquired lock %llu\n", lid);
// locks[lid] is created automatically when accessing first time
locks[lid].acquire();
return lock_protocol::OK;
}
lock_protocol::status
lock_server::release(int clt, lock_protocol::lockid_t lid, int &)
{
printf("server: released lock %llu\n", lid);
// locks[lid] is created automatically when accessing first time
locks[lid].release();
return lock_protocol::OK;
}