xref: /aosp_15_r20/external/openscreen/platform/impl/socket_handle_waiter_posix.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PLATFORM_IMPL_SOCKET_HANDLE_WAITER_POSIX_H_
6 #define PLATFORM_IMPL_SOCKET_HANDLE_WAITER_POSIX_H_
7 
8 #include <unistd.h>
9 
10 #include <atomic>
11 #include <mutex>
12 #include <vector>
13 
14 #include "platform/impl/socket_handle_waiter.h"
15 
16 namespace openscreen {
17 
18 class SocketHandleWaiterPosix : public SocketHandleWaiter {
19  public:
20   using SocketHandleRef = SocketHandleWaiter::SocketHandleRef;
21 
22   explicit SocketHandleWaiterPosix(ClockNowFunctionPtr now_function);
23   ~SocketHandleWaiterPosix() override;
24 
25   // Runs the Wait function in a loop until the below RequestStopSoon function
26   // is called.
27   void RunUntilStopped();
28 
29   // Signals for the RunUntilStopped loop to cease running.
30   void RequestStopSoon();
31 
32  protected:
33   using SocketHandleWaiter::ReadyHandle;
34 
35   ErrorOr<std::vector<ReadyHandle>> AwaitSocketsReadable(
36       const std::vector<SocketHandleRef>& socket_fds,
37       const Clock::duration& timeout) override;
38 
39  private:
40   // Atomic so that we can perform atomic exchanges.
41   std::atomic_bool is_running_;
42 };
43 
44 }  // namespace openscreen
45 
46 #endif  // PLATFORM_IMPL_SOCKET_HANDLE_WAITER_POSIX_H_
47