In the official Akka doc, they disclaim:
To prevent visibility and reordering problems on actors, Akka guarantees the following two "happens before" rules:
The actor send rule: the send of the message to an actor happens before the receive of that message by the same actor. The actor subsequent processing rule: processing of one message happens before processing of the next message by the same actor.
Please ref the doc for details.
I'm wondering how does Akka achive this. I just went through the source code(latest at this moment), and I thought there should be a Lock before execute Actor.receive
, but I didn't find any locks(I think). Finally, I found a comment for ActorCell.invoke
:
//Memory consistency is handled by the Mailbox (reading mailbox status then processing messages, then writing mailbox status
Yeah, the Mailbox.status
, I guess this is what I'm looking for. I saw they use Unsafe
to access/update the status
field, but I just couldn't figure out how can this ensure the memory visibility.