timer.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/common_types.h"
  6. #include "core/hle/kernel/object.h"
  7. #include "core/hle/kernel/wait_object.h"
  8. namespace Kernel {
  9. class KernelCore;
  10. class Timer final : public WaitObject {
  11. public:
  12. /**
  13. * Creates a timer
  14. * @param kernel The kernel instance to create the timer callback handle for.
  15. * @param reset_type ResetType describing how to create the timer
  16. * @param name Optional name of timer
  17. * @return The created Timer
  18. */
  19. static SharedPtr<Timer> Create(KernelCore& kernel, ResetType reset_type,
  20. std::string name = "Unknown");
  21. std::string GetTypeName() const override {
  22. return "Timer";
  23. }
  24. std::string GetName() const override {
  25. return name;
  26. }
  27. static const HandleType HANDLE_TYPE = HandleType::Timer;
  28. HandleType GetHandleType() const override {
  29. return HANDLE_TYPE;
  30. }
  31. ResetType GetResetType() const {
  32. return reset_type;
  33. }
  34. u64 GetInitialDelay() const {
  35. return initial_delay;
  36. }
  37. u64 GetIntervalDelay() const {
  38. return interval_delay;
  39. }
  40. bool ShouldWait(Thread* thread) const override;
  41. void Acquire(Thread* thread) override;
  42. void WakeupAllWaitingThreads() override;
  43. /**
  44. * Starts the timer, with the specified initial delay and interval.
  45. * @param initial Delay until the timer is first fired
  46. * @param interval Delay until the timer is fired after the first time
  47. */
  48. void Set(s64 initial, s64 interval);
  49. void Cancel();
  50. void Clear();
  51. /**
  52. * Signals the timer, waking up any waiting threads and rescheduling it
  53. * for the next interval.
  54. * This method should not be called from outside the timer callback handler,
  55. * lest multiple callback events get scheduled.
  56. */
  57. void Signal(int cycles_late);
  58. private:
  59. explicit Timer(KernelCore& kernel);
  60. ~Timer() override;
  61. ResetType reset_type; ///< The ResetType of this timer
  62. u64 initial_delay; ///< The delay until the timer fires for the first time
  63. u64 interval_delay; ///< The delay until the timer fires after the first time
  64. bool signaled; ///< Whether the timer has been signaled or not
  65. std::string name; ///< Name of timer (optional)
  66. /// Handle used as userdata to reference this object when inserting into the CoreTiming queue.
  67. Handle callback_handle;
  68. };
  69. } // namespace Kernel