actor.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "actor.h"
  2. #include "executor_thread.h"
  3. #include "mailbox.h"
  4. #include <library/cpp/actors/util/datetime.h>
  5. namespace NActors {
  6. Y_POD_THREAD(TActivationContext*)
  7. TlsActivationContext((TActivationContext*)nullptr);
  8. bool TActorContext::Send(const TActorId& recipient, IEventBase* ev, ui32 flags, ui64 cookie, NWilson::TTraceId traceId) const {
  9. return Send(new IEventHandle(recipient, SelfID, ev, flags, cookie, nullptr, std::move(traceId)));
  10. }
  11. bool TActorContext::Send(TAutoPtr<IEventHandle> ev) const {
  12. return ExecutorThread.Send(ev);
  13. }
  14. void IActor::Registered(TActorSystem* sys, const TActorId& owner) {
  15. // fallback to legacy method, do not use it anymore
  16. if (auto eh = AfterRegister(SelfId(), owner))
  17. sys->Send(eh);
  18. }
  19. void IActor::Describe(IOutputStream &out) const noexcept {
  20. SelfActorId.Out(out);
  21. }
  22. bool IActor::Send(const TActorId& recipient, IEventBase* ev, ui32 flags, ui64 cookie, NWilson::TTraceId traceId) const noexcept {
  23. return SelfActorId.Send(recipient, ev, flags, cookie, std::move(traceId));
  24. }
  25. bool TActivationContext::Send(TAutoPtr<IEventHandle> ev) {
  26. return TlsActivationContext->ExecutorThread.Send(ev);
  27. }
  28. void TActivationContext::Schedule(TInstant deadline, TAutoPtr<IEventHandle> ev, ISchedulerCookie* cookie) {
  29. TlsActivationContext->ExecutorThread.Schedule(deadline, ev, cookie);
  30. }
  31. void TActivationContext::Schedule(TMonotonic deadline, TAutoPtr<IEventHandle> ev, ISchedulerCookie* cookie) {
  32. TlsActivationContext->ExecutorThread.Schedule(deadline, ev, cookie);
  33. }
  34. void TActivationContext::Schedule(TDuration delta, TAutoPtr<IEventHandle> ev, ISchedulerCookie* cookie) {
  35. TlsActivationContext->ExecutorThread.Schedule(delta, ev, cookie);
  36. }
  37. bool TActorIdentity::Send(const TActorId& recipient, IEventBase* ev, ui32 flags, ui64 cookie, NWilson::TTraceId traceId) const {
  38. return TActivationContext::Send(new IEventHandle(recipient, *this, ev, flags, cookie, nullptr, std::move(traceId)));
  39. }
  40. void TActorIdentity::Schedule(TInstant deadline, IEventBase* ev, ISchedulerCookie* cookie) const {
  41. return TActivationContext::Schedule(deadline, new IEventHandle(*this, {}, ev), cookie);
  42. }
  43. void TActorIdentity::Schedule(TMonotonic deadline, IEventBase* ev, ISchedulerCookie* cookie) const {
  44. return TActivationContext::Schedule(deadline, new IEventHandle(*this, {}, ev), cookie);
  45. }
  46. void TActorIdentity::Schedule(TDuration delta, IEventBase* ev, ISchedulerCookie* cookie) const {
  47. return TActivationContext::Schedule(delta, new IEventHandle(*this, {}, ev), cookie);
  48. }
  49. TActorId TActivationContext::RegisterWithSameMailbox(IActor* actor, TActorId parentId) {
  50. Y_VERIFY_DEBUG(parentId);
  51. auto& ctx = *TlsActivationContext;
  52. return ctx.ExecutorThread.RegisterActor(actor, &ctx.Mailbox, parentId.Hint(), parentId);
  53. }
  54. TActorId TActorContext::RegisterWithSameMailbox(IActor* actor) const {
  55. return ExecutorThread.RegisterActor(actor, &Mailbox, SelfID.Hint(), SelfID);
  56. }
  57. TActorId IActor::RegisterWithSameMailbox(IActor* actor) const noexcept {
  58. return TlsActivationContext->ExecutorThread.RegisterActor(actor, &TlsActivationContext->Mailbox, SelfActorId.Hint(), SelfActorId);
  59. }
  60. TActorId TActivationContext::Register(IActor* actor, TActorId parentId, TMailboxType::EType mailboxType, ui32 poolId) {
  61. return TlsActivationContext->ExecutorThread.RegisterActor(actor, mailboxType, poolId, parentId);
  62. }
  63. TActorId TActivationContext::InterconnectProxy(ui32 destinationNodeId) {
  64. return TlsActivationContext->ExecutorThread.ActorSystem->InterconnectProxy(destinationNodeId);
  65. }
  66. TActorSystem* TActivationContext::ActorSystem() {
  67. return TlsActivationContext->ExecutorThread.ActorSystem;
  68. }
  69. i64 TActivationContext::GetCurrentEventTicks() {
  70. return GetCycleCountFast() - TlsActivationContext->EventStart;
  71. }
  72. double TActivationContext::GetCurrentEventTicksAsSeconds() {
  73. return NHPTimer::GetSeconds(GetCurrentEventTicks());
  74. }
  75. TActorId TActorContext::Register(IActor* actor, TMailboxType::EType mailboxType, ui32 poolId) const {
  76. return ExecutorThread.RegisterActor(actor, mailboxType, poolId, SelfID);
  77. }
  78. TActorId IActor::Register(IActor* actor, TMailboxType::EType mailboxType, ui32 poolId) const noexcept {
  79. return TlsActivationContext->ExecutorThread.RegisterActor(actor, mailboxType, poolId, SelfActorId);
  80. }
  81. void TActorContext::Schedule(TInstant deadline, IEventBase* ev, ISchedulerCookie* cookie) const {
  82. ExecutorThread.Schedule(deadline, new IEventHandle(SelfID, TActorId(), ev), cookie);
  83. }
  84. void TActorContext::Schedule(TMonotonic deadline, IEventBase* ev, ISchedulerCookie* cookie) const {
  85. ExecutorThread.Schedule(deadline, new IEventHandle(SelfID, TActorId(), ev), cookie);
  86. }
  87. void TActorContext::Schedule(TDuration delta, IEventBase* ev, ISchedulerCookie* cookie) const {
  88. ExecutorThread.Schedule(delta, new IEventHandle(SelfID, TActorId(), ev), cookie);
  89. }
  90. void IActor::Schedule(TInstant deadline, IEventBase* ev, ISchedulerCookie* cookie) const noexcept {
  91. TlsActivationContext->ExecutorThread.Schedule(deadline, new IEventHandle(SelfActorId, TActorId(), ev), cookie);
  92. }
  93. void IActor::Schedule(TMonotonic deadline, IEventBase* ev, ISchedulerCookie* cookie) const noexcept {
  94. TlsActivationContext->ExecutorThread.Schedule(deadline, new IEventHandle(SelfActorId, TActorId(), ev), cookie);
  95. }
  96. void IActor::Schedule(TDuration delta, IEventBase* ev, ISchedulerCookie* cookie) const noexcept {
  97. TlsActivationContext->ExecutorThread.Schedule(delta, new IEventHandle(SelfActorId, TActorId(), ev), cookie);
  98. }
  99. TInstant TActivationContext::Now() {
  100. return TlsActivationContext->ExecutorThread.ActorSystem->Timestamp();
  101. }
  102. TMonotonic TActivationContext::Monotonic() {
  103. return TlsActivationContext->ExecutorThread.ActorSystem->Monotonic();
  104. }
  105. TInstant TActorContext::Now() const {
  106. return ExecutorThread.ActorSystem->Timestamp();
  107. }
  108. TMonotonic TActorContext::Monotonic() const {
  109. return ExecutorThread.ActorSystem->Monotonic();
  110. }
  111. NLog::TSettings* TActivationContext::LoggerSettings() const {
  112. return ExecutorThread.ActorSystem->LoggerSettings();
  113. }
  114. std::pair<ui32, ui32> TActorContext::CountMailboxEvents(ui32 maxTraverse) const {
  115. return Mailbox.CountMailboxEvents(SelfID.LocalId(), maxTraverse);
  116. }
  117. std::pair<ui32, ui32> IActor::CountMailboxEvents(ui32 maxTraverse) const {
  118. return TlsActivationContext->Mailbox.CountMailboxEvents(SelfActorId.LocalId(), maxTraverse);
  119. }
  120. void IActor::Die(const TActorContext& ctx) {
  121. if (ctx.SelfID)
  122. Y_VERIFY(ctx.SelfID == SelfActorId);
  123. PassAway();
  124. }
  125. void IActor::PassAway() {
  126. auto& cx = *TlsActivationContext;
  127. cx.ExecutorThread.UnregisterActor(&cx.Mailbox, SelfActorId.LocalId());
  128. }
  129. double IActor::GetElapsedTicksAsSeconds() const {
  130. return NHPTimer::GetSeconds(ElapsedTicks);
  131. }
  132. }