Apollo  6.0
Open source self driving car software
poller.h
Go to the documentation of this file.
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 
32 namespace apollo {
33 namespace cyber {
34 namespace io {
35 
36 class 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 
73 };
74 
75 } // namespace io
76 } // namespace cyber
77 } // namespace apollo
78 
79 #endif // CYBER_IO_POLLER_H_
Definition: poller.h:36
std::unordered_map< int, RequestPtr > RequestMap
Definition: poller.h:39
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: atomic_rw_lock.h:36
std::shared_ptr< PollRequest > RequestPtr
Definition: poller.h:38
Definition: poll_data.h:35
#define DECLARE_SINGLETON(classname)
Definition: macros.h:52
bool Register(const PollRequest &req)
bool Unregister(const PollRequest &req)
std::unordered_map< int, PollCtrlParam > CtrlParamMap
Definition: poller.h:40