Permalink
Browse files

Add context which owns an asynchronous worker thread

  • Loading branch information...
Hunter Morris
Hunter Morris committed Mar 12, 2012
1 parent 6f657b2 commit 5751feadd4ada77e5798213c902a8f9980384f5f
Showing with 39 additions and 3 deletions.
  1. +20 −1 c_src/bcrypt_nif.c
  2. +5 −1 src/bcrypt.erl
  3. +9 −1 src/bcrypt_nif.erl
  4. +5 −0 src/bcrypt_nif_worker.erl
View
@@ -160,10 +160,29 @@ static ERL_NIF_TERM bcrypt_hashpw(ErlNifEnv* env, int argc, const ERL_NIF_TERM a
return enif_make_string(env, ret, ERL_NIF_LATIN1);
}
static ERL_NIF_TERM bcrypt_create_ctx(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ERL_NIF_TERM ret;
bcrypt_privdata_t *priv = (bcrypt_privdata_t*)enif_priv_data(env);
ctx_t* ctx = (ctx_t*)enif_alloc_resource(priv->bcrypt_rt, sizeof(ctx_t));
if (ctx == NULL)
return enif_make_badarg(env);
ctx->queue = async_queue_create();
ctx->topts = enif_thread_opts_create("bcrypt_thread_opts");
if (enif_thread_create("bcrypt_worker", &ctx->tid, async_worker, ctx, ctx->topts) != 0) {
enif_release_resource(ctx);
return enif_make_badarg(env);
}
ret = enif_make_resource(env, ctx);
enif_release_resource(ctx);
return ret;
}
static ErlNifFunc bcrypt_nif_funcs[] =
{
{"encode_salt", 2, bcrypt_encode_salt},
{"hashpw", 2, bcrypt_hashpw}
{"hashpw", 2, bcrypt_hashpw},
{"create_ctx", 0, bcrypt_create_ctx},
};
static void bcrypt_rt_dtor(ErlNifEnv* env, void* obj)
View
@@ -6,7 +6,7 @@
%% API
-export([start/0, stop/0]).
-export([mechanism/0]).
-export([gen_salt/0, gen_salt/1, hashpw/2]).
-export([gen_salt/0, gen_salt/1, hashpw/2, create_ctx/0]).
start() -> application:start(bcrypt).
stop() -> application:stop(bcrypt).
@@ -18,6 +18,7 @@ mechanism() ->
gen_salt() -> do_gen_salt(mechanism()).
gen_salt(Rounds) -> do_gen_salt(mechanism(), Rounds).
hashpw(Password, Salt) -> do_hashpw(mechanism(), Password, Salt).
create_ctx() -> do_create_ctx(mechanism()).
do_gen_salt(nif) -> bcrypt_nif_worker:gen_salt();
do_gen_salt(port) -> bcrypt_pool:gen_salt().
@@ -27,3 +28,6 @@ do_gen_salt(port, Rounds) -> bcrypt_pool:gen_salt(Rounds).
do_hashpw(nif, Password, Salt) -> bcrypt_nif_worker:hashpw(Password, Salt);
do_hashpw(port, Password, Salt) -> bcrypt_pool:hashpw(Password, Salt).
do_create_ctx(nif) -> bcrypt_nif_worker:create_ctx();
do_create_ctx(port) -> ok.
View
@@ -22,7 +22,7 @@
%% API
-export([init/0]).
-export([gen_salt/1, hashpw/2]).
-export([gen_salt/1, hashpw/2, create_ctx/0]).
-on_load(init/0).
@@ -60,6 +60,14 @@ gen_salt(LogRounds)
encode_salt(_R, _LogRounds) ->
nif_stub_error(?LINE).
%%--------------------------------------------------------------------
%% @doc Create a context which hashes passwords in a separate thread.
%% @spec create_ctx() -> term()
%% @end
%%--------------------------------------------------------------------
create_ctx() ->
nif_stub_error(?LINE).
%%--------------------------------------------------------------------
%% @doc Hash the specified password and the salt using the OpenBSD
%% Blowfish password hashing algorithm. Returns the hashed password.
@@ -8,6 +8,7 @@
-export([start_link/0]).
-export([gen_salt/0, gen_salt/1]).
-export([hashpw/2]).
-export([create_ctx/0]).
%% gen_server
-export([init/1, code_change/3, terminate/2,
@@ -22,6 +23,8 @@ gen_salt(Rounds) ->
gen_server:call(?MODULE, {gen_salt, Rounds}, infinity).
hashpw(Password, Salt) ->
gen_server:call(?MODULE, {hashpw, Password, Salt}, infinity).
create_ctx() ->
gen_server:call(?MODULE, create_ctx, infinity).
init([]) ->
{ok, Default} = application:get_env(bcrypt, default_log_rounds),
@@ -35,6 +38,8 @@ handle_call({gen_salt, R}, _From, State) ->
{reply, {ok, bcrypt_nif:gen_salt(R)}, State};
handle_call({hashpw, Password, Salt}, _From, State) ->
{reply, {ok, bcrypt_nif:hashpw(Password, Salt)}, State};
handle_call(create_ctx, _From, State) ->
{reply, {ok, bcrypt_nif:create_ctx()}, State};
handle_call(Msg, _, _) -> exit({unknown_call, Msg}).
handle_cast(Msg, _) -> exit({unknown_cast, Msg}).
handle_info(Msg, _) -> exit({unknown_info, Msg}).

0 comments on commit 5751fea

Please sign in to comment.