threading issues with D -> C -> Python

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 2 19:04:38 PST 2014


On Wed, 03 Dec 2014 02:52:27 +0000
Michael via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:

> Okay. Well I am already not passing any D-allocated data. I'm 
> specifically creating variables/arrays on the C-stack, and then 
> passing the pointer of that to D and overwriting the data of the 
> C-stack pointer for any return values. I was worried about that 
> specific problem and I thought this would be a solution. I am 
> then able to tell python to use the C-stack variable without 
> having to worry about D trying to run any garbage collection on 
> it.
if D code has any pointer to that data stored anywhere, GC will walk it
and hit another thread's stack. and now it doesn't know where it is,
and it can't pause that uknown thread so it will not mutate the area GC
is scanning now. this *may* work, but it will segfault sooner or later.

> Going the other way, I probably am passing some python strings 
> etc.. into D, but I would assume they are valid for the lifetime 
> of the function call, and that D would have no reason to try and 
> perform any garbage collection on them.
D has conservative GC, so it will try to walk with the unknown data
just in case that data contains some pointers. and GC can hit "false
positives" there (something that *looks* like a pointer to some area)
and other things.

so to make the long story short: you should either register and
deregister *all* your threads in GC (for D threads it's automatic
process; for other threads you must do it manually), or don't use GC at
all.

besides, if you are using your D library from C code, you must call
`rt_init()` once before calling any D code. this function will
initialize D runtime. and you have to call `rt_term()` before exiting
your program to deinitialize D runtime.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141203/7f0479b6/attachment.sig>


More information about the Digitalmars-d-learn mailing list