<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Jan 22, 2016, at 2:22 PM, David P Grove <<a href="mailto:groved@us.ibm.com" class="">groved@us.ibm.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><p class="">Hi,<br class=""><br class="">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 & 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 class=""><br class="">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 class=""><br class="">Does this sound plausible? If so, I can start working on it. Or is some other approach better?<br class=""></p></div></div></blockquote><div><br class=""></div><div>yes, I think we should have libdispatch build-system have an option to use the libclosure built by swift, as opposed to the libbllocksruntime.</div><div><br class=""></div><div>Hopefully the blocks runtime for swift understands C closures generated by clang too (but I would assume the reverse is not true).</div><div><br class=""></div><div>the things that will IMO require adaptaitons is if you call dispatch_async() from swift, that will likely break because dispatch only understands C closures today and my understanding is that the swift generated ones have a different layout.</div><div><br class=""></div><div>If the legacy blocks runtime is used, then you don’t get the swift variants working for you and that’s fine.</div><div><br class=""></div><div>Foundation/Swift people, is my expectation valid?</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><p class=""><br class="">thanks,<br class=""><br class="">--dave<br class=""><br class=""><br class=""><span id="cid:1__=0ABBF5D1DFE4C5408f9e8a93df938690918c0AB@"><graycol.gif></span><font color="#424282" class="">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 class=""><br class=""><font size="2" color="#5F5F5F" class="">From: </font><font size="2" class="">Pierre Habouzit <<a href="mailto:phabouzit@apple.com" class="">phabouzit@apple.com</a>></font><br class=""><font size="2" color="#5F5F5F" class="">To: </font><font size="2" class="">Philippe Hausler <<a href="mailto:phausler@apple.com" class="">phausler@apple.com</a>></font><br class=""><font size="2" color="#5F5F5F" class="">Cc: </font><font size="2" class="">David P Grove/Watson/IBM@IBMUS, Swift Core Libs <<a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a>></font><br class=""><font size="2" color="#5F5F5F" class="">Date: </font><font size="2" class="">01/14/2016 10:09 PM</font><br class=""><font size="2" color="#5F5F5F" class="">Subject: </font><font size="2" class="">Re: [swift-corelibs-dev] libdispatch prep for integration to the rest of swift-corelibs</font><br class=""><font size="2" color="#5F5F5F" class="">Sent by: </font><font size="2" class=""><a href="mailto:phabouzit@apple.com" class="">phabouzit@apple.com</a></font><br class=""></p><hr width="100%" size="2" align="left" noshade="" style="color:#8091A5; " class=""><br class=""><br class=""><br class=""><font size="4" class="">well dispatch also uses a bit of the blocks runtime because it uncorks the blocks and gets to the captured function pointer.</font><br class=""><br class=""><font size="4" class="">This is used pervasively, through this macro (internal.h):</font><br class=""><br class=""><font size="2" color="#D28F5A" class="">#define _dispatch_Block_invoke(bb) \</font><br class=""><font size="2" color="#D28F5A" class="">((dispatch_function_t)((struct Block_layout *)bb)->invoke)</font><br class=""><br class=""><font size="4" class="">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 class=""><br class=""><font size="4" class="">technically speaking dispatch only cares about the offsets/sizes and not about the rest of the layout.</font><br class=""><br class=""><font size="4" class="">-Pierre </font><br class="">
<ul class=""><ul class=""><font size="4" class="">On Jan 14, 2016, at 3:00 PM, Philippe Hausler via swift-corelibs-dev <</font><a href="mailto:swift-corelibs-dev@swift.org" class=""><u class=""><font size="4" color="#0000FF" class="">swift-corelibs-dev@swift.org</font></u></a><font size="4" class="">> wrote:</font><br class=""><br class=""><font size="4" class="">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 class=""><br class=""><font size="4" class="">So the change would be effectively something like this:</font><br class=""><br class=""><font size="4" class="">struct Block_layout {<br class=""> void *isa;<br class="">#if DEPLOYMENT_RUNTIME_SWIFT<br class=""> uint32_t refCount;<br class=""> uint32_t weakRefCount;<br class="">#else // not certain this branch can happen or not. we may need both</font><br class=""><font size="4" class=""> volatile int32_t flags; // contains ref count<br class=""> int32_t reserved;</font><br class=""><font size="4" class="">#endif</font><br class=""><font size="4" class=""> void (*invoke)(void *, ...);<br class=""> struct Block_descriptor_1 *descriptor;<br class=""> // imported variables<br class="">};</font><br class=""><br class=""><font size="4" class="">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 class=""><br class=""><font size="4" class="">Then we would need to find some way to have the values for:</font><br class=""><br class=""><font size="4" class="">void * _NSConcreteStackBlock[32] = { 0 };<br class="">void * _NSConcreteMallocBlock[32] = { 0 };<br class="">void * _NSConcreteAutoBlock[32] = { 0 };<br class="">void * _NSConcreteFinalizingBlock[32] = { 0 };<br class="">void * _NSConcreteGlobalBlock[32] = { 0 };<br class="">void * _NSConcreteWeakBlockVariable[32] = { 0 };</font><br class=""><br class=""><font size="4" class="">these guys emitted as objects as well so that the isa’s would be treated correctly.</font><br class=""><br class=""><font size="4" class="">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 class="">
<ul class=""><ul class=""><font size="4" class="">On Jan 14, 2016, at 2:49 PM, David P Grove <</font><a href="mailto:groved@us.ibm.com" class=""><u class=""><font size="4" color="#0000FF" class="">groved@us.ibm.com</font></u></a><font size="4" class="">> wrote:</font><br class=""><p class=""><a href="mailto:phausler@apple.com" class=""><tt class=""><u class=""><font size="4" color="#0000FF" class="">phausler@apple.com</font></u></tt></a><tt class=""><font size="4" class=""> wrote on 01/13/2016 06:04:23 PM:<br class="">> <br class="">> Tony brought up an important point about the prep for integration <br class="">> this morning: the blocks runtime from libblocksruntime-dev will be <br class="">> incompatible with the layout of blocks referenced from swift since <br class="">> the object size there is 2 uint32’s bigger to handle the RR (we <br class="">> don’t have objc). Without modifying clang and the runtime we won’t <br class="">> have a way to properly handoff blocks back and forth w/o objc.</font></tt><font size="4" class=""><br class=""></font><tt class=""><font size="4" class=""><br class="">Hi,</font></tt><font size="4" class=""><br class=""></font><tt class=""><font size="4" class=""><br class="">I'm guessing RR expands to Retain Release?</font></tt><font size="4" class=""><br class=""></font><tt class=""><font size="4" class=""><br class="">I've started exploring the source in swift-corelibs-foundation/closure. Any chance there is a design doc laying out the object model used by the Swift implementation? 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" class=""><br class=""></font><tt class=""><font size="4" class=""><br class="">thanks,</font></tt><font size="4" class=""><br class=""></font><tt class=""><font size="4" class=""><br class="">--dave</font></tt><font size="4" class=""><br class=""></font><tt class=""><font size="4" class=""><br class="">> <br class="">> I am currently taking a look at this to see what we can do to add an<br class="">> option to the clang code-gen to properly emit this structural <br class="">> difference. This isn’t a big issue for Foundation to CF since we <br class="">> don’t have many block APIs but dispatch is mostly blocks and that <br class="">> might pose an issue.<br class="">> </font></tt><font size="4" class=""><br class=""></font></p></ul></ul><font size="4" class=""><br class=""></font><br class=""><font size="4" class="">_______________________________________________<br class="">swift-corelibs-dev mailing list</font><u class=""><font size="4" color="#0000FF" class=""><br class=""></font></u><a href="mailto:swift-corelibs-dev@swift.org" class=""><u class=""><font size="4" color="#0000FF" class="">swift-corelibs-dev@swift.org</font></u></a><font size="4" class=""><br class=""></font><font size="4" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-corelibs-dev" class="">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev</a></font></ul></ul><br class=""><br class=""><br class="">
</div>
</div></blockquote></div><br class=""></body></html>