From b0426732cc19598f0c0c310b1e79918252495259 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Mon, 17 Jan 2011 15:47:00 +0100 Subject: Add plugin support for alternative name lookup OTP behaviour instances (gen_server, gen_fsm, gen_event) can currently register themselves either locally or globally, and the behaviour libraries (including gen.erl) support both addressing methods, as well as the normal Pid and {Name, Node}. However, there are alternative registry implementations - e.g. gproc - and one can well imagine other ways of locating a behaviour instance, e.g. on a node connected only via a TCP tunnel, rather than via Distributed Erlang. In all these cases, one needs to write extra code to identify the behaviour instance, even though the instance itself need not be aware of how it is located. This patch introduces a new way of locating a behaviour instance: {via, Module, Name}. Module is expected to export a subset of the functions in global.erl, namely: register_name(Name, Pid) -> yes | no whereis_name(Name) -> pid() | undefined unregister_name(Name) -> ok send(Name, Msg) -> Pid Semantics are expected to be the same as for global.erl This can be used in all places where {global, Name} is accepted. faulty export in gen_fsm_SUITE.erl await process death in dummy_via:reset() fix error in gen_[server|fsm]:enter_loop() fix documentation --- lib/stdlib/doc/src/gen_event.xml | 61 ++++++++++++++++++++++++-------- lib/stdlib/doc/src/gen_fsm.xml | 55 ++++++++++++++++++---------- lib/stdlib/doc/src/gen_server.xml | 39 +++++++++++++------- lib/stdlib/doc/src/supervisor.xml | 12 +++++-- lib/stdlib/doc/src/supervisor_bridge.xml | 7 ++++ 5 files changed, 126 insertions(+), 48 deletions(-) (limited to 'lib/stdlib/doc/src') diff --git a/lib/stdlib/doc/src/gen_event.xml b/lib/stdlib/doc/src/gen_event.xml index 24bcb419fe..0f50b37de6 100644 --- a/lib/stdlib/doc/src/gen_event.xml +++ b/lib/stdlib/doc/src/gen_event.xml @@ -120,8 +120,10 @@ gen_event:stop -----> Module:terminate/2 start_link(EventMgrName) -> Result Create a generic event manager process in a supervision tree. - EventMgrName = {local,Name} | {global,Name} + EventMgrName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() +  GlobalName = ViaName = term() Result = {ok,Pid} | {error,{already_started,Pid}}  Pid = pid() @@ -132,10 +134,17 @@ gen_event:stop -----> Module:terminate/2 the event manager is linked to the supervisor.

If EventMgrName={local,Name}, the event manager is registered locally as Name using register/2. - If EventMgrName={global,Name}, the event manager is - registered globally as Name using + If EventMgrName={global,GlobalName}, the event manager is + registered globally as GlobalName using global:register_name/2. If no name is provided, - the event manager is not registered.

+ the event manager is not registered. + If EventMgrName={via,Module,ViaName}, the event manager will + register with the registry represented by Module. + The Module callback should export the functions + register_name/2, unregister_name/1, + whereis_name/1 and send/2, which should behave like the + corresponding functions in global. Thus, + {via,global,GlobalName} is a valid reference.

If the event manager is successfully created the function returns {ok,Pid}, where Pid is the pid of the event manager. If there already exists a process with @@ -149,8 +158,10 @@ gen_event:stop -----> Module:terminate/2 start(EventMgrName) -> Result Create a stand-alone event manager process. - EventMgrName = {local,Name} | {global,Name} + EventMgrName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() +  GlobalName = ViaName = term() Result = {ok,Pid} | {error,{already_started,Pid}}  Pid = pid() @@ -166,8 +177,10 @@ gen_event:stop -----> Module:terminate/2 add_handler(EventMgrRef, Handler, Args) -> Result Add an event handler to a generic event manager. - EventMgr = Name | {Name,Node} | {global,Name} | pid() + EventMgr = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() +  GlobalName = ViaName = term() Handler = Module | {Module,Id}  Module = atom()  Id = term() @@ -185,8 +198,10 @@ gen_event:stop -----> Module:terminate/2 Name, if the event manager is locally registered, {Name,Node}, if the event manager is locally registered at another node, or - {global,Name}, if the event manager is globally + {global,GlobalName}, if the event manager is globally registered. + {via,Module,ViaName}, if the event manager is registered + through an alternative process registry.

