1. # A batch of work sent to a worker in a hyper or race operation. It is an
  2. # Iterable, and iterates to the items in the batch. This is so that it can be
  3. # easily processed in terms of (non-hyper) Iterable implementations.
  4. my class Rakudo::Internals::HyperWorkBatch does Iterable {
  5. # The items in the batch.
  6. has IterationBuffer $.items;
  7. # Sequence number of the batch, starting from zero.
  8. has int $.sequence-number;
  9. # Is this the first batch that was produced at the last fork point or the
  10. # last batch that the fork point will produce?
  11. has Bool $.first;
  12. has Bool $.last;
  13. method SET-SELF(\sequence-number, \items, \first, \last) {
  14. $!sequence-number = sequence-number;
  15. $!items := items;
  16. $!first = first.Bool;
  17. $!last = last.Bool;
  18. self
  19. }
  20. method new(\sn,\it,\f,\l) { nqp::create(self).SET-SELF(sn,it,f,l) }
  21. # Iterator for a HyperWorkBatch;
  22. my class HyperWorkBatchIterator does Iterator {
  23. has $!items;
  24. has int $!i;
  25. has int $!n;
  26. submethod BUILD(:$items --> Nil) {
  27. $!items := nqp::decont($items);
  28. $!i = -1;
  29. $!n = nqp::elems($!items);
  30. }
  31. method pull-one() {
  32. ++$!i < $!n
  33. ?? nqp::atpos($!items, $!i)
  34. !! IterationEnd
  35. }
  36. }
  37. method iterator(--> Iterator) {
  38. HyperWorkBatchIterator.new(:$!items)
  39. }
  40. method replace-with(IterationBuffer $ib --> Nil) {
  41. $!items := $ib;
  42. }
  43. }