<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;">
<div><font face="UICTFontTextStyleBody" size="4">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.</font><br /></div>
</div>
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;"><br />
On Jun 13, 2017, 1:39 PM -0400, Kevin Nattinger via swift-evolution &lt;swift-evolution@swift.org&gt;, wrote:<br />
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #1abc9c;">
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
<div style="color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">I'd prefer to see block-scoped synchronization rather than whole-method-only; it gives much more flexibility.</span></span></div>
<div style="color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class="" /></span></div>
<div style="color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">Note that you can use the objc synchronization feature with reference types:</span></div>
<div class=""><br class="" /></div>
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class="">
<div style="margin: 0px; line-height: normal;" class="">
<div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// Or (_ obj:...) if you prefer the label-less objc style)</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span> <span style="font-variant-ligatures: no-common-ligatures" class="">synchronized(on obj:</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">AnyObject</span><span style="font-variant-ligatures: no-common-ligatures" class="">, do block: ()</span> <span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">throws</span> <span style="font-variant-ligatures: no-common-ligatures" class="">-&gt;</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Void</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span> <span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">rethrows</span> <span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(62, 30, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&#160; &#160;</span> <span style="font-variant-ligatures: no-common-ligatures" class="">objc_sync_enter</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(obj)</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&#160; &#160;</span> <span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">defer</span> <span style="font-variant-ligatures: no-common-ligatures" class="">{</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(62, 30, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&#160; &#160; &#160; &#160;</span> <span style="font-variant-ligatures: no-common-ligatures" class="">objc_sync_exit</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(obj)</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&#160; &#160; }</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&#160; &#160;</span> <span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">try</span> <span style="font-variant-ligatures: no-common-ligatures" class="">block()</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div>
<div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class="" /></span></div>
</div>
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
<div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class="">
<div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class="">
<div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">// or synchronized(self)</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">synchronized</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(on:</span> <span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) {</span></div>
<div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&#160; &#160;</span> <span style="font-variant-ligatures: no-common-ligatures" class="">// do something</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">try</span> <span style="font-variant-ligatures: no-common-ligatures" class="">synchronized</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(on:</span> <span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) {</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&#160; &#160;</span> <span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">throw</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSError</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div>
<div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div>
<div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class="" /></span></div>
</div>
<div style="margin: 0px; line-height: normal; color: rgb(0, 0, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Not 100% sure this works with bridged reference types (e.g. Array); definitely doesn't work with Int. Use a dummy NSObject() if needed.</span></div>
<div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class="" /></span></div>
</div>
<div class="">That said, I'd love to see a swift-native solution that works with value types (and doesn't rely on the objc runtime).</div>
<div class=""><br class="" /></div>
</div>
<div>
<blockquote type="cite" class="" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e67e22;">
<div class="">On Jun 12, 2017, at 2:10 AM, Erik Aigner via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div>
<div class="">
<div class="">[...]<br class="" />
With synchronized attribute (the semaphore/wait/deferred-signal is synthesized by Swift automatically)<br class="" />
<br class="" />
class Obj {<br class="" />
<br class="" />
synchronized func method() {<br class="" />
// semaphore is synthesized automatically, do something…<br class="" />
}<br class="" />
}<br class="" />
<br class="" /></div>
</div>
</blockquote>
</div>
<br class="" />
_______________________________________________<br />
swift-evolution mailing list<br />
swift-evolution@swift.org<br />
https://lists.swift.org/mailman/listinfo/swift-evolution<br /></blockquote>
</div>
</body>
</html>