Skip to content

Instantly share code, notes, and snippets.

@godrm
Created November 8, 2020 03:46
Show Gist options
  • Save godrm/02ea5a352a4b22e38af1af32a4bcbd34 to your computer and use it in GitHub Desktop.
Save godrm/02ea5a352a4b22e38af1af32a4bcbd34 to your computer and use it in GitHub Desktop.
RacyActor Phase#2
var racyGlobal: [String] = []
@MyGlobalActor
var safeGlobal: [String] = []
class PlainOldClass {
var unprotectedState: String = []
}
actor class RacyActor {
let immutableClassReference: PlainOldClass
func racyFunction(other: RacyActor) async {
// protected: global variable protected by a global actor
safeGlobal += ["Safe access"]
// unprotected: global variable not in an actor
racyGlobal += ["Racy access"]
// unprotected: racyProperty is immutable, but it is a reference type
// so it allows access to unprotected shared mutable type
other.takeClass(immutableClassReference)
}
func takeClass(_ plainClass: PlainOldClass) {
plainClass.unprotectedState += ["Racy access"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment