This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathnotifiable_task.cpp
59 lines (51 loc) · 1.65 KB
/
notifiable_task.cpp
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
//===----------------------------------------------------------------------===//
//
// Peloton
//
// notifiable_task.cpp
//
// Identification: src/common/notifiable_task.cpp
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "common/notifiable_task.h"
#include "common/logger.h"
#include "common/event_util.h"
#include <cstring>
namespace peloton {
NotifiableTask::NotifiableTask(size_t task_id) : task_id_(task_id) {
base_ = EventUtil::EventBaseNew();
// For exiting a loop
terminate_ = RegisterManualEvent([](int, short, void *arg) {
EventUtil::EventBaseLoopExit((struct event_base *) arg, nullptr);
}, base_);
};
NotifiableTask::~NotifiableTask() {
for (struct event *event : events_) {
EventUtil::EventDel(event);
event_free(event);
}
event_base_free(base_);
}
struct event *NotifiableTask::RegisterEvent(int fd,
short flags,
event_callback_fn callback,
void *arg,
const struct timeval *timeout) {
struct event *event = event_new(base_, fd, flags, callback, arg);
events_.insert(event);
EventUtil::EventAdd(event, timeout);
return event;
}
void NotifiableTask::UnregisterEvent(struct event *event) {
auto it = events_.find(event);
if (it == events_.end()) return;
if (event_del(event) == -1) {
LOG_ERROR("Failed to delete event");
return;
}
event_free(event);
events_.erase(event);
}
} // namespace peloton