<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Sep 11, 2017, at 9:00 PM, Chris Lattner via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">On Sep 4, 2017, at 12:18 PM, Pierre Habouzit <<a href="mailto:phabouzit@apple.com" class="">phabouzit@apple.com</a>> wrote:<br class=""><div class=""><blockquote type="cite" class="">Something else I realized, is that this code is fundamentally broken in swift:<br class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class="">actor func foo()</div></div><div class=""><div class="">{</div></div><div class=""><div class=""> NSLock *lock = NSLock();</div></div><div class=""><div class=""> lock.lock();</div></div><div class=""><div class=""><br class=""></div></div><div class=""><div class=""> let compute = await someCompute(); <--- this will really break `foo` in two pieces of code that can execute on two different physical threads.</div></div><div class=""><div class=""> lock.unlock();</div></div><div class=""><div class="">}</div></div></blockquote><div class=""><div class=""><br class=""></div><div class=""><br class=""></div><div class="">The reason why it is broken is that mutexes (whether it's NSLock, pthread_mutex, os_unfair_lock) have to be unlocked from the same thread that took it. the await right in the middle here means that we can't guarantee it.</div></div></div></div></blockquote><div class=""><br class=""></div><div class="">Agreed, this is just as broken as:</div><div class=""><br class=""></div><div class="">func foo()<br class="">{<br class=""> let lock = NSLock()<br class=""> lock.lock()<br class=""><br class=""> someCompute {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> lock.unlock()</div><div class=""> }<br class="">}</div><div class=""><br class=""></div><div class="">and it is just as broken as trying to do the same thing across queues. Stuff like this, or the use of TLS, is just inherently broken, both with GCD and with any sensible model underlying actors. Trying to fix this is not worth it IMO, it is better to be clear that they are different things and that (as a programmer) you should *expect* your tasks to run on multiple kernel threads.</div><div class=""><br class=""></div><div class="">BTW, why are you using a lock in a single threaded context in the first place??? ;-)</div></div></div></div></blockquote><div><br class=""></div>I don't do locks, I do atomics as a living.</div><div><br class=""></div><div>Joke aside, it's easy to write this bug we should try to have the compiler/analyzer help here for these broken patterns.</div><div>TSD is IMO less of a problem because people using them are aware of their sharp edges. Not so much for locks.</div><div><br class=""></div><div>-Pierre</div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div><div class="">-Chris</div><div class=""><br class=""></div></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>