• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef TEST_VENDOR_LIB_ASYNC_MANAGER_H_
2 #define TEST_VENDOR_LIB_ASYNC_MANAGER_H_
3 
4 #include <time.h>
5 #include <cstdint>
6 #include <map>
7 #include <set>
8 #include "errno.h"
9 #include "stdio.h"
10 
11 #include <chrono>
12 #include <functional>
13 #include <memory>
14 #include <mutex>
15 #include <utility>
16 
17 namespace test_vendor_lib {
18 
19 using TaskCallback = std::function<void(void)>;
20 using ReadCallback = std::function<void(int)>;
21 using CriticalCallback = std::function<void(void)>;
22 using AsyncTaskId = uint16_t;
23 constexpr uint16_t kInvalidTaskId = 0;
24 
25 // Manages tasks that should be done in the future. It can watch file
26 // descriptors to call a given callback when it is certain that a non-blocking
27 // read is possible or can call a callback at a specific time (aproximately) and
28 // (optionally) repeat the call periodically.
29 // The class is thread safe in the sense that all its member functions can be
30 // called simultaneously from different concurrent threads. The exception to
31 // this rule is the class destructor, which is unsafe to call concurrently with
32 // calls to other class member functions. This exception also has its own
33 // exception: it is safe to destroy the object even if some of its callbacks may
34 // call its member functions, because the destructor will make sure all callback
35 // calling threads are stopped before actually destroying anything. Callbacks
36 // that wait for file descriptor always run on the same thread, so there is no
37 // need of additional synchronization between them. The same applies to task
38 // callbacks since they also run on a thread of their own, however it is
39 // possible for a read callback and a task callback to execute at the same time
40 // (they are garanteed to run in different threads) so synchronization is needed
41 // to access common state (other than the internal state of the AsyncManager
42 // class). While not required, it is strongly recommended to use the
43 // Synchronize(const CriticalCallback&) member function to execute code inside
44 // critical sections. Callbacks passed to this method on the same AsyncManager
45 // object from different threads are granted to *NOT* run concurrently.
46 class AsyncManager {
47  public:
48   // Starts watching a file descriptor in a separate thread. The
49   // on_read_fd_ready_callback() will be asynchronously called when it is
50   // guaranteed that a call to read() on the FD will not block. No promise is
51   // made about when in the future the callback will be called, in particular,
52   // it is perfectly possible to have it called before this function returns. A
53   // return of 0 means success, an error code is returned otherwise.
54   int WatchFdForNonBlockingReads(int file_descriptor, const ReadCallback& on_read_fd_ready_callback);
55 
56   // If the fd was not being watched before the call will be ignored.
57   void StopWatchingFileDescriptor(int file_descriptor);
58 
59   // Schedules an action to occur in the future. Even if the delay given is not
60   // positive the callback will be called asynchronously.
61   AsyncTaskId ExecAsync(std::chrono::milliseconds delay, const TaskCallback& callback);
62 
63   // Schedules an action to occur periodically in the future. If the delay given
64   // is not positive the callback will be asynchronously called once for each
65   // time in the past that it should have been called and then scheduled for
66   // future times.
67   AsyncTaskId ExecAsyncPeriodically(std::chrono::milliseconds delay, std::chrono::milliseconds period,
68                                     const TaskCallback& callback);
69 
70   // Cancels the/every future ocurrence of the action specified by this id. It
71   // is guaranteed that the asociated callback will not be called after this
72   // method returns (it could be called during the execution of the method).
73   // The calling thread may block until the scheduling thread acknowledges the
74   // cancelation.
75   bool CancelAsyncTask(AsyncTaskId async_task_id);
76 
77   // Execs the given code in a synchronized manner. It is guaranteed that code
78   // given on (possibly)concurrent calls to this member function on the same
79   // AsyncManager object will never be executed simultaneously. It is the
80   // class's user's resposability to ensure that no calls to Synchronize are
81   // made from inside a CriticalCallback, since that would cause a lock to be
82   // acquired twice with unpredictable results. It is strongly recommended to
83   // have very simple CriticalCallbacks, preferably using lambda expressions.
84   void Synchronize(const CriticalCallback&);
85 
86   AsyncManager();
87 
88   ~AsyncManager();
89 
90  private:
91   // Implementation of the FD watching part of AsyncManager, extracted to its
92   // own class for clarity purposes.
93   class AsyncFdWatcher;
94 
95   // Implementation of the asynchronous tasks part of AsyncManager, extracted to
96   // its own class for clarity purposes.
97   class AsyncTaskManager;
98 
99   AsyncManager(const AsyncManager&) = delete;
100   AsyncManager& operator=(const AsyncManager&) = delete;
101 
102   // Kept as pointers because we may want to support reseting either without
103   // destroying the other one
104   std::unique_ptr<AsyncFdWatcher> fdWatcher_p_;
105   std::unique_ptr<AsyncTaskManager> taskManager_p_;
106 
107   std::mutex synchronization_mutex_;
108 };
109 }  // namespace test_vendor_lib
110 #endif  // TEST_VENDOR_LIB_ASYNC_MANAGER_H_
111