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
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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








