Permalink
Browse files

Added abort

  • Loading branch information...
1 parent e3c3b04 commit 46f30f1999407cfcae8b94f9cdd21ba2732ef7f6 @ian-plosker committed Feb 15, 2012
Showing with 32 additions and 1 deletion.
  1. +6 −1 c_src/stm_erl.c
  2. +26 −0 src/stm.erl
View
@@ -46,14 +46,18 @@ ERL_NIF_TERM stm_erl_commit(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
return enif_make_atom(env, aborted == 0 ? "ok" : "error");
}
+ERL_NIF_TERM stm_erl_abort(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
+ stm_abort(STM_ABORT_EXPLICIT);
+ return enif_make_atom(env, "ok");
+}
+
ERL_NIF_TERM stm_erl_new_var(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
stm_erl_var* var = (stm_erl_var*)enif_alloc_resource(stm_erl_RESOURCE,
sizeof(stm_erl_var));
long integer;
ErlNifBinary binary;
if (enif_get_int64(env, argv[0], &integer)) {
-
var->field = enif_alloc(sizeof(long));
var->type = INTEGER;
var->size = NULL;
@@ -141,6 +145,7 @@ static ErlNifFunc nif_funcs[] =
{"close", 0, stm_erl_close},
{"trans_start", 0, stm_erl_trans_start},
{"commit", 0, stm_erl_commit},
+ {"abort", 0, stm_erl_abort},
{"new_var", 1, stm_erl_new_var},
{"store_var", 2, stm_erl_store_var},
{"load_var", 1, stm_erl_load_var}
View
@@ -71,6 +71,13 @@ commit() ->
_ -> exit("NIF library not loaded")
end.
+-spec abort() -> ok.
+abort() ->
+ case random:uniform(999999999999) of
+ 666 -> ok;
+ _ -> exit("NIF library not loaded")
+ end.
+
-spec new_var(integer()) -> {ok, reference()}.
new_var(_Val) ->
case random:uniform(999999999999) of
@@ -132,6 +139,25 @@ binary_test() ->
Val2 = ?atomic(load_var(Var)),
?assertEqual(<<"abcdef">>, Val2).
+abort_test() ->
+ initialize(),
+ {ok, Var} = new_var(<<"abc">>),
+
+ Val1 = ?atomic(load_var(Var)),
+
+ ?assertEqual(<<"abc">>, Val1),
+
+ ?atomic(
+ begin
+ Val1 = load_var(Var),
+ store_var(<<Val1/binary, "def">>, Var),
+ abort()
+ end),
+
+ Val2 = ?atomic(load_var(Var)),
+ ?assertEqual(Val1, Val2),
+ ?assertNotEqual(<<"abcdef">>, Val2).
+
sync_test() ->
initialize(),

0 comments on commit 46f30f1

Please sign in to comment.