forked from Juniper/contrail-third-party
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiredis-asio-patch1.diff
106 lines (91 loc) · 3.52 KB
/
hiredis-asio-patch1.diff
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
101
102
103
104
105
106
diff --git a/third_party/hiredis-boostasio-adapter/boostasio.cpp b/third_party/hiredis-boostasio-adapter/boostasio.cpp
index 2be99ad..b123fb5 100644
--- a/third_party/hiredis-boostasio-adapter/boostasio.cpp
+++ b/third_party/hiredis-boostasio-adapter/boostasio.cpp
@@ -1,8 +1,11 @@
#include "boostasio.hpp"
+#include <tbb/mutex.h>
-redisBoostClient::redisBoostClient(boost::asio::io_service& io_service,redisAsyncContext *ac)
-: socket_(io_service)
+
+redisBoostClient::redisBoostClient(boost::asio::io_service& io_service,
+ redisAsyncContext *ac, tbb::mutex& mtex)
+: socket_(io_service), mtex_(mtex), read_requested_(false), write_requested_(false), read_in_progress_(false), write_in_progress_(false), cleanup_(false)
{
/*this gives us access to c->fd*/
@@ -39,18 +42,21 @@ void redisBoostClient::operate()
if(read_requested_ && !read_in_progress_) {
read_in_progress_ = true;
socket_.async_read_some(boost::asio::null_buffers(),
- boost::bind(&redisBoostClient::handle_read,this,boost::asio::placeholders::error));
+ boost::bind(&redisBoostClient::handle_read,shared_from_this(),boost::asio::placeholders::error));
}
if(write_requested_ && !write_in_progress_) {
write_in_progress_ = true;
socket_.async_write_some(boost::asio::null_buffers(),
- boost::bind(&redisBoostClient::handle_write,this,boost::asio::placeholders::error));
+ boost::bind(&redisBoostClient::handle_write,shared_from_this(),boost::asio::placeholders::error));
}
}
void redisBoostClient::handle_read(boost::system::error_code ec)
{
+ if (cleanup_) return;
+
+ tbb::mutex::scoped_lock lock(mtex_);
read_in_progress_ = false;
if(!ec) {
redisAsyncHandleRead(context_);
@@ -62,6 +68,9 @@ void redisBoostClient::handle_read(boost::system::error_code ec)
void redisBoostClient::handle_write(boost::system::error_code ec)
{
+ if (cleanup_) return;
+
+ tbb::mutex::scoped_lock lock(mtex_);
write_in_progress_ = false;
if(!ec) {
redisAsyncHandleWrite(context_);
@@ -96,7 +105,11 @@ void redisBoostClient::del_write(void *privdata)
void redisBoostClient::cleanup(void *privdata)
{
/*Do I even need this?*/
- printf("cleanup called...\n");
+ read_requested_ = false;
+ write_requested_ = false;
+ read_in_progress_ = false;
+ write_in_progress_ = false;
+ cleanup_ = true;
}
/*wrappers*/
diff --git a/third_party/hiredis-boostasio-adapter/boostasio.hpp b/third_party/hiredis-boostasio-adapter/boostasio.hpp
index c24140a..8ae5cc0 100644
--- a/third_party/hiredis-boostasio-adapter/boostasio.hpp
+++ b/third_party/hiredis-boostasio-adapter/boostasio.hpp
@@ -10,13 +10,18 @@
#include <boost/asio.hpp>
#include <boost/bind.hpp>
+#include <boost/enable_shared_from_this.hpp>
+
+#include <tbb/mutex.h>
using boost::asio::ip::tcp;
-class redisBoostClient
+class redisBoostClient : public boost::enable_shared_from_this<redisBoostClient>
{
public:
- redisBoostClient(boost::asio::io_service& io_service,redisAsyncContext *ac);
+ redisBoostClient(boost::asio::io_service& io_service,
+ redisAsyncContext *ac,
+ tbb::mutex &mtex);
void operate();
@@ -29,12 +34,14 @@ public:
void cleanup(void *privdata);
private:
-redisAsyncContext *context_;
boost::asio::ip::tcp::socket socket_;
+tbb::mutex &mtex_;
+redisAsyncContext *context_;
bool read_requested_;
bool write_requested_;
bool read_in_progress_;
bool write_in_progress_;
+bool cleanup_;
};