Let’s Build @synchronized
Apple’s implementation of
@synchronized
is available as part of the Objective-C runtime source distribution. This specific bit is available here.It’s build for speed rather than simplicity as the above toy implementation is. It’s interesting to see what it does the same and what it does differently.
The basic concept is the same. There’s a global table that maps object pointers to locks, and the lock is then locked and unlocked around the
@synchronized
block.
I love reading about Apple’s runtime optimization techniques.