couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chewbra...@apache.org
Subject couch commit: updated refs/heads/2657-fix-cassim-fabric-calls-revised to f056d83
Date Thu, 21 May 2015 21:12:03 GMT
Repository: couchdb-couch
Updated Branches:
  refs/heads/2657-fix-cassim-fabric-calls-revised 67584b931 -> f056d837c (forced update)


add couch_util:with_proc/4

BugzId: 2657


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/f056d837
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/f056d837
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/f056d837

Branch: refs/heads/2657-fix-cassim-fabric-calls-revised
Commit: f056d837cc7436e02b02e37955e2c86827b00b1d
Parents: 42c9047
Author: Russell Branca <chewbranca@apache.org>
Authored: Mon Apr 20 19:57:20 2015 +0000
Committer: Russell Branca <chewbranca@apache.org>
Committed: Thu May 21 21:11:34 2015 +0000

----------------------------------------------------------------------
 src/couch_util.erl | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/f056d837/src/couch_util.erl
----------------------------------------------------------------------
diff --git a/src/couch_util.erl b/src/couch_util.erl
index 7e16866..742ab70 100644
--- a/src/couch_util.erl
+++ b/src/couch_util.erl
@@ -33,6 +33,7 @@
 -export([integer_to_boolean/1, boolean_to_integer/1]).
 -export([find_in_binary/2]).
 -export([callback_exists/3, validate_callback_exists/3]).
+-export([with_proc/4]).
 
 -include_lib("couch/include/couch_db.hrl").
 
@@ -569,3 +570,20 @@ ensure_loaded(Module) when is_atom(Module) ->
         false
     end;
 ensure_loaded(_Module) -> false.
+
+
+%% This is especially useful in gen_servers when you need to call
+%% a function that does a receive as it would hijack incoming messages.
+with_proc(M, F, A, Timeout) ->
+    {Pid, Ref} = spawn_monitor(fun() ->
+        exit({reply, erlang:apply(M, F, A)})
+    end),
+    receive
+        {'DOWN', Ref, process, Pid, {reply, Resp}} ->
+            {ok, Resp};
+        {'DOWN', Ref, process, Pid, Error} ->
+            {error, Error}
+    after Timeout ->
+        erlang:demonitor(Ref, [flush]),
+        {error, timeout}
+    end.


Mime
View raw message