Handler is the name of the callback module Module or a tuple {Module,Id}, where Id is any term. @@ -207,8 +222,10 @@ gen_event:stop -----> Module:terminate/2 add_sup_handler(EventMgrRef, Handler, Args) -> Result Add a supervised event handler to a generic event manager. - EventMgr = Name | {Name,Node} | {global,Name} | pid() + EventMgr = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() +  GlobalName = ViaName = term() Handler = Module | {Module,Id}  Module = atom()  Id = term() @@ -252,8 +269,10 @@ gen_event:stop -----> Module:terminate/2 sync_notify(EventMgrRef, Event) -> ok Notify an event manager about an event. - EventMgrRef = Name | {Name,Node} | {global,Name} | pid() + EventMgrRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() +  GlobalName = ViaName = term() Event = term() @@ -277,8 +296,10 @@ gen_event:stop -----> Module:terminate/2 call(EventMgrRef, Handler, Request, Timeout) -> Result Make a synchronous call to a generic event manager. - EventMgrRef = Name | {Name,Node} | {global,Name} | pid() + EventMgrRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() +  GlobalName = ViaName = term() Handler = Module | {Module,Id}  Module = atom()  Id = term() @@ -317,8 +338,10 @@ gen_event:stop -----> Module:terminate/2 delete_handler(EventMgrRef, Handler, Args) -> Result Delete an event handler from a generic event manager. - EventMgrRef = Name | {Name,Node} | {global,Name} | pid() + EventMgrRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() +  GlobalName = ViaName = term() Handler = Module | {Module,Id}  Module = atom()  Id = term() @@ -345,8 +368,10 @@ gen_event:stop -----> Module:terminate/2 swap_handler(EventMgrRef, {Handler1,Args1}, {Handler2,Args2}) -> Result Replace an event handler in a generic event manager. - EventMgrRef = Name | {Name,Node} | {global,Name} | pid() + EventMgrRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() +  GlobalName = ViaName = term() Handler1 = Handler2 = Module | {Module,Id}  Module = atom()  Id = term() @@ -389,8 +414,10 @@ gen_event:stop -----> Module:terminate/2 swap_sup_handler(EventMgrRef, {Handler1,Args1}, {Handler2,Args2}) -> Result Replace an event handler in a generic event manager. - EventMgrRef = Name | {Name,Node} | {global,Name} | pid() + EventMgrRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() +  GlobalName = ViaName = term() Handler1 = Handler 2 = Module | {Module,Id}  Module = atom()  Id = term() @@ -411,8 +438,10 @@ gen_event:stop -----> Module:terminate/2 which_handlers(EventMgrRef) -> [Handler] Return all event handlers installed in a generic event manager. - EventMgrRef = Name | {Name,Node} | {global,Name} | pid() + EventMgrRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() +  GlobalName = ViaName = term() Handler = Module | {Module,Id}  Module = atom()  Id = term() @@ -428,8 +457,10 @@ gen_event:stop -----> Module:terminate/2 stop(EventMgrRef) -> ok Terminate a generic event manager. - EventMgrRef = Name | {Name,Node} | {global,Name} | pid() + EventMgrRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid() Name = Node = atom() + GlobalName = ViaName = term()

Terminates the event manager EventMgrRef. Before diff --git a/lib/stdlib/doc/src/gen_fsm.xml b/lib/stdlib/doc/src/gen_fsm.xml index 421eeb4fd3..73c1911f1e 100644 --- a/lib/stdlib/doc/src/gen_fsm.xml +++ b/lib/stdlib/doc/src/gen_fsm.xml @@ -84,9 +84,10 @@ gen_fsm:sync_send_all_state_event -----> Module:handle_sync_event/4 start_link(FsmName, Module, Args, Options) -> Result Create a gen_fsm process in a supervision tree. - FsmName = {local,Name} | {global,GlobalName} + FsmName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Module = atom() Args = term() Options = [Option] @@ -113,8 +114,16 @@ gen_fsm:sync_send_all_state_event -----> Module:handle_sync_event/4 locally as Name using register/2. If FsmName={global,GlobalName}, the gen_fsm is registered globally as GlobalName using - global:register_name/2. If no name is provided, - the gen_fsm is not registered.

