subconsciousnetwork / subconscious

Apache License 2.0
6 stars 0 forks source link

Isolate Noosphere wrappers to shared global actor #1132

Closed gordonbrander closed 4 months ago

gordonbrander commented 4 months ago

As of XCode 15.3 (15E204a) we are getting a slew of new warnings about OpaquePointers in the Noosphere wrappers.

Problem

The problem stems from our Noosphere wrappers being individual actors, and OpaquePointers not being a sendable type (e.g. they should not cross state synchronization boundaries). That makes sense.

Design considerations

We implement Noosphere, Sphere, and SphereFile as actors because:

Since they're currently separate actors, we're passing OpaquePointers across actor boundaries. Anything passed across actor boundaries should be sendable (e.g. no shared mutable state. However, it's not really necessary that these be separate actors, just that they have actor isolation. Preferably, we should have them isolated to the same actor.

Solution: global actor

Swift introduced @globalActor to solve for this kind of "many things isolated to the same actor context" problem.

By wrapping a new NoosphereActor shared actor in @globalActor and conforming to the protocol, we get a @NoosphereActor decorator we can use to isolate classes and functions.

Noosphere, Sphere, and SphereFile all become final classes, isolated. to @globalActor.

Resources