Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive

Port osx_tls.c to D#1582

Closed
jacob-carlborg wants to merge 2 commits intodlang:masterfrom
jacob-carlborg:osx_tls_d
Closed

Port osx_tls.c to D#1582
jacob-carlborg wants to merge 2 commits intodlang:masterfrom
jacob-carlborg:osx_tls_d

Conversation

@jacob-carlborg
Copy link
Contributor

No description provided.

@Geod24
Copy link
Member

Geod24 commented Jun 2, 2016

Since it's OSX-specific, should it be in stdc ?

}
});

dyld_enumerate_tlv_storage(&handler);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handler doesn't escape, does it ?
In which case the delegate literal at line 156 should be made a scope delegate to avoid needless memory allocation.
What is blocks semantic in that regard ? Is it mostly / only used for callback or can it be returned as a true delegate as well ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handler doesn't escape, does it ?

I really hope it doesn't because block returns by value and handler is on the stack, which will be gone after getTLSRange completes. I can look at the source code to confirm, but I would be very surprised.

In which case the delegate literal at line 156 should be made a scope delegate to avoid needless memory allocation.

Is that possible to do on the call site?

What is blocks semantic in that regard ? Is it mostly / only used for callback or can it be returned as a true delegate as well ?

What do you mean with "true delegate"? A closure that captures local variables which can be used after a function returns? Yes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that possible to do on the call site?

Yes. Either you can use:

scope dg = (dyled_tlv_states state, dyld_tlv_info* info) { /*...  */ }
auto handler = block(dg);

You could change the definition of block to take a parameter of type scope R delegate(Params) dg which will mean delegate literals passed directly will never allocate. But see the next point.

What do you mean with "true delegate"? A closure that captures local variables which can be used after a function returns? Yes.

Quite a poor phrasing on my side, the question was actually "can the block escape the lexical scope in which it was declared", to which the answers seems to be yes (and so you can't use scope delegate for block's prototype).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scope dg = ...

Aha, I've never though of doing that for anything like but classes. I'll update that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Geod24 changed delegate to be scope.

@Geod24
Copy link
Member

Geod24 commented Jun 2, 2016

Out of curiosity, what's the motivation behind the porting ?

@jacob-carlborg
Copy link
Contributor Author

Out of curiosity, what's the motivation behind the porting ?

  • Dogfooding
  • I got some complains first time this was added and was not written in D
  • Why is the rest of druntime, DMD (half of it), or anything wirtten in D? 😃
  • Because I can 😃

@jacob-carlborg
Copy link
Contributor Author

Since it's OSX-specific, should it be in stdc ?

I don't know, suggestions are welcome. I had to put it somewhere. I quick search on google suggest that it works on Linux as well with the correct runtime libraries installed. I can do a test and confirm.

@jacob-carlborg
Copy link
Contributor Author

jacob-carlborg commented Jun 2, 2016

I just did a test in a Debian container, following these instructions [1], and it works perfectly fine.

[1] http://stackoverflow.com/a/11097610

@jacob-carlborg
Copy link
Contributor Author

jacob-carlborg commented Jun 2, 2016

I'll add some code to make it possible to call blocks from the D side as well.

@jacob-carlborg
Copy link
Contributor Author

Suggestions for the naming convention would be appreciated as well. I think the official name is "Blocks" but I called it "Clang Blocks" for now. To avoid confusion with a regular block. Perhaps "Block" with a capital letter is enough to avoid any confusion.

@jacob-carlborg
Copy link
Contributor Author

@MartinNowak osx_tls.c is now ported to D.

@jacob-carlborg jacob-carlborg force-pushed the osx_tls_d branch 3 times, most recently from d7db859 to 9551eba Compare June 5, 2016 19:02
@jacob-carlborg
Copy link
Contributor Author

I'll add some code to make it possible to call blocks from the D side as well.

I'll skip this because the Block struct need to look different depending on if any local variables are captured on the C side. This seems a bit too complicated.

@jacob-carlborg
Copy link
Contributor Author

Ok, all tests are passing.

#ifndef __BLOCKS__
#error "Need a C compiler with Apple Blocks support – not building on OS X with Clang?"
#endif
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an equivalent test needed in the D version? That is, what happens if you are building on something other than OSX, or linking with non-clang libc?

I totally admit, I'm not sure what should be done, I'm just pointing out a difference between the C code and the D code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The content of this file is moved into sections_osx_x86_64.d, which is already properly guarded for OS X 64bit.

The code which makes calling Blocks possible, clang_block.d, is cross-platform. Using that code only make sense if you have a C function taking Block you want to call. That C code needs to be compiled with Clang, and linking that on non-Apple platforms requires the Blocks runtime, which is noted in the documentation. We can't do anything, as far as I know, on the D side to enforce this.

If you try to compile the C code with something else than Clang you'll get a syntax error. Linking without the Blocks runtime will cause undefined symbol errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far the tests for clang_block.d is only enabled on OS X because it has everything required out of the box. If we install Clang and the Blocks runtime on the other machines used by the autotester I can enable it for those other platforms as well.

@Geod24
Copy link
Member

Geod24 commented Jun 6, 2016

I don't know, suggestions are welcome. I had to put it somewhere. I quick search on google suggest that it works on Linux as well with the correct runtime libraries installed. I can do a test and confirm.

I would definitely not put it in stdc, as its for C standard library.

Either we can have a core.clang package, but that seems like a dangerous precedent, or as part of core.sys.osx[.clang]. Last but not least, it could be in an internal module, like rt.
I am leaning towards the later option.

* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Authors: Jacob Carlborg
* Source: $(DRUNTIMESRC core/_clang_block.d)
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kudos for documentation !

@jacob-carlborg
Copy link
Contributor Author

Either we can have a core.clang package, but that seems like a dangerous precedent, or as part of core.sys.osx[.clang]. Last but not least, it could be in an internal module, like rt.
I am leaning towards the later option.

It works on other platforms than OS X (that's why I put it in core.stdc) but I guessing it's not going to be used on any non-Apple platform. I did not want to put it in the rt package because it's useful outside of the runtime.

@Geod24
Copy link
Member

Geod24 commented Jun 7, 2016

It works on other platforms than OS X (that's why I put it in core.stdc) but I guessing it's not going to be used on any non-Apple platform. I did not want to put it in the rt package because it's useful outside of the runtime.

I think we should separate what is used for implementing the runtime and what druntime expose.
There is a great deal of overlap thanks to libc, but I don't think we should expose this one.

@jacob-carlborg
Copy link
Contributor Author

I think we should separate what is used for implementing the runtime and what druntime expose.
There is a great deal of overlap thanks to libc

There's a specific reason why I chose to expose the Blocks related code. That's because it's very useful to be able to interface with functions taking Blocks on OS X.

but I don't think we should expose this one.

I don't see a reason why not.

Clang Blocks, or just Blocks [1], is an extension to the C family of
languages which adds support for anonymous functions, much like
delegates in D.

Being able to interface with Blocks is important when interfacing
with Apple specific API's since some of the API's are only available
in a form requiring using Blocks.

Interfacing with Blocks is done by directly using the underlying ABI.
A Block basically consist of a struct containing a function pointer to
the body of the Block, any captured values and some metadata.
The ABI that is used is the Apple ABI [2].

It's possible to use Blocks on non-Apple platforms as well. This
requires to compile the C code using Clang and install and link with
the Blocks runtime [3].

[1] Language specification for Blocks:
http://clang.llvm.org/docs/BlockLanguageSpec.html

[2] http://clang.llvm.org/docs/Block-ABI-Apple.html
[3] http://compiler-rt.llvm.org
@Geod24
Copy link
Member

Geod24 commented Jun 11, 2016

There's a specific reason why I chose to expose the Blocks related code. That's because it's very useful to be able to interface with functions taking Blocks on OS X.
[...]
I don't see a reason why not.

Okay, let's wait for some other opinion on this topic then.

@MartinNowak
Copy link
Member

Blocks aren't standard C, so they don't belong into core.stdc, use core.sys.osx.something.
If the ported D version doesn't rely on hacks (looks like http://clang.llvm.org/docs/Block-ABI-Apple.html), then replacing the C code is fine with me.

@dnadlinger
Copy link
Contributor

dnadlinger commented Jun 19, 2016

sys.osx isn't really correct either, though, since they are not OS X-specific (even though obviously developed by Apple). It's a Clang thing.

@jacob-carlborg
Copy link
Contributor Author

If the ported D version doesn't rely on hacks (looks like http://clang.llvm.org/docs/Block-ABI-Apple.html), then replacing the C code is fine with me.

Not sure what you mean with "hacks". But yes, it uses/follows http://clang.llvm.org/docs/Block-ABI-Apple.html

@jacob-carlborg
Copy link
Contributor Author

sys.osx isn't really correct either, though, since they are not OS X-specific (even though obviously developed by Apple). It's a Clang thing.

Exactly. If we install Clang and the Blocks Runtime on the autotester machines I could enable the tests on more platforms.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants