{"id":13130,"date":"2015-12-16T08:44:22","date_gmt":"2015-12-16T13:44:22","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=13130"},"modified":"2022-10-10T16:58:57","modified_gmt":"2022-10-10T20:58:57","slug":"osspinlock-is-unsafe","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/12\/16\/osspinlock-is-unsafe\/","title":{"rendered":"OSSpinLock Is Unsafe"},"content":{"rendered":"<p><a href=\"https:\/\/forums.swift.org\/t\/thread-safety-of-weak-properties\/422\/7\">John McCall<\/a> (via <a href=\"https:\/\/twitter.com\/steipete\/status\/676851647042203648\">Peter Steinberger<\/a>):<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/thread-safety-of-weak-properties\/422\/7\"><p>Spin locks are, unfortunately, illegal on iOS, which does not guarantee progress in the face of priority inversion.<\/p><\/blockquote>\n<p><a href=\"https:\/\/forums.swift.org\/t\/thread-safety-of-weak-properties\/422\/12\">Greg Parker<\/a>:<\/p>\n<blockquote cite=\"https:\/\/forums.swift.org\/t\/thread-safety-of-weak-properties\/422\/12\"><p>OSSpinLock is unsafe unless you can guarantee that all users have the same priority.<\/p><\/blockquote>\n<p><a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/676851988596809728\">David Smith<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/Catfish_Man\/status\/676851988596809728\"><p>also applies to the new MacBooks since they will depress priority and throttle in thermal overload situations.<\/p><\/blockquote>\n<blockquote cite=\"https:\/\/twitter.com\/Catfish_Man\/status\/676852111527706624\"><p>to compensate, pthread mutexes are 2-2.5x faster than they used to be on new OSs<\/p><\/blockquote>\n<p>It&rsquo;s a shame that this is <a href=\"https:\/\/developer.apple.com\/library\/mac\/documentation\/System\/Reference\/OSAtomic_header_reference\">not documented<\/a>. But how great is it to see Apple engineers discuss these sorts of details in public?<\/p>\n<p>Update (2015-12-31): <a href=\"http:\/\/engineering.postmates.com\/Spinlocks-Considered-Harmful-On-iOS\/\">Kevin Ballard<\/a>:<\/p>\n<blockquote cite=\"http:\/\/engineering.postmates.com\/Spinlocks-Considered-Harmful-On-iOS\/\">\n<p>The reason for this comes back to the thread scheduler and QOS. You remember how I said low-priority threads will eventually execute? <a href=\"https:\/\/forums.swift.org\/t\/thread-safety-of-weak-properties\/422\/13\" title=\"[swift-dev] Re: Thread safety of weak properties\">That&rsquo;s no longer true with QOS<\/a>. More specifically, threads in a higher QOS class will never decay to a lower QOS class, and the scheduler will always prioritize runnable threads in a given QOS class before threads in lower classes. And since threads spinning on a spinlock are always runnable, this means that if there&rsquo;s enough high-QOS threads waiting on a lock held by a lower-QOS thread, the thread that owns the lock will <em>never execute<\/em>.<\/p>\n<p>[&#8230;]<\/p>\n<p>The Obj-C runtime switched to a handoff lock algorithm, where the spinlock is the size of a word and the owning thread actually stores its thread ID in the lock. Threads that block on the lock can then temporarily donate their priority to the thread that owns the lock, which fixes the priority inversion. There&rsquo;s potential issues when multiple locks are involved, but in practice it works. The only problem with this solution is <a href=\"https:\/\/forums.swift.org\/t\/thread-safety-of-weak-properties\/422\/17\" title=\"[swift-dev] Re: Thread safety of weak properties\">it relies on private API<\/a>, and the spinlock implementation itself isn&rsquo;t public, so there&rsquo;s no way for third-party code to use these locks.<\/p>\n<\/blockquote>\n<p>Update (2022-10-10): See also: <a href=\"https:\/\/mjtsai.com\/blog\/2022\/10\/10\/improving-firefox-responsiveness-on-macos\/\">Improving Firefox Responsiveness on macOS<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>John McCall (via Peter Steinberger): Spin locks are, unfortunately, illegal on iOS, which does not guarantee progress in the face of priority inversion. Greg Parker: OSSpinLock is unsafe unless you can guarantee that all users have the same priority. David Smith: also applies to the new MacBooks since they will depress priority and throttle in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2020-04-21T18:11:11Z","apple_news_api_id":"3abdd1f6-a15b-4b91-b566-633d768ecb2c","apple_news_api_modified_at":"2022-10-10T20:59:00Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAQ==","apple_news_api_share_url":"https:\/\/apple.news\/AOr3R9qFbS5G1ZmM9do7LLA","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[800,164,31,1137,30,1199,760,71,1942],"class_list":["post-13130","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-documentation","tag-ios","tag-ios-9","tag-mac","tag-mac-os-x-10-11","tag-objective-c-runtime","tag-programming","tag-thermal"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=13130"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13130\/revisions"}],"predecessor-version":[{"id":37293,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13130\/revisions\/37293"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=13130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=13130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=13130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}