+ global:register_name/2. + If EventMgrName={via,Module,ViaName}, the event manager will + register with the registry represented by Module. + The Module callback should export the functions + register_name/2, unregister_name/1, + whereis_name/1 and send/2, which should behave like the + corresponding functions in global. Thus, + {via,global,GlobalName} is a valid reference.

+

If no name is provided, + the gen_fsm is not registered.

Module is the name of the callback module.

Args is an arbitrary term which is passed as the argument to Module:init/1.

@@ -154,9 +163,10 @@ gen_fsm:sync_send_all_state_event -----> Module:handle_sync_event/4 start(FsmName, Module, Args, Options) -> Result Create a stand-alone gen_fsm process. - FsmName = {local,Name} | {global,GlobalName} + FsmName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Module = atom() Args = term() Options = [Option] @@ -180,9 +190,10 @@ gen_fsm:sync_send_all_state_event -----> Module:handle_sync_event/4 send_event(FsmRef, Event) -> ok Send an event asynchronously to a generic FSM. - FsmRef = Name | {Name,Node} | {global,GlobalName} | pid() + FsmRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Event = term() @@ -196,9 +207,11 @@ gen_fsm:sync_send_all_state_event -----> Module:handle_sync_event/4 the pid, Name, if the gen_fsm is locally registered, {Name,Node}, if the gen_fsm is locally - registered at another node, or - {global,GlobalName}, if the gen_fsm is globally - registered. + registered at another node, or + {global,GlobalName}, if the gen_fsm is globally + registered. + {via,Module,ViaName}, if the event manager is registered + through an alternative process registry.

Event is an arbitrary term which is passed as one of the arguments to Module:StateName/2.

@@ -208,9 +221,10 @@ gen_fsm:sync_send_all_state_event -----> Module:handle_sync_event/4 send_all_state_event(FsmRef, Event) -> ok Send an event asynchronously to a generic FSM. - FsmRef = Name | {Name,Node} | {global,GlobalName} | pid() + FsmRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Event = term() @@ -232,9 +246,10 @@ gen_fsm:sync_send_all_state_event -----> Module:handle_sync_event/4 sync_send_event(FsmRef, Event, Timeout) -> Reply Send an event synchronously to a generic FSM. - FsmRef = Name | {Name,Node} | {global,GlobalName} | pid() + FsmRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Event = term() Timeout = int()>0 | infinity Reply = term() @@ -264,9 +279,10 @@ gen_fsm:sync_send_all_state_event -----> Module:handle_sync_event/4 sync_send_all_state_event(FsmRef, Event, Timeout) -> Reply Send an event synchronously to a generic FSM. - FsmRef = Name | {Name,Node} | {global,GlobalName} | pid() + FsmRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Name = Node = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Event = term() Timeout = int()>0 | infinity Reply = term() @@ -388,9 +404,10 @@ gen_fsm:sync_send_all_state_event -----> Module:handle_sync_event/4     | {log_to_file,FileName} | {install,{Func,FuncState}} StateName = atom() StateData = term() - FsmName = {local,Name} | {global,GlobalName} + FsmName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Timeout = int() | infinity diff --git a/lib/stdlib/doc/src/gen_server.xml b/lib/stdlib/doc/src/gen_server.xml index 1045766e01..90b77d5cb6 100644 --- a/lib/stdlib/doc/src/gen_server.xml +++ b/lib/stdlib/doc/src/gen_server.xml @@ -83,9 +83,10 @@ gen_server:abcast -----> Module:handle_cast/2 start_link(ServerName, Module, Args, Options) -> Result Create a gen_server process in a supervision tree. - ServerName = {local,Name} | {global,GlobalName} + ServerName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Module = atom() Args = term() Options = [Option] @@ -111,14 +112,22 @@ gen_server:abcast -----> Module:handle_cast/2 If ServerName={global,GlobalName} the gen_server is registered globally as GlobalName using global:register_name/2. If no name is provided, - the gen_server is not registered.

+ the gen_server is not registered. + If EventMgrName={via,Module,ViaName}, the event manager will + register with the registry represented by Module. + The Module callback should export the functions + register_name/2, unregister_name/1, + whereis_name/1 and send/2, which should behave like the + corresponding functions in global. Thus, + {via,global,GlobalName} is a valid reference.

Module is the name of the callback module.

Args is an arbitrary term which is passed as the argument to Module:init/1.

If the option {timeout,Time} is present, the gen_server is allowed to spend Time milliseconds initializing or it will be terminated and the start function - will return {error,timeout}.

