C/C++ API Reference
Loading...
Searching...
No Matches
epoll_dispatcher.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <unordered_map>
17
18#include "pw_assert/assert.h"
19#include "pw_async2/runnable_dispatcher.h"
20
21namespace pw::async2 {
22
24
25class EpollDispatcher final : public RunnableDispatcher {
26 public:
27 EpollDispatcher() { PW_ASSERT_OK(NativeInit()); }
28 ~EpollDispatcher() override { Terminate(); }
29
30 Status NativeInit();
31
32 enum FileDescriptorType {
33 kReadable = 1 << 0,
34 kWritable = 1 << 1,
35 kReadWrite = kReadable | kWritable,
36 };
37
38 Status NativeRegisterFileDescriptor(int fd, FileDescriptorType type);
39 Status NativeUnregisterFileDescriptor(int fd);
40
41 Waker& NativeAddReadWakerForFileDescriptor(int fd) {
42 return wakers_[fd].read;
43 }
44
45 Waker& NativeAddWriteWakerForFileDescriptor(int fd) {
46 return wakers_[fd].write;
47 }
48
49 private:
50 friend class ::pw::async2::Dispatcher;
51
52 static constexpr size_t kMaxEventsToProcessAtOnce = 5;
53
54 struct ReadWriteWaker {
55 Waker read;
56 Waker write;
57 };
58
59 void DoWake() override;
60
61 void DoWaitForWake() override;
62
63 Status NativeWaitForWake();
64 void NativeFindAndWakeFileDescriptor(int fd, FileDescriptorType type);
65
66 int epoll_fd_;
67 int notify_fd_;
68 int wait_fd_;
69
70 std::unordered_map<int, ReadWriteWaker> wakers_;
71};
72
74
75} // namespace pw::async2
Definition: status.h:120
Definition: epoll_dispatcher.h:25
void DoWaitForWake() override
Definition: runnable_dispatcher.h:24
Definition: waker.h:149