alarm.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /******************************************************************************
  2. *
  3. * Copyright 2014 Google, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #pragma once
  19. #include <stdbool.h>
  20. #include <stdint.h>
  21. typedef struct alarm_t alarm_t;
  22. typedef struct fixed_queue_t fixed_queue_t;
  23. typedef struct thread_t thread_t;
  24. // Prototype for the alarm callback function.
  25. typedef void (*alarm_callback_t)(void* data);
  26. // Creates a new one-time off alarm object with user-assigned
  27. // |name|. |name| may not be NULL, and a copy of the string will
  28. // be stored internally. The value of |name| has no semantic
  29. // meaning. It is recommended that the name is unique (for
  30. // better debuggability), but that is not enforced. The returned
  31. // object must be freed by calling |alarm_free|. Returns NULL on
  32. // failure.
  33. alarm_t* alarm_new(const char* name);
  34. // Creates a new periodic alarm object with user-assigned |name|.
  35. // |name| may not be NULL, and a copy of the string will be
  36. // stored internally. The value of |name| has no semantic
  37. // meaning. It is recommended that the name is unique (for better
  38. // debuggability), but that is not enforced. The returned object
  39. // must be freed by calling |alarm_free|. Returns NULL on
  40. // failure.
  41. alarm_t* alarm_new_periodic(const char* name);
  42. // Frees an |alarm| object created by |alarm_new| or
  43. // |alarm_new_periodic|. |alarm| may be NULL. If the alarm is
  44. // pending, it will be cancelled first. It is not safe to call
  45. // |alarm_free| from inside the callback of |alarm|.
  46. void alarm_free(alarm_t* alarm);
  47. // Sets an |alarm| to execute a callback in the future. The |cb|
  48. // callback is called after the given |interval_ms|, where
  49. // |interval_ms| is the number of milliseconds relative to the
  50. // current time. If |alarm| was created with
  51. // |alarm_new_periodic|, the alarm is scheduled to fire
  52. // periodically every |interval_ms|, otherwise it is a one time
  53. // only alarm. A periodic alarm repeats every |interval_ms| until
  54. // it is cancelled or freed. When the alarm fires, the |cb|
  55. // callback is called with the context argument |data|:
  56. //
  57. // void cb(void *data) {...}
  58. //
  59. // The |data| argument may be NULL, but the |cb| callback may not
  60. // be NULL. All |cb| callbacks scheduled through this call are
  61. // called within a single (internally created) thread. That
  62. // thread is not same as the caller’s thread. If two (or more)
  63. // alarms are set back-to-back with the same |interval_ms|, the
  64. // callbacks will be called in the order the alarms are set.
  65. void alarm_set(alarm_t* alarm, uint64_t interval_ms, alarm_callback_t cb,
  66. void* data);
  67. // Sets an |alarm| to execute a callback in the main message loop. This function
  68. // is same as |alarm_set| except that the |cb| callback is scheduled for
  69. // execution in the context of the main message loop.
  70. void alarm_set_on_mloop(alarm_t* alarm, uint64_t interval_ms,
  71. alarm_callback_t cb, void* data);
  72. // This function cancels the |alarm| if it was previously set.
  73. // When this call returns, the caller has a guarantee that the
  74. // callback is not in progress and will not be called if it
  75. // hasn't already been called. This function is idempotent.
  76. // |alarm| may not be NULL.
  77. void alarm_cancel(alarm_t* alarm);
  78. // Tests whether the |alarm| is scheduled.
  79. // Return true if the |alarm| is scheduled or NULL, otherwise false.
  80. bool alarm_is_scheduled(const alarm_t* alarm);
  81. // Figure out how much time until next expiration.
  82. // Returns 0 if not armed. |alarm| may not be NULL.
  83. // TODO: Remove this function once PM timers can be re-factored
  84. uint64_t alarm_get_remaining_ms(const alarm_t* alarm);
  85. // Cleanup the alarm internal state.
  86. // This function should be called by the OSI module cleanup during
  87. // graceful shutdown.
  88. void alarm_cleanup(void);
  89. // Dump alarm-related statistics and debug info to the |fd| file descriptor.
  90. // The information is in user-readable text format. The |fd| must be valid.
  91. void alarm_debug_dump(int fd);