amnu3387

amnu3387

Creating long running NIFs (or something else)

Hi,

I’m not sure if a NIF is the best option for this - I’m also not experienced with C (don’t even know how to properly link libs, for instance) at all but I’m taking small steps in order to use OpenGL.

Imagine you are building a graphical interface that needs to do OpenGL. I can write a small program in C that just opens a window and stays there. All good.

My question now is, if I want to interact with this program, like send it draw commands so it applies them to the gl context, what would be the best way to do that? Although initially a toy project I would definitely prefer the option that is the most performant, as this would be something akin to a browser.

Do you know any good resources/libs for looking into this? I’m going for the lowest common denominator possible - elixir/erl ↔ c program using glfw3 - the C program also has to be able to callback somehow, since glfw handles user interaction on the window as well.

Any pointers welcomed, thanks

Most Liked

elcritch

elcritch

Take a look at Nim and Nimler if C and linking seems too intimidating. Nim is much easier to learn than Rust but still provides memory safety by default. It’s become my default for NIF’s since it’s new GC plays well with BEAM. Glfw might require some fiddling with linking but the forums are helpful.

Edit: actually the nimler docs is probably better than its github: Getting started - nimler

Plus, Nim has been used in a few moderate sized games. There’s decent OpenGL libraries like NimGL or nim glfw. Or a cool looking 2d library, pixie.

Happy hacking!

P.S. If you use long running NIF’s they’ll need to be marked as “dirty nifs”. Also I wouldn’t worry upfront about killing beam, it’s just that we get used to the beam rarely ever dying. It sounds like you’re interested in local ui stuff anyway.

ityonemo

ityonemo

yielding currently works as of 0.7.2, . the api changes are between 0.7.0 and 0.7.1, and the updated niceties coming in 0.8.0 are separate things. I don’t expect the yielding API to change moving forward, except to support interrupting threaded nifs too. If you’re curious as to how it works:

https://github.com/ityonemo/zigler/blob/master/lib/zig/nif/yielding.ex

The entrypoint functions start at line 105, the async function is line 159

Ability to seamlessly swap between yielding, sync, and threaded depends on the fact that zig has colorless async functions.

beam.yield function is here: zigler/priv/beam/beam.zig at main · E-xyza/zigler · GitHub

tl;dr - launcher function sets up a frame (heap-allocated memory space for an async function + its stack) inside of a BEAM resource, then it launches the function, inside of an event loop, watching for the 1 ms time limit to elapse. If you get close to exhausting the event loop’s 1ms requirement, intead of resuming the async function, it calls the BEAM nif ABI “reschedule a tail-call” function, passing the resource along as an artifact. On reentry, it accesses the resource and obtains the frame pointer and is able to resume the async function. If you kill the parent process, it triggers garbage collection of the resource, which is set up with a hook to resume at the beam.yield point, except signalling an error, which triggers any defer statements in the zig as usual.

Also importantly, strategic use of threadlocal variables are necessary to allow for ‘global’ information exchange.

dimitarvp

dimitarvp

Fair. As a preliminary spoiler alert: in C it’s trivial to allocate a memory buffer of 10 bytes and then try to get a value from the 11th element. This, oversimplified, has been the reason for most of the security fiascos in the last several years.

Rust prevents those.

Where Next?

Popular in Questions Top

New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement