On Saturday, 28 December 2013 at 22:21:46 UTC, Joakim wrote:
I finished porting most of druntime to Android/x86 and have started trying to run the tests: I just got 31 out of 38 druntime modules' unit tests to pass. :)

I figured out the segfault mentioned above was because TLS is done differently on Android- flags[] is a global when outside main and thread-local by default as a result, adding a shared attribute makes it work again- and I was getting a lot of segfaults on the unit tests for the same reason. After making all the simple globals in the unit tests, like cpuid in rt.arrayfloat/int/etc., into shared variables, only 8 modules' unittests segfault, one after passing.

Bionic doesn't support __thread for TLS, but it does let you use the pthread_key_(create|delete)/pthread_(set|get)specific APIs, just as druntime does on OS X. Here's the bionic implementation:

https://github.com/android/platform_bionic/blob/master/libc/bionic/pthread_key.cpp

I haven't completely grasped TLS and how it's done on OS X yet, but I get the sense I'll have to modify dmd a little to get TLS working with bionic. It appears that Martin and Johannes have looked into this before, so if you two have any feedback, let me know.

I'm hoping TLS is the last remaining piece for druntime to be finished porting to Android/x86, then I want to try Phobos, which I'm guessing shouldn't be as bad.
Been awhile since I updated on the Android effort: I'm now able to get all 38 druntime modules' unit tests to pass on Android/x86... under somewhat random conditions. It's finicky and some of the tests start failing and many segfaulting on exit, as mentioned before, if I make minor changes to the unit tests, like changing some TLS globals to shared. I'm guessing this is because I don't really have TLS working yet, I'm just taking advantage of the fact that the baked-in TLS in linux kinda sorta still works on Android.

As long as it's local-exec, uninitialized TLS mostly works if it's less than 4-5 KB, while initialized TLS doesn't get initialized correctly. Of course, it's possible that I'm hitting some other codegen incompatibility altogether, unrelated to TLS, but the fact that it sometimes works depending on the number of TLS variables makes me think it's a TLS issue. I also got a bit more than half of the Phobos unit tests to pass, with a lot of seg faults there too.

Android is going to need a packed TLS approach, similar to what Walter implemented for OS X with dmd:

http://www.drdobbs.com/architecture-and-design/implementing-thread-local-storage-on-os/228701185

I've been looking into doing something similar for ELF, but this is the first time hacking on a compiler for me. If someone else more familiar with dmd or ldc could put packed TLS for ELF together more quickly, that would certainly speed things up. If not, I'll have something together eventually. ;)

Reply via email to