Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:48

0001 //===-- Alarm.h -------------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_HOST_ALARM_H
0010 #define LLDB_HOST_ALARM_H
0011 
0012 #include "lldb/Host/HostThread.h"
0013 #include "lldb/lldb-types.h"
0014 #include "llvm/Support/Chrono.h"
0015 
0016 #include <condition_variable>
0017 #include <mutex>
0018 
0019 namespace lldb_private {
0020 
0021 /// \class Alarm abstraction that enables scheduling a callback function after a
0022 /// specified timeout. Creating an alarm for a callback returns a Handle that
0023 /// can be used to restart or cancel the alarm.
0024 class Alarm {
0025 public:
0026   using Handle = uint64_t;
0027   using Callback = std::function<void()>;
0028   using TimePoint = llvm::sys::TimePoint<>;
0029   using Duration = std::chrono::milliseconds;
0030 
0031   Alarm(Duration timeout, bool run_callback_on_exit = false);
0032   ~Alarm();
0033 
0034   /// Create an alarm for the given callback. The alarm will expire and the
0035   /// callback will be called after the timeout.
0036   ///
0037   /// \returns
0038   ///   Handle which can be used to restart or cancel the alarm.
0039   Handle Create(Callback callback);
0040 
0041   /// Restart the alarm for the given Handle. The alarm will expire and the
0042   /// callback will be called after the timeout.
0043   ///
0044   /// \returns
0045   ///   True if the alarm was successfully restarted. False if there is no alarm
0046   ///   for the given Handle or the alarm already expired.
0047   bool Restart(Handle handle);
0048 
0049   /// Cancel the alarm for the given Handle. The alarm and its handle will be
0050   /// removed.
0051   ///
0052   /// \returns
0053   ///   True if the alarm was successfully canceled and the Handle removed.
0054   ///   False if there is no alarm for the given Handle or the alarm already
0055   ///   expired.
0056   bool Cancel(Handle handle);
0057 
0058   static constexpr Handle INVALID_HANDLE = 0;
0059 
0060 private:
0061   /// Helper functions to start, stop and check the status of the alarm thread.
0062   /// @{
0063   void StartAlarmThread();
0064   void StopAlarmThread();
0065   bool AlarmThreadRunning();
0066   /// @}
0067 
0068   /// Return an unique, monotonically increasing handle.
0069   static Handle GetNextUniqueHandle();
0070 
0071   /// Helper to compute the next time the alarm thread needs to wake up.
0072   TimePoint GetNextExpiration() const;
0073 
0074   /// Alarm entry.
0075   struct Entry {
0076     Handle handle;
0077     Callback callback;
0078     TimePoint expiration;
0079 
0080     Entry(Callback callback, TimePoint expiration);
0081     bool operator==(const Entry &rhs) { return handle == rhs.handle; }
0082   };
0083 
0084   /// List of alarm entries.
0085   std::vector<Entry> m_entries;
0086 
0087   /// Timeout between when an alarm is created and when it fires.
0088   Duration m_timeout;
0089 
0090   /// The alarm thread.
0091   /// @{
0092   HostThread m_alarm_thread;
0093   lldb::thread_result_t AlarmThread();
0094   /// @}
0095 
0096   /// Synchronize access between the alarm thread and the main thread.
0097   std::mutex m_alarm_mutex;
0098 
0099   /// Condition variable used to wake up the alarm thread.
0100   std::condition_variable m_alarm_cv;
0101 
0102   /// Flag to signal the alarm thread that something changed and we need to
0103   /// recompute the next alarm.
0104   bool m_recompute_next_alarm = false;
0105 
0106   /// Flag to signal the alarm thread to exit.
0107   bool m_exit = false;
0108 
0109   /// Flag to signal we should run all callbacks on exit.
0110   bool m_run_callbacks_on_exit = false;
0111 };
0112 
0113 } // namespace lldb_private
0114 
0115 #endif // LLDB_HOST_ALARM_H