-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle.cc
87 lines (79 loc) · 1.92 KB
/
handle.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
#include "handle.h"
#include <stdio.h>
handle_mgr mgr;
handle::handle(std::string m)
{
h = mgr.get_handle(m);
}
handle::~handle()
{
if (h != 0) mgr.done_handle(h);
}
handle_mgr::handle_mgr()
{
assert (pthread_mutex_init(&handle_mutex, NULL) == 0);
}
struct hinfo *
handle_mgr::get_handle(std::string m)
{
int ret;
assert(pthread_mutex_lock(&handle_mutex)==0);
rpcc *cl = 0;
struct hinfo *h = 0;
if (hmap.find(m) == hmap.end()) {
sockaddr_in dstsock;
make_sockaddr(m.c_str(), &dstsock);
cl = new rpcc(dstsock);
printf("paxos::get_handle trying to bind...%s\n", m.c_str());
ret = cl->bind(rpcc::to(1000));
if (ret < 0) {
printf("handle_mgr::get_handle bind failure! %s %d\n", m.c_str(), ret);
} else {
printf("handle_mgr::get_handle bind succeeded %s\n", m.c_str());
hmap[m].cl = cl;
hmap[m].refcnt = 1;
hmap[m].del = false;
hmap[m].m = m;
h = &hmap[m];
}
} else if (!hmap[m].del) {
hmap[m].refcnt++;
h = &hmap[m];
}
assert(pthread_mutex_unlock(&handle_mutex)==0);
return h;
}
void
handle_mgr::done_handle(struct hinfo *h)
{
assert(pthread_mutex_lock(&handle_mutex)==0);
h->refcnt--;
if (h->refcnt <= 0 && h->del)
delete_handle_wo(h->m);
assert(pthread_mutex_unlock(&handle_mutex)==0);
}
void
handle_mgr::delete_handle(std::string m)
{
assert(pthread_mutex_lock(&handle_mutex)==0);
delete_handle_wo(m);
assert(pthread_mutex_unlock(&handle_mutex)==0);
}
// Must be called with handle_mutex locked.
void
handle_mgr::delete_handle_wo(std::string m)
{
if (hmap.find(m) == hmap.end()) {
printf("handle_mgr::delete_handle_wo: cl %s isn't in cl list\n", m.c_str());
} else {
printf("handle_mgr::delete_handle_wo: cl %s refcnt %d\n", m.c_str(),
hmap[m].refcnt);
if (hmap[m].refcnt == 0) {
hmap[m].cl->cancel();
delete hmap[m].cl;
hmap.erase(m);
} else {
hmap[m].del = true;
}
}
}