4
4
//
5
5
// notifiable_task.h
6
6
//
7
- // Identification: src/include/network /notifiable_task.h
7
+ // Identification: src/include/common /notifiable_task.h
8
8
//
9
9
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
13
13
#pragma once
14
14
15
- #include < csignal>
16
- #include < cstdio>
17
- #include < cstdlib>
18
- #include < cstring>
19
- #include < vector>
20
15
#include < unordered_set>
21
-
22
- #include < sys/file.h>
23
16
#include < event2/thread.h>
24
-
25
- #include " common/exception.h"
26
- #include " common/logger.h"
27
- #include " network_state.h"
28
- #include " error_util.h"
17
+ #include " common/event_util.h"
29
18
30
19
namespace peloton {
31
- namespace network {
32
20
33
21
/* *
34
22
* Convenient MACRO to use a method as a libevent callback function. Example
@@ -61,29 +49,13 @@ class NotifiableTask {
61
49
* Constructs a new NotifiableTask instance.
62
50
* @param task_id a unique id assigned to this task
63
51
*/
64
- explicit NotifiableTask (int task_id) : task_id_(task_id) {
65
- base_ = EventUtil::EventBaseNew ();
66
- // TODO(tianyu) Determine whether we actually need this line. Tianyi says we
67
- // need it, libevent documentation says no
68
- // evthread_make_base_notifiable(base_);
69
-
70
- // For exiting a loop
71
- terminate_ = RegisterManualEvent ([](int , short , void *arg) {
72
- EventUtil::EventBaseLoopExit ((struct event_base *)arg, nullptr );
73
- }, base_);
74
- };
52
+ explicit NotifiableTask (int task_id);
75
53
76
54
/* *
77
- * Destructs this NotifiableTask. All events currently registered to its base
78
- * are also deleted and freed.
79
- */
80
- virtual ~NotifiableTask () {
81
- for (struct event *event : events_) {
82
- EventUtil::EventDel (event);
83
- event_free (event);
84
- }
85
- event_base_free (base_);
86
- }
55
+ * Destructs this NotifiableTask. All events currently registered to its base
56
+ * are also deleted and freed.
57
+ */
58
+ virtual ~NotifiableTask ();
87
59
88
60
/* *
89
61
* @return unique id assigned to this task
@@ -114,13 +86,7 @@ class NotifiableTask {
114
86
*/
115
87
struct event *RegisterEvent (int fd, short flags, event_callback_fn callback,
116
88
void *arg,
117
- const struct timeval *timeout = nullptr ) {
118
- struct event *event = event_new (base_, fd, flags, callback, arg);
119
- events_.insert (event);
120
- EventUtil::EventAdd (event, timeout);
121
- return event;
122
- }
123
-
89
+ const struct timeval *timeout = nullptr );
124
90
/* *
125
91
* @brief Register a signal event. This is a wrapper around RegisterEvent()
126
92
*
@@ -133,8 +99,9 @@ class NotifiableTask {
133
99
* null which will wait forever
134
100
* @return pointer to the allocated event.
135
101
*/
136
- struct event *RegisterSignalEvent (int signal, event_callback_fn callback,
137
- void *arg) {
102
+ inline struct event *RegisterSignalEvent (int signal,
103
+ event_callback_fn callback,
104
+ void *arg) {
138
105
return RegisterEvent (signal, EV_SIGNAL | EV_PERSIST, callback, arg);
139
106
}
140
107
@@ -150,8 +117,9 @@ class NotifiableTask {
150
117
* @param arg an argument to be passed to the callback function
151
118
* @return pointer to the allocated event.
152
119
*/
153
- struct event *RegisterPeriodicEvent (const struct timeval *timeout,
154
- event_callback_fn callback, void *arg) {
120
+ inline struct event *RegisterPeriodicEvent (const struct timeval *timeout,
121
+ event_callback_fn callback,
122
+ void *arg) {
155
123
return RegisterEvent (-1 , EV_TIMEOUT | EV_PERSIST, callback, arg, timeout);
156
124
}
157
125
@@ -166,14 +134,14 @@ class NotifiableTask {
166
134
* @param arg an argument to be passed to the callback function
167
135
* @return pointer to the allocated event.
168
136
*/
169
- struct event *RegisterManualEvent (event_callback_fn callback, void *arg) {
137
+ inline struct event *RegisterManualEvent (event_callback_fn callback,
138
+ void *arg) {
170
139
return RegisterEvent (-1 , EV_PERSIST, callback, arg);
171
140
}
172
141
173
142
// TODO(tianyu): The original network code seems to do this as an
174
143
// optimization. Specifically it avoids new memory allocation by reusing
175
144
// an existing event. I am leaving this out until we get numbers.
176
-
177
145
// void UpdateEvent(struct event *event, int fd, short flags,
178
146
// event_callback_fn callback, void *arg,
179
147
// const struct timeval *timeout = nullptr) {
@@ -198,25 +166,15 @@ class NotifiableTask {
198
166
*
199
167
* @param event the event to be freed
200
168
*/
201
- void UnregisterEvent (struct event *event) {
202
- auto it = events_.find (event);
203
- if (it == events_.end ()) return ;
204
- if (event_del (event) == -1 ) {
205
- LOG_ERROR (" Failed to delete event" );
206
- return ;
207
- }
208
- event_free (event);
209
- events_.erase (event);
210
- }
169
+ void UnregisterEvent (struct event *event);
211
170
212
171
/* *
213
172
* In a loop, make this notifiable task wait and respond to incoming events
214
173
*/
215
- void EventLoop () {
174
+ inline void EventLoop () {
216
175
EventUtil::EventBaseDispatch (base_);
217
176
LOG_TRACE (" stop" );
218
177
}
219
-
220
178
/* *
221
179
* Exits the event loop
222
180
*/
@@ -225,7 +183,7 @@ class NotifiableTask {
225
183
/* *
226
184
* Wrapper around ExitLoop() to conform to libevent callback signature
227
185
*/
228
- void ExitLoop (int , short ) { ExitLoop (); }
186
+ inline void ExitLoop (int , short ) { ExitLoop (); }
229
187
230
188
private:
231
189
const int task_id_;
@@ -236,5 +194,4 @@ class NotifiableTask {
236
194
std::unordered_set<struct event *> events_;
237
195
};
238
196
239
- } // namespace network
240
197
} // namespace peloton
0 commit comments