Create haxe functions for threads in hxcpp

332 views
Skip to first unread message

Marat Bektimirov

unread,
Nov 13, 2013, 10:21:02 AM11/13/13
to haxe...@googlegroups.com
What is the proper way to create functions with hxcpp? I see there is the Dynamic class for function:

Dynamic __hxcpp_thread_create(Dynamic inStart);

, but how to use it? I need it for create a haxe Thread in hxcpp runtime to wrap my existing multithreading code.

Thanks for any help.

Sam MacPherson

unread,
Nov 13, 2013, 3:31:50 PM11/13/13
to haxe...@googlegroups.com
cpp.vm.Thread.create(function () {
// This is running in a new, separate thread
});

Is this what you are looking for?

Marat Bektimirov

unread,
Nov 13, 2013, 3:34:45 PM11/13/13
to haxe...@googlegroups.com
No, I mean doing this with C++ and hxcpp

Daniel Uranga

unread,
Nov 13, 2013, 4:19:11 PM11/13/13
to haxe...@googlegroups.com
I think you shouldn't be doing anything that complex in your "glue" code. With "glue" code I mean:
(your C++) <-> ("glue" code (using hxcpp c++)) <-> (Haxe)

I suggest you to put your threading code in either the C++ side or the Haxe side, not in-between.

Marat Bektimirov

unread,
Nov 13, 2013, 4:29:36 PM11/13/13
to haxe...@googlegroups.com
All is perfect without glue until you need to make some calls to Haxe from a thread through external callback. The program crashes immediately with this error (Android) :

"GetLocalAllocMT - requesting memory from unregistered thread!"

See this post with the same problem https://groups.google.com/forum/m/#!topic/haxelang/_l2LP0lY8JM

Hugh

unread,
Nov 14, 2013, 12:22:07 AM11/14/13
to haxe...@googlegroups.com
Hi,
You need to call:

void hx::RegisterCurrentThread(void *inTopOfStack)
void hx::UnregisterCurrentThread()

It should be possible to call these "around" the code that performs allocations, although this would be expensive to register/unregister all the time.
Otherwise you will need to call __hxcpp_enter_gc_free_zone/__hxcpp_exit_gc_free_zone when you are no longer "under haxe control".
This behaviour  is "undocumented", that is, so far I have left myself the option of completely reworking the implementation.
There is a cost to making this an official API (since then it would have to be supported always) so I have delayed 

If the native producer/haxe consumer thread model does not work for you, perhaps you could some indication of the use case, which would help towards an official API.

Hugh

Marat Bektimirov

unread,
Nov 14, 2013, 4:07:43 AM11/14/13
to haxe...@googlegroups.com
Thanks for the help, Hugh! 
But now I have a linker problem: "undefined reference to `hx::RegisterCurrentThread(void*)". Is GcInternal.cpp linked well when I do a custom build? 

четверг, 14 ноября 2013 г., 11:22:07 UTC+6 пользователь Hugh написал:

Hugh

unread,
Nov 15, 2013, 1:05:34 AM11/15/13
to haxe...@googlegroups.com
Hi,
Are you building a ndll?  Looking over the code again, you should be able to do this with the CFFI.h functions:

// Called from thread....
int top = 0;
gc_set_top_of_stack(&top, true);
gc_exit_blocking();

.. haxe calls here ...

gc_enter_blocking()

// back to native work..

The " gc_set_top_of_stack" will register the thread for you, however it will currently leak the thread context, since there is no good way to unregister the thread.
The last parameter to "gc_set_top_of_stack" says that there are no haxe values on the stack at this point in time.  So use true if you will not nest these calls, or false to be safe.

This is not really tested, but let me know how you go, and I will look to improve the API.

Marat Bektimirov

unread,
Nov 15, 2013, 2:33:09 AM11/15/13
to haxe...@googlegroups.com
Great, it works now! Yes, I am building a ndll lib and before I've tried to call hx::RegisterCurrentThread directly with no luck. I am very grateful to you for your help. Hugh! It would be great if native multithreading api become more flexible and customizable in the future.

Thanks!
Reply all
Reply to author
Forward
0 new messages