-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_server.h
92 lines (68 loc) · 1.97 KB
/
lock_server.h
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
// this is the lock server
// the lock client has a similar interface
#ifndef lock_server_h
#define lock_server_h
#include <string>
#include "lock_protocol.h"
#include "lock_client.h"
#include "rpc.h"
/** lock_t class
*
* This class represents a single lock on a server. It's purpose to provide
* functions that can safely aquire and release lock in multithreading
* enviroment.
*/
class lock_t {
public:
/// Constructor. Initalizes internal structures and sets "unlocked" state
/// for the lock.
lock_t();
/// Aquires the lock. Pauses thread until lock is released.
void acquire();
/// Releases the lock. Wakes up other thread waiting to aquire this lock.
void release();
/// Returns the current status of the lock.
bool isLocked();
private:
bool locked;
pthread_cond_t okToLock;
pthread_mutex_t mutex;
};
/** lock_server class
*
* This class provides services to clients via RPC. Current implementation
* provides basic lock managment.
*/
class lock_server {
protected:
int nacquire;
std::map<lock_protocol::lockid_t, lock_t> locks;
public:
lock_server();
~lock_server() {};
/** Get status of a certain lock
*
* @param clt Client ID
* @param lid Lock ID
* @return Execution status of the RPC function. Indicates success or
* failure code.
*/
lock_protocol::status stat(int clt, lock_protocol::lockid_t lid, int &);
/** Aquire certain lock
*
* @param clt Client ID
* @param lid Lock ID
* @return Execution status of the RPC function. Indicates success or
* failure code.
*/
lock_protocol::status acquire(int clt, lock_protocol::lockid_t lid, int &);
/** Release certain lock
*
* @param clt Client ID
* @param lid Lock ID
* @return Execution status of the RPC function. Indicates success or
* failure code.
*/
lock_protocol::status release(int clt, lock_protocol::lockid_t lid, int &);
};
#endif