test-runqueue.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * runqueue-example.c
  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 <stdbool.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include "uloop.h"
  23. #include "runqueue.h"
  24. static struct runqueue q;
  25. struct sleeper {
  26. int val;
  27. bool kill;
  28. struct uloop_timeout t;
  29. struct runqueue_process proc;
  30. };
  31. static void q_empty(struct runqueue *q)
  32. {
  33. fprintf(stderr, "All done!\n");
  34. uloop_end();
  35. }
  36. static const char* sleeper_type(struct sleeper *s)
  37. {
  38. return s->kill ? "killer" : "sleeper";
  39. }
  40. static void q_sleep_run(struct runqueue *q, struct runqueue_task *t)
  41. {
  42. struct sleeper *s = container_of(t, struct sleeper, proc.task);
  43. char str[32];
  44. pid_t pid;
  45. fprintf(stderr, "[%d/%d] start 'sleep %d' (%s)\n", q->running_tasks,
  46. q->max_running_tasks, s->val, sleeper_type(s));
  47. pid = fork();
  48. if (pid < 0)
  49. return;
  50. if (pid) {
  51. runqueue_process_add(q, &s->proc, pid);
  52. return;
  53. }
  54. sprintf(str, "%d", s->val);
  55. execlp("sleep", "sleep", str, NULL);
  56. exit(1);
  57. }
  58. static void q_sleep_cancel(struct runqueue *q, struct runqueue_task *t, int type)
  59. {
  60. struct sleeper *s = container_of(t, struct sleeper, proc.task);
  61. fprintf(stderr, "[%d/%d] cancel 'sleep %d' (%s)\n", q->running_tasks,
  62. q->max_running_tasks, s->val, sleeper_type(s));
  63. runqueue_process_cancel_cb(q, t, type);
  64. }
  65. static void q_sleep_complete(struct runqueue *q, struct runqueue_task *p)
  66. {
  67. struct sleeper *s = container_of(p, struct sleeper, proc.task);
  68. fprintf(stderr, "[%d/%d] finish 'sleep %d' (%s) \n", q->running_tasks,
  69. q->max_running_tasks, s->val, sleeper_type(s));
  70. free(s);
  71. }
  72. static void my_runqueue_process_kill_cb(struct runqueue *q, struct runqueue_task *p)
  73. {
  74. struct sleeper *s = container_of(p, struct sleeper, proc.task);
  75. fprintf(stderr, "[%d/%d] killing process (%s)\n", q->running_tasks,
  76. q->max_running_tasks, sleeper_type(s));
  77. runqueue_process_kill_cb(q, p);
  78. }
  79. static void timer_cb(struct uloop_timeout *t)
  80. {
  81. struct sleeper *s = container_of(t, struct sleeper, t);
  82. if (s->kill)
  83. runqueue_task_kill(&s->proc.task);
  84. }
  85. static struct sleeper* create_sleeper(int val, const struct runqueue_task_type *type, bool kill)
  86. {
  87. struct sleeper *s = calloc(1, sizeof(*s));
  88. s->kill = kill;
  89. s->t.cb = timer_cb;
  90. s->proc.task.type = type;
  91. s->proc.task.run_timeout = 500;
  92. s->proc.task.complete = q_sleep_complete;
  93. s->val = val;
  94. return s;
  95. }
  96. static void add_sleeper(int val)
  97. {
  98. static const struct runqueue_task_type sleeper_type = {
  99. .run = q_sleep_run,
  100. .cancel = q_sleep_cancel,
  101. .kill = runqueue_process_kill_cb,
  102. };
  103. static const struct runqueue_task_type killer_type = {
  104. .run = q_sleep_run,
  105. .cancel = q_sleep_cancel,
  106. .kill = my_runqueue_process_kill_cb,
  107. };
  108. struct sleeper *k = create_sleeper(val, &killer_type, true);
  109. uloop_timeout_set(&k->t, 10);
  110. uloop_timeout_add(&k->t);
  111. runqueue_task_add(&q, &k->proc.task, false);
  112. struct sleeper *s = create_sleeper(val, &sleeper_type, false);
  113. runqueue_task_add(&q, &s->proc.task, false);
  114. }
  115. int main(int argc, char **argv)
  116. {
  117. uloop_init();
  118. runqueue_init(&q);
  119. q.empty_cb = q_empty;
  120. q.max_running_tasks = 1;
  121. if (argc > 1)
  122. q.max_running_tasks = atoi(argv[1]);
  123. add_sleeper(1);
  124. add_sleeper(1);
  125. add_sleeper(1);
  126. uloop_run();
  127. uloop_done();
  128. return 0;
  129. }