<div dir="ltr"><div>Hi Max,</div><div>I had a similar problem when interfacing towards the ALSA library for sound generation on Linux, but it is probably much more simple than your case so I am not sure it will help. <br></div><div>For every thread I have a peer gen_server that opens a handle in a resource and starts the callback thread (I get events every 5 ms) in init/1 and handle_continue/2 function . When the gen_server is shutdown and terminate/2 is called I call the a nif to close the handle. The resource itself is garbage collected by erlang (I think :-) ).</div><div>What I noticed was that I had to set the process_flag(trap_exit, true) in order to have the terminate function called at supervisor:terminate_child/2 .</div><div>I am not sure what you are meaning with delayed destructor but if you need a lot of time for complete destruction maybe you can orchestrate it from a separately spawned erlang process that calls the proper nifs.<br></div><div><br></div><div>You can checkout my code at <a href="https://github.com/karlsson/xalsa">https://github.com/karlsson/xalsa</a> , mainly c_src/xalsa_nif.c and src/xalsa_server.erl</div><div><br></div><div>Best Regards</div><div>Mikael<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Den ons 17 juli 2019 kl 17:53 skrev Max Lapshin <<a href="mailto:max.lapshin@gmail.com">max.lapshin@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi.<div><br></div><div>We are writing NIF wrapper for OpenMAX — standard API for transcoding video on Android or Raspberry Pi.</div><div><br></div><div>This API requires to launch separate thread (launched by library) and this thread receives some events from hardware. This thread wraps hardware events in erlang messages and sends them to owner process. All this looks really cool and we have excellently working happy path of code.</div><div><br></div><div>When whole resource is to be destroyed, we need to clean it properly, shutdown all opened hardware handlers. Right now we have to write complicated code on C in destructor that calls cond_wait, mutex_lock, etc.</div><div><br></div><div>The same code in erlang is 100 times cleaner and easier, however in destructor we cannot write in erlang. I cannot exit from destructor function and ask to continue destructing when separate thread will be activated by hardware.</div><div><br></div><div>So it would be cool to continue having erlang vm even in destructor or make something like "delayed destructor" with "I promise to finish this destruction".</div><div><br></div><div>Question is: is my problem well known or it is very specific?</div><div><br></div></div>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div>