{"id":17219,"date":"2017-02-15T14:38:53","date_gmt":"2017-02-15T19:38:53","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=17219"},"modified":"2017-02-15T14:48:32","modified_gmt":"2017-02-15T19:48:32","slug":"grand-central-dispatchs-achilles-heel","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/02\/15\/grand-central-dispatchs-achilles-heel\/","title":{"rendered":"Grand Central Dispatch&rsquo;s Achilles Heel"},"content":{"rendered":"<p><a href=\"http:\/\/blog.wilshipley.com\/2017\/02\/pimp-my-code-book-2-grand-central.html\">Wil Shipley<\/a> (<a href=\"https:\/\/twitter.com\/wilshipley\/status\/831665898394038273\">tweet<\/a>):<\/p>\n<blockquote cite=\"http:\/\/blog.wilshipley.com\/2017\/02\/pimp-my-code-book-2-grand-central.html\">\n<p>I don&rsquo;t know much about the internals of GCD so I can&rsquo;t speak with authority, but it <em>seems<\/em> like this could be solved with a couple of minor changes to sync(): figure out if the destination queue is the current queue, and if so just execute the submitted block immediately and return. This wouldn&rsquo;t even be a source or binary-breaking change, because, again, the current behavior is HANG the app.<\/p>\n<p>And, in fact, this is the workaround third-party programmers have made for the last several years. If you do a Google search for <code>dispatch_get_current_queue<\/code> [<a href=\"http:\/\/stackoverflow.com\/q\/23955948\/6311\">now<\/a> <a href=\"http:\/\/stackoverflow.com\/q\/13237417\/6311\">deprecated<\/a>] you&rsquo;ll see a bunch of developers complaining about that call disappearing because they were using it for this hack.<\/p>\n<p>[&#8230;]<\/p>\n<p>Hopefully you&rsquo;re as horrified by this mess as I am. This is the very model of spaghetti code. Last week I ported this file from macOS 10.8 to 10.12 and honestly I still couldn&rsquo;t come up with a good way to re-architect it. I&rsquo;m bending over backwards to interact with the main thread in multiple places in this codebase and I&rsquo;m not sure if I&rsquo;m on the main thread or not and it&rsquo;s a nightmare.<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/twitter.com\/gparker\/status\/831672896099540993\">Greg Parker<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/gparker\/status\/831672896099540993\"><p><code>dispatch_get_current_queue() == someQueue<\/code> is insufficient to avoid the deadlocks you describe.<\/p><\/blockquote>\n<p><a href=\"https:\/\/twitter.com\/cocoaphony\/status\/831862298566352896\">Rob Napier<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/cocoaphony\/status\/831862298566352896\"><p>Interestingly, they <em>did<\/em> fix this in CoreData on top of GCD. <code>performAndWait<\/code> is reentrant. But unsure how they implemented.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Wil Shipley (tweet): I don&rsquo;t know much about the internals of GCD so I can&rsquo;t speak with authority, but it seems like this could be solved with a couple of minor changes to sync(): figure out if the destination queue is the current queue, and if so just execute the submitted block immediately and return. [&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":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","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,109,880,31,1380,30,1381,71,901],"class_list":["post-17219","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-coredata","tag-grand-central-dispatch-gcd","tag-ios","tag-ios-10","tag-mac","tag-macos-10-12","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17219","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=17219"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17219\/revisions"}],"predecessor-version":[{"id":17221,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17219\/revisions\/17221"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}