Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
events.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef PLASMA_EVENTS
19 #define PLASMA_EVENTS
20 
21 #include <functional>
22 #include <memory>
23 #include <unordered_map>
24 
25 extern "C" {
26 #include "ae/ae.h"
27 }
28 
29 namespace plasma {
30 
33 
35 constexpr int kEventLoopRead = AE_READABLE;
36 
38 constexpr int kEventLoopWrite = AE_WRITABLE;
39 
40 typedef long long TimerID; // NOLINT
41 
42 class EventLoop {
43  public:
44  // Signature of the handler that will be called when there is a new event
45  // on the file descriptor that this handler has been registered for.
46  //
47  // The arguments are the event flags (read or write).
48  using FileCallback = std::function<void(int)>;
49 
50  // This handler will be called when a timer times out. The timer id is
51  // passed as an argument. The return is the number of milliseconds the timer
52  // shall be reset to or kEventLoopTimerDone if the timer shall not be
53  // triggered again.
54  using TimerCallback = std::function<int(int64_t)>;
55 
56  EventLoop();
57 
64  bool AddFileEvent(int fd, int events, const FileCallback& callback);
65 
69  void RemoveFileEvent(int fd);
70 
77  int64_t AddTimer(int64_t timeout, const TimerCallback& callback);
78 
83  int RemoveTimer(int64_t timer_id);
84 
86  void Start();
87 
89  void Stop();
90 
91  private:
92  static void FileEventCallback(aeEventLoop* loop, int fd, void* context, int events);
93 
94  static int TimerEventCallback(aeEventLoop* loop, TimerID timer_id, void* context);
95 
96  aeEventLoop* loop_;
97  std::unordered_map<int, std::unique_ptr<FileCallback>> file_callbacks_;
98  std::unordered_map<int64_t, std::unique_ptr<TimerCallback>> timer_callbacks_;
99 };
100 
101 } // namespace plasma
102 
103 #endif // PLASMA_EVENTS
int64_t AddTimer(int64_t timeout, const TimerCallback &callback)
Register a handler that will be called after a time slice of "timeout" milliseconds.
void Start()
Run the event loop.
long long TimerID
Definition: events.h:40
constexpr int kEventLoopWrite
Write event on the file descriptor.
Definition: events.h:38
#define AE_READABLE
Definition: ae.h:42
#define AE_NOMORE
Definition: ae.h:50
constexpr int kEventLoopTimerDone
Constant specifying that the timer is done and it will be removed.
Definition: events.h:32
std::function< void(int)> FileCallback
Definition: events.h:48
Definition: events.h:42
std::function< int(int64_t)> TimerCallback
Definition: events.h:54
Definition: client.h:35
void Stop()
Stop the event loop.
void RemoveFileEvent(int fd)
Remove a file event handler from the event loop.
bool AddFileEvent(int fd, int events, const FileCallback &callback)
Add a new file event handler to the event loop.
constexpr int kEventLoopRead
Read event on the file descriptor.
Definition: events.h:35
Definition: ae.h:90
#define AE_WRITABLE
Definition: ae.h:43
int RemoveTimer(int64_t timer_id)
Remove a timer handler from the event loop.