Giter Site home page Giter Site logo

Comments (6)

jorendorff avatar jorendorff commented on April 27, 2024 4

Options:

  1. What the current send branch does. This is sound, and it supports both enif_send use cases ("calling process" and "created thread"), but it incurs measurable overhead on every NIF call.
  2. We can add a bool to NifEnv telling whether or not it's the "environment of the calling process". The bit would be true for envs created by handle_nif_call and false for envs created by OwnedEnv::run etc. This would impose some overhead, not on every NIF call, but on everything you do with a NifEnv (no guess whether this is better or worse than option 1). Plus this is a half solution. It works only for "calling process" cases, i.e. when the bit is set. If it's not set, we might be either in a "calling process" or a "created thread" situation, and we have no way of knowing which.
  3. We can create a different thread-local variable, a bool that is set true by ThreadSpawner::spawn() to mark "created" threads. This too is a half solution, but the other half: if we're in a calling process, we know it's safe to call enif_send(null, ...). If not, we don't know if env is the correct environment to pass to enif_send or not.
  4. We could do both option 2 and option 3.
  5. We can mark NifEnv::send() and trait JobSpawner as unsafe.
  6. We can add some kind of flag that you have to set in your rustler_export_nifs if you want to use env.send(), so you only pay the overhead if you're actually planning to send messages. If you don't set the flag, the thread-local variable will be None and .send(), seeing this, can just panic.

from rustler.

scrogson avatar scrogson commented on April 27, 2024 2

Looks like another option would be to use enif_thread_type

from rustler.

goertzenator avatar goertzenator commented on April 27, 2024

Thanks for posing this in the erlang-questions lists. enif_send is the only safety hole that I haven't solved yet. If you learn anything new, please do record it here for posterity.

Maybe once the dust has settled on OTP-20 (and they are juggling quite a bit in the NIF code) we can see if there are any changes/additions on the Erlang side that will allow safe enif_send.

from rustler.

hansihe avatar hansihe commented on April 27, 2024

An alternative to option 6 would be to use cargo conditional compilation. If the user wants to use send, they add the send feature to the Cargo.toml, and the code for the feature only gets compiled in if the user explicitly enables the feature. This would completely remove all runtime cost when the feature is disabled, and possibly make the implementation simpler.

from rustler.

jorendorff avatar jorendorff commented on April 27, 2024

Right -- I forgot about that. I'll do that.

from rustler.

goertzenator avatar goertzenator commented on April 27, 2024

@scrogson Nice find! enif_thread_type first appeared in OTP-19 and is provided by erlang_nif-sys = ">=0.5.3".

from rustler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.