Permalink
Browse files

Basic dirty scheduler usage

  • Loading branch information...
1 parent c474fa2 commit 7aed7fcf3798bc22217c11bed24e5cbebac3775e @davisp committed Feb 2, 2014
@@ -0,0 +1,83 @@
+#include "erl_nif.h"
+
+#ifdef ERL_NIF_DIRTY_SCHEDULER_SUPPORT
+
+static ERL_NIF_TERM
+double_run(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ ERL_NIF_TERM r;
+ int v;
+ enif_get_int(env, argv[0], &v);
+ r = enif_make_int(env, v * 2);
+ return enif_schedule_dirty_nif_finalizer(env, r, enif_dirty_nif_finalizer);
+}
+
+
+static ERL_NIF_TERM
+double_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int flags = ERL_NIF_DIRTY_JOB_CPU_BOUND;
+ return enif_schedule_dirty_nif(env, flags, double_run, argc, argv);
+}
+
+
+static ERL_NIF_TERM
+triple_run(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ ERL_NIF_TERM r;
+ int v;
+
+ enif_get_int(env, argv[0], &v);
+ r = enif_make_int(env, v * 3);
+
+ if(enif_is_on_dirty_scheduler(env)) {
+ return enif_schedule_dirty_nif_finalizer(
+ env,
+ r,
+ enif_dirty_nif_finalizer
+ );
+ } else {
+ return r;
+ }
+}
+
+
+static ERL_NIF_TERM
+triple_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ int flags = ERL_NIF_DIRTY_JOB_CPU_BOUND;
+ return enif_schedule_dirty_nif(env, flags, triple_run, argc, argv);
+}
+
+
+static ErlNifFunc nif_funcs[] = {
+ {"double", 1, double_nif},
+ {"triple_defer", 1, triple_nif},
+ {"triple_direct", 1, triple_run}
+};
+
+
+#else // No dirty schedulers
+
+
+static ERL_NIF_TERM
+no_dirty(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ return enif_make_tuple2(
+ enif_make_atom(env, "error"),
+ enif_make_atom(env, "no_dirty_schedulers")
+ );
+}
+
+
+static ErlNifFunc nif_funcs[] = {
+ {"double", 1, no_dirty},
+ {"triple_defer", 1, no_dirty},
+ {"triple_direct", 1, no_dirty}
+};
+
+
+#endif
+
+
+ERL_NIF_INIT(dirty_sched, nif_funcs, NULL, NULL, NULL, NULL);
@@ -0,0 +1,3 @@
+{port_specs, [
+ {"priv/dirty_sched.so", ["c_src/*.c"]}
+]}.
@@ -0,0 +1,7 @@
+{application, dirty_sched, [
+ {description, "Examples using dirty schedulers"},
+ {vsn, "0.0.0"},
+ {registered, []},
+ {applications, [kernel, stdlib]},
+ {env, []}
+]}.
@@ -0,0 +1,39 @@
+-module(dirty_sched).
+-on_load(init/0).
+
+-export([
+ double/1,
+ triple_defer/1,
+ triple_direct/1
+]).
+
+
+double(_N) ->
+ not_loaded(?LINE).
+
+
+triple_defer(_N) ->
+ not_loaded(?LINE).
+
+
+triple_direct(_N) ->
+ not_loaded(?LINE).
+
+
+init() ->
+ SoName = case code:priv_dir(?MODULE) of
+ {error, bad_name} ->
+ case filelib:is_dir(filename:join(["..", priv])) of
+ true ->
+ filename:join(["..", priv, ?MODULE]);
+ _ ->
+ filename:join([priv, ?MODULE])
+ end;
+ Dir ->
+ filename:join(Dir, ?MODULE)
+ end,
+ erlang:load_nif(SoName, 0).
+
+
+not_loaded(Line) ->
+ exit({not_loaded, [{module, ?MODULE}, {line, Line}]}).
@@ -0,0 +1,15 @@
+-module(dirty_sched_tests).
+
+-include_lib("eunit/include/eunit.hrl").
+
+
+double_test() ->
+ ?assertEqual(2, dirty_sched:double(1)).
+
+
+triple_defer_test() ->
+ ?assertEqual(3, dirty_sched:triple_defer(1)).
+
+
+triple_direct_test() ->
+ ?assertEqual(3, dirty_sched:triple_direct(1)).
View
@@ -1,5 +1,5 @@
{sub_dirs, [
- "apps/etap",
+ "apps/dirty_sched",
"apps/resources",
"apps/skeleton",
"apps/termsend",

0 comments on commit 7aed7fc

Please sign in to comment.