Apollo 10.0
自动驾驶开放平台
poller.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#ifndef CYBER_IO_POLLER_H_
18#define CYBER_IO_POLLER_H_
19
20#include <atomic>
21#include <list>
22#include <memory>
23#include <mutex>
24#include <thread>
25#include <unordered_map>
26#include <vector>
27
29#include "cyber/common/macros.h"
30#include "cyber/io/poll_data.h"
31
32namespace apollo {
33namespace cyber {
34namespace io {
35
36class Poller {
37 public:
38 using RequestPtr = std::shared_ptr<PollRequest>;
39 using RequestMap = std::unordered_map<int, RequestPtr>;
40 using CtrlParamMap = std::unordered_map<int, PollCtrlParam>;
41
42 virtual ~Poller();
43
44 void Shutdown();
45
46 bool Register(const PollRequest& req);
47 bool Unregister(const PollRequest& req);
48
49 private:
50 bool Init();
51 void Clear();
52 void Poll(int timeout_ms);
53 void ThreadFunc();
54 void HandleChanges();
55 int GetTimeoutMs();
56 void Notify();
57
58 int epoll_fd_ = -1;
59 std::thread thread_;
60 std::atomic<bool> is_shutdown_ = {true};
61
62 int pipe_fd_[2] = {-1, -1};
63 std::mutex pipe_mutex_;
64
65 RequestMap requests_;
66 CtrlParamMap ctrl_params_;
67 base::AtomicRWLock poll_data_lock_;
68
69 const int kPollSize = 32;
70 const int kPollTimeoutMs = 100;
71
72 DECLARE_SINGLETON(Poller)
73};
74
75} // namespace io
76} // namespace cyber
77} // namespace apollo
78
79#endif // CYBER_IO_POLLER_H_
bool Unregister(const PollRequest &req)
Definition poller.cc:85
std::unordered_map< int, RequestPtr > RequestMap
Definition poller.h:39
std::unordered_map< int, PollCtrlParam > CtrlParamMap
Definition poller.h:40
bool Register(const PollRequest &req)
Definition poller.cc:54
std::shared_ptr< PollRequest > RequestPtr
Definition poller.h:38
#define DECLARE_SINGLETON(classname)
Definition macros.h:52
class register implement
Definition arena_queue.h:37