Imported from 6.824 labs
[invirt/third/libt4.git] / lock_server.cc
1 // the lock server implementation
2
3 #include "lock_server.h"
4 #include <sstream>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <arpa/inet.h>
8
9 lock_server::lock_server():
10     nacquire (0)
11 {
12 }
13
14 // caller must hold lock_lock
15 mutex &
16 lock_server::get_lock(lock_protocol::lockid_t lid) {
17     lock_lock.acquire();
18     // by the semantics of std::map, this will create
19     // the mutex if it doesn't already exist
20     mutex &l = locks[lid];
21     lock_lock.release();
22     return l;
23 }
24
25 lock_protocol::status
26 lock_server::stat(int clt, lock_protocol::lockid_t lid, int &r)
27 {
28     lock_protocol::status ret = lock_protocol::OK;
29     printf("stat request from clt %d\n", clt);
30     r = nacquire;
31     return ret;
32 }
33
34 lock_protocol::status
35 lock_server::acquire(int clt, lock_protocol::lockid_t lid, int &r)
36 {
37     get_lock(lid).acquire();
38     return lock_protocol::OK;
39 }
40
41 lock_protocol::status
42 lock_server::release(int clt, lock_protocol::lockid_t lid, int &r)
43 {
44     get_lock(lid).release();
45     return lock_protocol::OK;
46 }