schedule.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // SPDX-License-Identifier: GPL-2.0 or GPL-3.0
  2. // Copyright © 2018-2019 Ariadne Devos
  3. /* sHT -- schedule tasks */
  4. #include "worker.h"
  5. #include <sHT/compiler.h>
  6. #include <sys/epoll.h>
  7. #include <sHT/index.h>
  8. #include <sHT/scheduling.h>
  9. #include <sHT/task.h>
  10. #include <sHT/test.h>
  11. __attribute__((returns_nonnull))
  12. __attribute__((const))
  13. static struct sHT_task *
  14. sHT_task_from_epoll_data(epoll_data_t d)
  15. {
  16. /* Alternatively, store an offset into worker->tasks.
  17. That should speed up worker transmutation up to some degree
  18. by being able to hand an epoll set over instead of having
  19. to create it all over, or modifying the data value. */
  20. /* If the pointer is invalid, e.g. due to buggy migration code
  21. (forgetting to fix up pointers), an assert would make this
  22. crash. Otherwise, a segfault would do that.
  23. Both work most of the time, but sometimes they don't, so
  24. there is not much of an advantage. So don't check.
  25. (The kernel is trusted.) */
  26. return d.ptr;
  27. }
  28. void
  29. sHT_schedule_events(struct sHT_worker *worker, size_t n)
  30. {
  31. size_t i;
  32. sHT_index_iterate(i, n) {
  33. struct epoll_event e = worker->epoll_events[i];
  34. struct sHT_task *task = sHT_task_from_epoll_data(e.data);
  35. task->epollflags |= e.events;
  36. /* Something can be done, if there is spare memory, even
  37. if 'something' is ignoring it. Edge-triggered. */
  38. sHT_qadd_try(&worker->todo.queue, task);
  39. }
  40. }