idle.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <unistd.h>
  26. #include <stdbool.h>
  27. #include <stdint.h>
  28. #include "useful.h"
  29. #include "idle.h"
  30. #include "main-private.h"
  31. #include "private.h"
  32. /**
  33. * SECTION:idle
  34. * @short_description: Idle processing support
  35. *
  36. * Idle processing support
  37. */
  38. /**
  39. * l_idle:
  40. *
  41. * Opague object representing the idle time event.
  42. */
  43. struct l_idle {
  44. union {
  45. l_idle_notify_cb_t callback;
  46. l_idle_oneshot_cb_t oneshot;
  47. };
  48. l_idle_destroy_cb_t destroy;
  49. void *user_data;
  50. int id;
  51. };
  52. static void idle_destroy(void *user_data)
  53. {
  54. struct l_idle *idle = user_data;
  55. if (idle->destroy)
  56. idle->destroy(idle->user_data);
  57. l_free(idle);
  58. }
  59. static void idle_callback(void *user_data)
  60. {
  61. struct l_idle *idle = user_data;
  62. if (idle->callback)
  63. idle->callback(idle, idle->user_data);
  64. }
  65. static void oneshot_callback(void *user_data)
  66. {
  67. struct l_idle *idle = user_data;
  68. if (idle->oneshot)
  69. idle->oneshot(idle->user_data);
  70. idle_remove(idle->id);
  71. }
  72. /**
  73. * l_idle_create:
  74. * @callback: idle callback function
  75. * @user_data: user data provided to idle callback function
  76. * @destroy: destroy function for user data
  77. *
  78. * Create a new idle event processing object.
  79. *
  80. * The idle callback will be called until canceled using l_idle_remove().
  81. *
  82. * Returns: a newly allocated #l_idle object
  83. **/
  84. LIB_EXPORT struct l_idle *l_idle_create(l_idle_notify_cb_t callback,
  85. void *user_data, l_idle_destroy_cb_t destroy)
  86. {
  87. struct l_idle *idle;
  88. if (unlikely(!callback))
  89. return NULL;
  90. idle = l_new(struct l_idle, 1);
  91. idle->callback = callback;
  92. idle->destroy = destroy;
  93. idle->user_data = user_data;
  94. idle->id = idle_add(idle_callback, idle, 0, idle_destroy);
  95. if (idle->id < 0) {
  96. l_free(idle);
  97. return NULL;
  98. }
  99. return idle;
  100. }
  101. /**
  102. * l_idle_oneshot:
  103. * @callback: idle callback function
  104. * @user_data: user data provided to idle callback function
  105. * @destroy: destroy function for user data
  106. *
  107. * Create a new idle event processing object. The callback will be called
  108. * only once at which point the object will be destroyed.
  109. *
  110. * Returns: true if the oneshot idle object could be created successfully.
  111. **/
  112. LIB_EXPORT bool l_idle_oneshot(l_idle_oneshot_cb_t callback, void *user_data,
  113. l_idle_destroy_cb_t destroy)
  114. {
  115. struct l_idle *idle;
  116. if (unlikely(!callback))
  117. return NULL;
  118. idle = l_new(struct l_idle, 1);
  119. idle->oneshot = callback;
  120. idle->destroy = destroy;
  121. idle->user_data = user_data;
  122. idle->id = idle_add(oneshot_callback, idle,
  123. IDLE_FLAG_NO_WARN_DANGLING, idle_destroy);
  124. if (idle->id < 0) {
  125. l_free(idle);
  126. return false;
  127. }
  128. return true;
  129. }
  130. /**
  131. * l_idle_remove:
  132. * @idle: idle object
  133. *
  134. * Remove idle event processing object.
  135. **/
  136. LIB_EXPORT void l_idle_remove(struct l_idle *idle)
  137. {
  138. if (unlikely(!idle))
  139. return;
  140. idle_remove(idle->id);
  141. }