loop.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-License-Identifier: GPL-2.0 or GPL-3.0
  2. // Copyright © 2018-2019 Ariadne Devos
  3. /* sHT -- process all events in a loop */
  4. #if 0
  5. /* XXX "worker.h" will be deleted */
  6. #include "worker.h"
  7. #include <sHT/bugs.h>
  8. #include <sHT/compiler.h>
  9. #include <errno.h>
  10. #include <sys/epoll.h>
  11. __attribute__((nonnull (1)))
  12. __attribute__((warn_unused_result))
  13. static int
  14. sHT_wait_and_poll(struct sHT_worker *worker, int timeout)
  15. {
  16. int n;
  17. int *err = &errno;
  18. do n = epoll_wait(worker->watches, worker->epoll_events, worker->todo->capacity, timeout);
  19. while (sHT_test_hidden(n, n < 0) && sHT_test_hidden(*err, *err == EINTR));
  20. sHT_require(n >= 0);
  21. return n;
  22. }
  23. __attribute__((nonnull (1, 2)))
  24. static void
  25. sHT_perform_task_wrapper(void *cookie, struct sHT_task *task)
  26. {
  27. /* TODO: distinction between homogeneous and heterogeneous */
  28. sHT_perform_task(cookie, task);
  29. }
  30. __attribute__((nonnull (1)))
  31. static void
  32. sHT_worker_iteration(struct sHT_worker *worker)
  33. {
  34. /* If there are things to do, do poll, such that other tasks may
  35. make progress, but don't wait. */
  36. _Bool nonblock = sHT_test_hidden(worker->todo->n, worker->todo->n > 0);
  37. int n_events = sHT_wait_and_poll(worker, nonblock ? 0 : -1);
  38. sHT_schedule_events(worker, n_events);
  39. sHT_perform_all(worker, worker->todo, &sHT_perform_task_wrapper);
  40. /* XXX: notify the control thread (OOM ...) */
  41. sHT_todo(worker->flags);
  42. }
  43. _Noreturn void
  44. sHT_worker(struct sHT_worker *worker)
  45. {
  46. while (1)
  47. sHT_worker_iteration(worker);
  48. }
  49. #endif