[swift-evolution] Introducing synthesized locks
David Moore
mooredev at me.com
Tue Jun 13 17:48:11 CDT 2017
Synchronization would indeed be an area of improvement for Swift. I would, however, add that the primitive should also work with more than just functions. For example, it should play nicely with closures and should most definitely work with properties (to synchronize access for a given resource value). Furthermore, I would like to suggest a change of name from ‘synchronized’ to ‘exclusive’, as it fits more tightly with the new exclusivity rules in Swift 4.
On Jun 13, 2017, 1:39 PM -0400, Kevin Nattinger via swift-evolution <swift-evolution at swift.org>, wrote:
> I'd prefer to see block-scoped synchronization rather than whole-method-only; it gives much more flexibility.
>
> Note that you can use the objc synchronization feature with reference types:
>
> // Or (_ obj:...) if you prefer the label-less objc style)
> func synchronized(on obj: AnyObject, do block: () throws -> Void) rethrows {
> objc_sync_enter(obj)
> defer {
> objc_sync_exit(obj)
> }
> try block()
> }
>
> // or synchronized(self)
> synchronized(on: self) {
> // do something
> }
> try synchronized(on: self) {
> throw NSError()
> }
>
> Not 100% sure this works with bridged reference types (e.g. Array); definitely doesn't work with Int. Use a dummy NSObject() if needed.
>
> That said, I'd love to see a swift-native solution that works with value types (and doesn't rely on the objc runtime).
>
> > On Jun 12, 2017, at 2:10 AM, Erik Aigner via swift-evolution <swift-evolution at swift.org> wrote:
> > [...]
> > With synchronized attribute (the semaphore/wait/deferred-signal is synthesized by Swift automatically)
> >
> > class Obj {
> >
> > synchronized func method() {
> > // semaphore is synthesized automatically, do something…
> > }
> > }
> >
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170613/b89215a0/attachment.html>
More information about the swift-evolution
mailing list