timer.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. /**
  43. * Starts the timer, with the specified initial delay and interval.
  44. * @param initial Delay until the timer is first fired
  45. * @param interval Delay until the timer is fired after the first time
  46. */
  47. void Set(s64 initial, s64 interval);
  48. void Cancel();
  49. void Clear();
  50. /**
  51. * Signals the timer, waking up any waiting threads and rescheduling it
  52. * for the next interval.
  53. * This method should not be called from outside the timer callback handler,
  54. * lest multiple callback events get scheduled.
  55. */
  56. void Signal(int cycles_late);
  57. private:
  58. explicit Timer(KernelCore& kernel);
  59. ~Timer() override;
  60. ResetType reset_type; ///< The ResetType of this timer
  61. u64 initial_delay; ///< The delay until the timer fires for the first time
  62. u64 interval_delay; ///< The delay until the timer fires after the first time
  63. bool signaled; ///< Whether the timer has been signaled or not
  64. std::string name; ///< Name of timer (optional)
  65. /// Handle used as userdata to reference this object when inserting into the CoreTiming queue.
  66. Handle callback_handle;
  67. };
  68. } // namespace Kernel