<html><body><p>Hi,<br><br>Following up on the blocks discussion.  I looked at the various Block header files and it looked there was a good chance things would just work.  So, I did a quick &amp; dirty hack of copying the .c and .h from foundation/closure into libdispatch/src/closure, building them, and linking with them instead of with libBlocksRuntime.  All of the libdispatch tests that were working before, still work (not surprising), so it seems promising.<br><br>I speculate we would prefer to not have multiple copies of the blocks runtime used by an application.  Therefore we need to get libdispatch and libFoundation to use the same one.   The natural path would be to just build a blocks runtime library from the sources in foundation/closure and have foundation and dispatch link against that library and compile against its headers.   I'd further guess that it would be preferred to leave the files in foundation/closure (not create some new project) and tweak the foundation build process so that it is possible to invoke it to build just the blocks runtime as a separate step.  <br><br>Does this sound plausible?  If so, I can start working on it.  Or is some other approach better?<br><br>thanks,<br><br>--dave<br><br><br><img width="16" height="16" src="cid:1__=0ABBF5D1DFE4C5408f9e8a93df938690918c0AB@" border="0" alt="Inactive hide details for Pierre Habouzit ---01/14/2016 10:09:28 PM---well dispatch also uses a bit of the blocks runtime becau"><font color="#424282">Pierre Habouzit ---01/14/2016 10:09:28 PM---well dispatch also uses a bit of the blocks runtime because it uncorks the blocks and gets to the ca</font><br><br><font size="2" color="#5F5F5F">From:        </font><font size="2">Pierre Habouzit &lt;phabouzit@apple.com&gt;</font><br><font size="2" color="#5F5F5F">To:        </font><font size="2">Philippe Hausler &lt;phausler@apple.com&gt;</font><br><font size="2" color="#5F5F5F">Cc:        </font><font size="2">David P Grove/Watson/IBM@IBMUS, Swift Core Libs &lt;swift-corelibs-dev@swift.org&gt;</font><br><font size="2" color="#5F5F5F">Date:        </font><font size="2">01/14/2016 10:09 PM</font><br><font size="2" color="#5F5F5F">Subject:        </font><font size="2">Re: [swift-corelibs-dev] libdispatch prep for integration to the rest of swift-corelibs</font><br><font size="2" color="#5F5F5F">Sent by:        </font><font size="2">phabouzit@apple.com</font><br><hr width="100%" size="2" align="left" noshade style="color:#8091A5; "><br><br><br><font size="4">well dispatch also uses a bit of the blocks runtime because it uncorks the blocks and gets to the captured function pointer.</font><br><br><font size="4">This is used pervasively, through this macro (internal.h):</font><br><br><font size="2" color="#D28F5A">#define _dispatch_Block_invoke(bb) \</font><br><font size="2" color="#D28F5A">((dispatch_function_t)((struct Block_layout *)bb)-&gt;invoke)</font><br><br><font size="4">And we need to know the size of the Block_layout for a simple block (abused in _dispatch_block_get_data(), see inline_internal.h).</font><br><br><font size="4">technically speaking dispatch only cares about the offsets/sizes and not about the rest of the layout.</font><br><br><font size="4">-Pierre </font><br>
<ul><ul><font size="4">On Jan 14, 2016, at 3:00 PM, Philippe Hausler via swift-corelibs-dev &lt;</font><a href="mailto:swift-corelibs-dev@swift.org"><u><font size="4" color="#0000FF">swift-corelibs-dev@swift.org</font></u></a><font size="4">&gt; wrote:</font><br><br><font size="4">As a note: our closure implementation in Foundation does NOT adhere to this (since it would mean that we would need to alter the c compiler to do so and that was not something that I have gotten to look at beyond a cursory glance.</font><br><br><font size="4">So the change would be effectively something like this:</font><br><br><font size="4">struct Block_layout {<br>    void *isa;<br>#if DEPLOYMENT_RUNTIME_SWIFT<br>    uint32_t refCount;<br>    uint32_t weakRefCount;<br>#else // not certain this branch can happen or not. we may need both</font><br><font size="4">    volatile int32_t flags; // contains ref count<br>    int32_t reserved;</font><br><font size="4">#endif</font><br><font size="4">    void (*invoke)(void *, ...);<br>    struct Block_descriptor_1 *descriptor;<br>    // imported variables<br>};</font><br><br><font size="4">and making the retain and release methods for blocks just call swift_retain and swift_release. But since blocks may be emitted by the compiler we would need to make certain that the compiler had a flag to emit those with the new layout and emit correct values for the refCount fields to give it immortality for things like global static blocks etc.</font><br><br><font size="4">Then we would need to find some way to have the values for:</font><br><br><font size="4">void * _NSConcreteStackBlock[32] = { 0 };<br>void * _NSConcreteMallocBlock[32] = { 0 };<br>void * _NSConcreteAutoBlock[32] = { 0 };<br>void * _NSConcreteFinalizingBlock[32] = { 0 };<br>void * _NSConcreteGlobalBlock[32] = { 0 };<br>void * _NSConcreteWeakBlockVariable[32] = { 0 };</font><br><br><font size="4">these guys emitted as objects as well so that the isa’s would be treated correctly.</font><br><br><font size="4">I am not certain on how much of this lofty goal is actually needed; perhaps we should loop in one of the compiler team in on this to query what really needs to be done to properly support the @convention(block) syntax on linux. Perhaps I am overthinking this.</font><br>
<ul><ul><font size="4">On Jan 14, 2016, at 2:49 PM, David P Grove &lt;</font><a href="mailto:groved@us.ibm.com"><u><font size="4" color="#0000FF">groved@us.ibm.com</font></u></a><font size="4">&gt; wrote:</font><br>
<p><a href="mailto:phausler@apple.com"><tt><u><font size="4" color="#0000FF">phausler@apple.com</font></u></tt></a><tt><font size="4">&nbsp;wrote on 01/13/2016 06:04:23 PM:<br>&gt; <br>&gt; Tony brought up an important point about the prep for integration <br>&gt; this morning: the blocks runtime from libblocksruntime-dev will be <br>&gt; incompatible with the layout of blocks referenced from swift since <br>&gt; the object size there is 2 uint32’s bigger to handle the RR (we <br>&gt; don’t have objc). Without modifying clang and the runtime we won’t <br>&gt; have a way to properly handoff blocks back and forth w/o objc.</font></tt><font size="4"><br></font><tt><font size="4"><br>Hi,</font></tt><font size="4"><br></font><tt><font size="4"><br>I'm guessing RR expands to Retain Release?</font></tt><font size="4"><br></font><tt><font size="4"><br>I've started exploring the source in swift-corelibs-foundation/closure. &nbsp;Any chance there is a design doc laying out the object model used by the Swift implementation? &nbsp;I'm sure I could figure it out from the source code, but it would be nice to be able to cheat and start with a an overview.</font></tt><font size="4"><br></font><tt><font size="4"><br>thanks,</font></tt><font size="4"><br></font><tt><font size="4"><br>--dave</font></tt><font size="4"><br></font><tt><font size="4"><br>&gt; <br>&gt; I am currently taking a look at this to see what we can do to add an<br>&gt; option to the clang code-gen to properly emit this structural <br>&gt; difference. This isn’t a big issue for Foundation to CF since we <br>&gt; don’t have many block APIs but dispatch is mostly blocks and that <br>&gt; might pose an issue.<br>&gt; </font></tt><font size="4"><br></font></ul></ul><font size="4"><br></font><br><font size="4">_______________________________________________<br>swift-corelibs-dev mailing list</font><u><font size="4" color="#0000FF"><br></font></u><a href="mailto:swift-corelibs-dev@swift.org"><u><font size="4" color="#0000FF">swift-corelibs-dev@swift.org</font></u></a><font size="4"><br></font><font size="4"><a href="https://lists.swift.org/mailman/listinfo/swift-corelibs-dev">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev</a></font></ul></ul><br><br><BR>
</body></html>