runqueue.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * runqueue.c - a simple task queueing/completion tracking helper
  3. *
  4. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include "runqueue.h"
  21. static void
  22. __runqueue_empty_cb(struct uloop_timeout *timeout)
  23. {
  24. struct runqueue *q = container_of(timeout, struct runqueue, timeout);
  25. q->empty_cb(q);
  26. }
  27. void runqueue_init(struct runqueue *q)
  28. {
  29. INIT_SAFE_LIST(&q->tasks_active);
  30. INIT_SAFE_LIST(&q->tasks_inactive);
  31. }
  32. static void __runqueue_start_next(struct uloop_timeout *timeout)
  33. {
  34. struct runqueue *q = container_of(timeout, struct runqueue, timeout);
  35. struct runqueue_task *t;
  36. do {
  37. if (q->stopped)
  38. break;
  39. if (list_empty(&q->tasks_inactive.list))
  40. break;
  41. if (q->max_running_tasks && q->running_tasks >= q->max_running_tasks)
  42. break;
  43. t = list_first_entry(&q->tasks_inactive.list, struct runqueue_task, list.list);
  44. safe_list_del(&t->list);
  45. safe_list_add(&t->list, &q->tasks_active);
  46. t->running = true;
  47. q->running_tasks++;
  48. if (t->run_timeout)
  49. uloop_timeout_set(&t->timeout, t->run_timeout);
  50. t->type->run(q, t);
  51. } while (1);
  52. if (!q->empty &&
  53. list_empty(&q->tasks_active.list) &&
  54. list_empty(&q->tasks_inactive.list)) {
  55. q->empty = true;
  56. if (q->empty_cb) {
  57. q->timeout.cb = __runqueue_empty_cb;
  58. uloop_timeout_set(&q->timeout, 1);
  59. }
  60. }
  61. }
  62. static void runqueue_start_next(struct runqueue *q)
  63. {
  64. if (q->empty)
  65. return;
  66. q->timeout.cb = __runqueue_start_next;
  67. uloop_timeout_set(&q->timeout, 1);
  68. }
  69. static int __runqueue_cancel(void *ctx, struct safe_list *list)
  70. {
  71. struct runqueue_task *t;
  72. t = container_of(list, struct runqueue_task, list);
  73. runqueue_task_cancel(t, 0);
  74. return 0;
  75. }
  76. void runqueue_cancel_active(struct runqueue *q)
  77. {
  78. safe_list_for_each(&q->tasks_active, __runqueue_cancel, NULL);
  79. }
  80. void runqueue_cancel_pending(struct runqueue *q)
  81. {
  82. safe_list_for_each(&q->tasks_inactive, __runqueue_cancel, NULL);
  83. }
  84. void runqueue_cancel(struct runqueue *q)
  85. {
  86. runqueue_cancel_pending(q);
  87. runqueue_cancel_active(q);
  88. }
  89. void runqueue_kill(struct runqueue *q)
  90. {
  91. struct runqueue_task *t;
  92. while (!list_empty(&q->tasks_active.list)) {
  93. t = list_first_entry(&q->tasks_active.list, struct runqueue_task, list.list);
  94. runqueue_task_kill(t);
  95. }
  96. runqueue_cancel_pending(q);
  97. uloop_timeout_cancel(&q->timeout);
  98. }
  99. void runqueue_task_cancel(struct runqueue_task *t, int type)
  100. {
  101. if (!t->queued)
  102. return;
  103. if (!t->running) {
  104. runqueue_task_complete(t);
  105. return;
  106. }
  107. t->cancelled = true;
  108. if (t->cancel_timeout)
  109. uloop_timeout_set(&t->timeout, t->cancel_timeout);
  110. if (t->type->cancel)
  111. t->type->cancel(t->q, t, type);
  112. }
  113. static void
  114. __runqueue_task_timeout(struct uloop_timeout *timeout)
  115. {
  116. struct runqueue_task *t = container_of(timeout, struct runqueue_task, timeout);
  117. if (t->cancelled)
  118. runqueue_task_kill(t);
  119. else
  120. runqueue_task_cancel(t, t->cancel_type);
  121. }
  122. static void _runqueue_task_add(struct runqueue *q, struct runqueue_task *t, bool running, bool first)
  123. {
  124. struct safe_list *head;
  125. if (t->queued)
  126. return;
  127. if (!t->type->run && !running) {
  128. fprintf(stderr, "BUG: inactive task added without run() callback\n");
  129. return;
  130. }
  131. if (running) {
  132. q->running_tasks++;
  133. head = &q->tasks_active;
  134. } else {
  135. head = &q->tasks_inactive;
  136. }
  137. t->timeout.cb = __runqueue_task_timeout;
  138. t->q = q;
  139. if (first)
  140. safe_list_add_first(&t->list, head);
  141. else
  142. safe_list_add(&t->list, head);
  143. t->cancelled = false;
  144. t->queued = true;
  145. t->running = running;
  146. q->empty = false;
  147. runqueue_start_next(q);
  148. }
  149. void runqueue_task_add(struct runqueue *q, struct runqueue_task *t, bool running)
  150. {
  151. _runqueue_task_add(q, t, running, 0);
  152. }
  153. void runqueue_task_add_first(struct runqueue *q, struct runqueue_task *t, bool running)
  154. {
  155. _runqueue_task_add(q, t, running, 1);
  156. }
  157. void runqueue_task_kill(struct runqueue_task *t)
  158. {
  159. struct runqueue *q = t->q;
  160. bool running = t->running;
  161. if (!t->queued)
  162. return;
  163. if (running && t->type->kill)
  164. t->type->kill(q, t);
  165. runqueue_task_complete(t);
  166. }
  167. void runqueue_stop(struct runqueue *q)
  168. {
  169. q->stopped = true;
  170. }
  171. void runqueue_resume(struct runqueue *q)
  172. {
  173. q->stopped = false;
  174. runqueue_start_next(q);
  175. }
  176. void runqueue_task_complete(struct runqueue_task *t)
  177. {
  178. struct runqueue *q = t->q;
  179. if (!t->queued)
  180. return;
  181. if (t->running)
  182. t->q->running_tasks--;
  183. uloop_timeout_cancel(&t->timeout);
  184. safe_list_del(&t->list);
  185. t->queued = false;
  186. t->running = false;
  187. t->cancelled = false;
  188. if (t->complete)
  189. t->complete(q, t);
  190. runqueue_start_next(q);
  191. }
  192. static void
  193. __runqueue_proc_cb(struct uloop_process *p, int ret)
  194. {
  195. struct runqueue_process *t = container_of(p, struct runqueue_process, proc);
  196. runqueue_task_complete(&t->task);
  197. }
  198. void runqueue_process_cancel_cb(struct runqueue *q, struct runqueue_task *t, int type)
  199. {
  200. struct runqueue_process *p = container_of(t, struct runqueue_process, task);
  201. if (!type)
  202. type = SIGTERM;
  203. kill(p->proc.pid, type);
  204. }
  205. void runqueue_process_kill_cb(struct runqueue *q, struct runqueue_task *t)
  206. {
  207. struct runqueue_process *p = container_of(t, struct runqueue_process, task);
  208. uloop_process_delete(&p->proc);
  209. kill(p->proc.pid, SIGKILL);
  210. }
  211. static const struct runqueue_task_type runqueue_proc_type = {
  212. .name = "process",
  213. .cancel = runqueue_process_cancel_cb,
  214. .kill = runqueue_process_kill_cb,
  215. };
  216. void runqueue_process_add(struct runqueue *q, struct runqueue_process *p, pid_t pid)
  217. {
  218. if (p->proc.pending)
  219. return;
  220. p->proc.pid = pid;
  221. p->proc.cb = __runqueue_proc_cb;
  222. if (!p->task.type)
  223. p->task.type = &runqueue_proc_type;
  224. uloop_process_add(&p->proc);
  225. if (!p->task.running)
  226. runqueue_task_add(q, &p->task, true);
  227. }