<story><title>Locking in WebKit</title><url>https://webkit.org/blog/6161/locking-in-webkit/</url></story><parent_chain></parent_chain><comment><author>eridius</author><text>Two things come to mind when reading this:&lt;p&gt;1. WTF::Lock is using sequential consistency for all of the slow path operations. Surely this isn&amp;#x27;t necessary and it could still use acquire&amp;#x2F;release for most of them (like it does in the fast path).&lt;p&gt;2. Ditching OS-provided mutexes has one possibly-important drawback on OS X and iOS: lack of priority donation. Recent versions of OS X and iOS have a Quality Of Service implementation in the kernel that classifies threads (and libdispatch queues) according to one of several QOS levels, and then handles normal thread priorities within those levels. For this reason you actually cannot safely implement your own spinlock, as I documented a few months ago (&lt;a href=&quot;http:&amp;#x2F;&amp;#x2F;engineering.postmates.com&amp;#x2F;Spinlocks-Considered-Harmful-On-iOS&amp;#x2F;);&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;engineering.postmates.com&amp;#x2F;Spinlocks-Considered-Harmfu...&lt;/a&gt; it&amp;#x27;ll appear to work under many normal workloads, but once you start using the spinlock from threads of different QOS levels at the same time, you risk hitting a priority inversion livelock where a lower-QOS thread has the spinlock locked but is never scheduled because higher-QOS threads spinning on the lock are always given priority. The OS does actually have a safe spinlock (which does priority donation), but it&amp;#x27;s not public API and so can&amp;#x27;t be used outside of system code. Of course, this doesn&amp;#x27;t apply to adaptive spinlocks, since they&amp;#x27;ll just end up in the slow path of parking their thread, but this does still serve to highlight the issue they do have, which is if a high-QOS thread is parked waiting for a lock that&amp;#x27;s owned by a low-QOS thread, the high-QOS thread is effectively having its priority lowered to the low QOS until the owning thread releases the lock. This is obviously not what you want. The system-provided pthread_mutex_t does priority donation specifically to fix this issue, but WTF::Lock doesn&amp;#x27;t.</text></comment>
<story><title>Locking in WebKit</title><url>https://webkit.org/blog/6161/locking-in-webkit/</url></story><parent_chain></parent_chain><comment><author>cokernel_hacker</author><text>Spinning in locks are always tricky business.&lt;p&gt;The sharpest thorn, to me, is: what happens when you are running inside the critical section and your thread gets preempted?&lt;p&gt;Well, now the next thread which tries to acquire the lock is stuck waiting.  But waiting for what?  Waiting for the original thread to get scheduled.&lt;p&gt;Now, the OS has no idea that the original thread should get scheduled again and is free to continue scheduling more and more work items.&lt;p&gt;Fortunately for the lock in this article, and most other locks which spin, it is adaptive and will not spin for too long.  But how long is long enough?  If the spin is timed for a few loads and stores, then all is probably well as spins will not be attempted for very long.&lt;p&gt;I wonder how these locks figure out how long they should spin?  In the nasty case I previously mentioned, you&amp;#x27;d want to spin for a very short while to avoid large amounts of waste.  But spins which are too short lead to higher lock&amp;#x2F;unlock latency if the lock was held for any appreciable amount of time.&lt;p&gt;This leads me to the following conclusion: spinning inevitable leads to _some_ number of wasted CPU cycles and therefore increased latency.&lt;p&gt;I&amp;#x27;m curious as to how the amount of spinning was chosen.</text></comment>