• 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,
55                                  const ReadCallback& on_read_fd_ready_callback);
56 
57   // If the fd was not being watched before the call will be ignored.
58   void StopWatchingFileDescriptor(int file_descriptor);
59 
60   // Schedules an action to occur in the future. Even if the delay given is not
61   // positive the callback will be called asynchronously.
62   AsyncTaskId ExecAsync(std::chrono::milliseconds delay,
63                         const TaskCallback& callback);
64 
65   // Schedules an action to occur periodically in the future. If the delay given
66   // is not positive the callback will be asynchronously called once for each
67   // time in the past that it should have been called and then scheduled for
68   // future times.
69   AsyncTaskId ExecAsyncPeriodically(std::chrono::milliseconds delay,
70                                     std::chrono::milliseconds period,
71                                     const TaskCallback& callback);
72 
73   // Cancels the/every future ocurrence of the action specified by this id. It
74   // is guaranteed that the asociated callback will not be called after this
75   // method returns (it could be called during the execution of the method).
76   // The calling thread may block until the scheduling thread acknowledges the
77   // cancelation.
78   bool CancelAsyncTask(AsyncTaskId async_task_id);
79 
80   // Execs the given code in a synchronized manner. It is guaranteed that code
81   // given on (possibly)concurrent calls to this member function on the same
82   // AsyncManager object will never be executed simultaneously. It is the
83   // class's user's resposability to ensure that no calls to Synchronize are
84   // made from inside a CriticalCallback, since that would cause a lock to be
85   // acquired twice with unpredictable results. It is strongly recommended to
86   // have very simple CriticalCallbacks, preferably using lambda expressions.
87   void Synchronize(const CriticalCallback&);
88 
89   AsyncManager();
90 
91   ~AsyncManager();
92 
93  private:
94   // Implementation of the FD watching part of AsyncManager, extracted to its
95   // own class for clarity purposes.
96   class AsyncFdWatcher;
97 
98   // Implementation of the asynchronous tasks part of AsyncManager, extracted to
99   // its own class for clarity purposes.
100   class AsyncTaskManager;
101 
102   AsyncManager(const AsyncManager&) = delete;
103   AsyncManager& operator=(const AsyncManager&) = delete;
104 
105   // Kept as pointers because we may want to support reseting either without
106   // destroying the other one
107   std::unique_ptr<AsyncFdWatcher> fdWatcher_p_;
108   std::unique_ptr<AsyncTaskManager> taskManager_p_;
109 
110   std::mutex synchronization_mutex_;
111 };
112 }
113 #endif  // TEST_VENDOR_LIB_ASYNC_MANAGER_H_
114