{"id":103824,"date":"2020-06-03T07:00:00","date_gmt":"2020-06-03T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103824"},"modified":"2020-06-03T06:06:47","modified_gmt":"2020-06-03T13:06:47","slug":"20200603-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200603-00\/?p=103824","title":{"rendered":"Using fibers to expand a thread&#8217;s stack at runtime, part 2"},"content":{"rendered":"<p><a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200602-00\/?p=103819\"> Last time<\/a>, we wrote a <code>RunOnFiber<\/code> function that accepted a lambda and ran the lambda on a fiber.<\/p>\n<p>Since the <code>RunOnFiber<\/code> function is templated, a new copy of the function is created for each lambda. But we can reduce the code size explosion by factoring out the part that is independent of the lambda.<\/p>\n<p>We use a technique similar to the one we used when we <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200515-00\/?p=103755\"> wrote our own simplified version of <code>std::function<\/code><\/a>: Convert the lambda to a flat callback and a <code>void*<\/code>.<\/p>\n<pre><span style=\"color: blue;\">HRESULT RunOnFiberWorker(\r\n    HRESULT (*callback)(void*),\r\n    void* parameter)<\/span>\r\n{\r\n  struct State\r\n  {\r\n    <span style=\"color: blue;\">HRESULT (*callback)(void*);\r\n    void* parameter;<\/span>\r\n    HANDLE originalFiber;\r\n    HRESULT result = S_OK;\r\n\r\n    void FiberProc()\r\n    {\r\n      result = <span style=\"color: blue;\">callback(parameter)<\/span>;\r\n      SwitchToFiber(originalFiber);\r\n    }\r\n\r\n  } state{ <span style=\"color: blue;\">callback, parameter<\/span> };\r\n\r\n  unique_fiber workFiber{ CreateFiberEx(0, EXTRA_STACK_SIZE, 0,\r\n      [](void* parameter)\r\n      {\r\n        reinterpret_cast&lt;State*&gt;(parameter)-&gt;FiberProc();\r\n      }, &amp;state) };\r\n\r\n  if (!workFiber) return HRESULT_FROM_WIN32(GetLastError());\r\n\r\n  unique_thread_as_fiber threadFiber;\r\n  if (!IsThreadAFiber()) {\r\n    threadFiber.reset(ConvertThreadToFiber(nullptr));\r\n    if (!threadFiber) {\r\n      return HRESULT_FROM_WIN32(GetLastError());\r\n    }\r\n  }\r\n\r\n  state.originalFiber = GetCurrentFiber();\r\n  SwitchToFiber(workFiber.get());\r\n\r\n  return state.result;\r\n}\r\n\r\ntemplate&lt;typename Lambda&gt;\r\nHRESULT RunOnFiber(Lambda&amp;&amp; lambda)\r\n{\r\n  <span style=\"color: blue;\">using Type = std::remove_reference_t&lt;Lambda&gt;;\r\n  return RunOnFiberWorker([](void* parameter)\r\n    {\r\n      return (*reinterpret_cast&lt;Type*&gt;(parameter))();\r\n    }, &amp;lambda);<\/span>\r\n}\r\n<\/pre>\n<p>The boilerplate is now in a helper function called <code>Run\u00adOn\u00adFiber\u00adWorker<\/code>, and the template function type-erases the lambda into a <code>void*<\/code> and callback function. The callback function converts the <code>void*<\/code> back into the lambda and invokes it.<\/p>\n<p>The decomposition of the lambda into a callback and <code>void*<\/code> allows the same <code>Run\u00adOn\u00adFiber\u00adWorker<\/code> to be used for all lambdas. The lambda-specific code is just in the production of the callback function.<\/p>\n<p>This code is not quite finished yet, because there&#8217;s the case where the lambda is a functor passed by const reference, in which case we need to respect const-ness and invoke the const version of the <code>operator()<\/code> overload.<\/p>\n<p>There also the oddball case where the functor has an overloaded <code>operator&amp;<\/code>. We can avoid that by using <code>std::<code><\/code>addressof<\/code>.<\/p>\n<p>A little bit of additional fiddling will take care of that:<\/p>\n<pre>template&lt;typename Lambda&gt;\r\nHRESULT RunOnFiber(Lambda&amp;&amp; lambda)\r\n{\r\n  using Type = std::remove_reference_t&lt;Lambda&gt;;\r\n  <span style=\"color: blue;\">using Decayed = std::remove_cv_t&lt;Type&gt;;<\/span>\r\n  return RunOnFiberWorker([](void* parameter)\r\n    {\r\n      return (*reinterpret_cast&lt;Type*&gt;(parameter))();\r\n    }, <span style=\"color: blue;\">const_cast&lt;Decayed*&gt;<\/span>(<span style=\"color: blue;\">std::addressof<\/span>(lambda)));\r\n}\r\n<\/pre>\n<p>Next time, we&#8217;ll look at writing a <code>Run\u00adOn\u00adFiber<\/code> function that reports errors by means other than just the return value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reducing code size with type erasure.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-103824","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Reducing code size with type erasure.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=103824"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103824\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=103824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}