runqueue.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #ifndef __LIBUBOX_RUNQUEUE_H
  19. #define __LIBUBOX_RUNQUEUE_H
  20. #include "list.h"
  21. #include "safe_list.h"
  22. #include "uloop.h"
  23. struct runqueue;
  24. struct runqueue_task;
  25. struct runqueue_task_type;
  26. struct runqueue {
  27. struct safe_list tasks_active;
  28. struct safe_list tasks_inactive;
  29. struct uloop_timeout timeout;
  30. int running_tasks;
  31. int max_running_tasks;
  32. bool stopped;
  33. bool empty;
  34. /* called when the runqueue is emptied */
  35. void (*empty_cb)(struct runqueue *q);
  36. };
  37. struct runqueue_task_type {
  38. const char *name;
  39. /*
  40. * called when a task is requested to run
  41. *
  42. * The task is removed from the list before this callback is run. It
  43. * can re-arm itself using runqueue_task_add.
  44. */
  45. void (*run)(struct runqueue *q, struct runqueue_task *t);
  46. /*
  47. * called to request cancelling a task
  48. *
  49. * int type is used as an optional hint for the method to be used when
  50. * cancelling the task, e.g. a signal number for processes. The cancel
  51. * callback should call runqueue_task_complete when done.
  52. */
  53. void (*cancel)(struct runqueue *q, struct runqueue_task *t, int type);
  54. /*
  55. * called to kill a task. must not make any calls to runqueue_task_complete,
  56. * which will be called after this returns.
  57. */
  58. void (*kill)(struct runqueue *q, struct runqueue_task *t);
  59. };
  60. struct runqueue_task {
  61. struct safe_list list;
  62. const struct runqueue_task_type *type;
  63. struct runqueue *q;
  64. void (*complete)(struct runqueue *q, struct runqueue_task *t);
  65. struct uloop_timeout timeout;
  66. int run_timeout;
  67. int cancel_timeout;
  68. int cancel_type;
  69. bool queued;
  70. bool running;
  71. bool cancelled;
  72. };
  73. struct runqueue_process {
  74. struct runqueue_task task;
  75. struct uloop_process proc;
  76. };
  77. #define RUNQUEUE_INIT(_name, _max_running) { \
  78. .tasks_active = SAFE_LIST_INIT(_name.tasks_active), \
  79. .tasks_inactive = SAFE_LIST_INIT(_name.tasks_inactive), \
  80. .max_running_tasks = _max_running \
  81. }
  82. #define RUNQUEUE(_name, _max_running) \
  83. struct runqueue _name = RUNQUEUE_INIT(_name, _max_running)
  84. void runqueue_init(struct runqueue *q);
  85. void runqueue_cancel(struct runqueue *q);
  86. void runqueue_cancel_active(struct runqueue *q);
  87. void runqueue_cancel_pending(struct runqueue *q);
  88. void runqueue_kill(struct runqueue *q);
  89. void runqueue_stop(struct runqueue *q);
  90. void runqueue_resume(struct runqueue *q);
  91. void runqueue_task_add(struct runqueue *q, struct runqueue_task *t, bool running);
  92. void runqueue_task_add_first(struct runqueue *q, struct runqueue_task *t, bool running);
  93. void runqueue_task_complete(struct runqueue_task *t);
  94. void runqueue_task_cancel(struct runqueue_task *t, int type);
  95. void runqueue_task_kill(struct runqueue_task *t);
  96. void runqueue_process_add(struct runqueue *q, struct runqueue_process *p, pid_t pid);
  97. /* to be used only from runqueue_process callbacks */
  98. void runqueue_process_cancel_cb(struct runqueue *q, struct runqueue_task *t, int type);
  99. void runqueue_process_kill_cb(struct runqueue *q, struct runqueue_task *t);
  100. #endif