activeactors.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include <library/cpp/actors/core/actor.h>
  3. #include <library/cpp/actors/core/events.h>
  4. #include <util/generic/hash_set.h>
  5. namespace NActors {
  6. ////////////////////////////////////////////////////////////////////////////
  7. // TActiveActors
  8. // This class helps manage created actors and kill them all on PoisonPill.
  9. ////////////////////////////////////////////////////////////////////////////
  10. class TActiveActors : public THashSet<TActorId> {
  11. public:
  12. void Insert(const TActorId &aid) {
  13. bool inserted = insert(aid).second;
  14. Y_VERIFY(inserted);
  15. }
  16. void Insert(const TActiveActors &moreActors) {
  17. for (const auto &aid : moreActors) {
  18. Insert(aid);
  19. }
  20. }
  21. void Erase(const TActorId &aid) {
  22. auto num = erase(aid);
  23. Y_VERIFY(num == 1);
  24. }
  25. size_t KillAndClear(const TActorContext &ctx) {
  26. size_t s = size(); // number of actors managed
  27. for (const auto &x: *this) {
  28. ctx.Send(x, new TEvents::TEvPoisonPill());
  29. }
  30. clear();
  31. return s; // how many actors we killed
  32. }
  33. };
  34. } // NKikimr