+ will return {error,timeout}. +

If the option {debug,Dbgs} is present, the corresponding sys function will be called for each item in Dbgs. See @@ -151,9 +160,10 @@ gen_server:abcast -----> Module:handle_cast/2 start(ServerName, Module, Args, Options) -> Result Create a stand-alone gen_server process. - ServerName = {local,Name} | {global,GlobalName} + ServerName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Module = atom() Args = term() Options = [Option] @@ -178,9 +188,10 @@ gen_server:abcast -----> Module:handle_cast/2 call(ServerRef, Request, Timeout) -> Reply Make a synchronous call to a generic server. - ServerRef = Name | {Name,Node} | {global,GlobalName} | pid() + ServerRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Node = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Request = term() Timeout = int()>0 | infinity Reply = term() @@ -198,6 +209,8 @@ gen_server:abcast -----> Module:handle_cast/2 registered at another node, or {global,GlobalName}, if the gen_server is globally registered. + {via,Module,ViaName}, if the gen_server is + registered through an alternative process registry.

Request is an arbitrary term which is passed as one of the arguments to Module:handle_call/3.

@@ -281,9 +294,10 @@ gen_server:abcast -----> Module:handle_cast/2 cast(ServerRef, Request) -> ok Send an asynchronous request to a generic server. - ServerRef = Name | {Name,Node} | {global,GlobalName} | pid() + ServerRef = Name | {Name,Node} | {global,GlobalName} + | {via,Module,ViaName} | pid()  Node = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Request = term() @@ -355,9 +369,10 @@ gen_server:abcast -----> Module:handle_cast/2    Dbg = trace | log | statistics     | {log_to_file,FileName} | {install,{Func,FuncState}} State = term() - ServerName = {local,Name} | {global,GlobalName} + ServerName = {local,Name} | {global,GlobalName} + | {via,Module,ViaName}  Name = atom() -  GlobalName = term() +  GlobalName = ViaName = term() Timeout = int() | infinity
diff --git a/lib/stdlib/doc/src/supervisor.xml b/lib/stdlib/doc/src/supervisor.xml index cddb55e5c5..73df7183e6 100644 --- a/lib/stdlib/doc/src/supervisor.xml +++ b/lib/stdlib/doc/src/supervisor.xml @@ -271,8 +271,14 @@ child_spec() = {Id,StartFunc,Restart,Shutdown,Type,Modules}

If SupName={local,Name} the supervisor is registered locally as Name using register/2. If SupName={global,Name} the supervisor is registered - globally as Name using global:register_name/2. - If no name is provided, the supervisor is not registered.

+ globally as Name using global:register_name/2. If + SupName={via,Module,Name} the supervisor + is registered as Name using the registry represented by + Module. The Module callback should export the functions + register_name/2, unregister_name/1 and send/2, + which should behave like the corresponding functions in global. + Thus, {via,global,Name} is a valid reference.

+

If no name is provided, the supervisor is not registered.

Module is the name of the callback module.

Args is an arbitrary term which is passed as the argument to Module:init/1.

@@ -315,6 +321,8 @@ child_spec() = {Id,StartFunc,Restart,Shutdown,Type,Modules} registered at another node, or {global,Name}, if the supervisor is globally registered. + {via,Module,Name}, if the supervisor is registered + through an alternative process registry.

ChildSpec should be a valid child specification (unless the supervisor is a simple_one_for_one diff --git a/lib/stdlib/doc/src/supervisor_bridge.xml b/lib/stdlib/doc/src/supervisor_bridge.xml index c1a5e7947f..f6712d6c1d 100644 --- a/lib/stdlib/doc/src/supervisor_bridge.xml +++ b/lib/stdlib/doc/src/supervisor_bridge.xml @@ -63,6 +63,13 @@ If SupBridgeName={global,Name} the supervisor_bridge is registered globally as Name using global:register_name/2. + If SupBridgeName={via,Module,Name} the supervisor_bridge is + registered as Name using a registry represented + by Module. The Module callback should export + the functions register_name/2, unregister_name/1 + and send/2, which should behave like the + corresponding functions in global. Thus, + {via,global,GlobalName} is a valid reference. If no name is provided, the supervisor_bridge is not registered. If there already exists a process with the specified SupBridgeName the function returns -- cgit v1.2.3