Bethibande / actors

A actor framework and KSP processor/generator for kotlin.
Apache License 2.0
2 stars 0 forks source link

Make BehaviorMap thread-safe #1

Closed Bethibande closed 8 months ago

Bethibande commented 8 months ago

BehaviorMap is currently not thread-safe, if a BehaviorMap is modifier whilst actors already use it, the map could break or even throw exception. The behavior in such a situation is unknown. Making the class thread-safe could be challenging due to the fact that read operations occur in a coroutine context but write operations don't, also the get method used to read the map is not suspending (could be though). Simply synchronizing the map with a ReentrantReadWriteLock is not a good idea since that could lead to coroutine worker threads getting locked.

Bethibande commented 8 months ago

BehaviorMap is now synchronized using a ReentrantReadWrite lock, not ideal but should be fine as read operations can still occur concurrently and write operations to the underlying HashMap should be very fast thus only locking actor coroutines for a very short time.