Skip to content

Commit

Permalink
Document the usage of schedule flag for nif macro (#444)
Browse files Browse the repository at this point in the history
Closes #111
  • Loading branch information
philss committed Apr 12, 2022
1 parent 3b053a1 commit 6f80a55
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rustler_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ pub fn init(input: TokenStream) -> TokenStream {
/// a + b
/// }
/// ```
///
/// For functions that may take some time to return - let's say more than 1 millisecond - it is
/// recommended to use the `schedule` flag. This tells the BEAM to allocate that NIF call
/// to a special scheduler. These special schedulers are called "dirty" schedulers.
///
/// We can have two types of "lengthy work" functions: those that are CPU intensive
/// and those that are IO intensive. They should be flagged with "DirtyCpu" and "DirtyIo",
/// respectively.
///
/// See: <https://www.erlang.org/doc/man/erl_nif.html#lengthy_work>
///
/// ```ignore
/// #[nif(schedule = "DirtyCpu")]
/// pub fn my_lengthy_work() -> i64 {
/// let duration = Duration::from_millis(100);
/// std::thread::sleep(duration);
///
/// 42
/// }
/// ```
#[proc_macro_attribute]
pub fn nif(args: TokenStream, input: TokenStream) -> TokenStream {
let args = syn::parse_macro_input!(args as syn::AttributeArgs);
Expand Down

0 comments on commit 6f80a55

Please sign in to comment.