gproc.erl 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866
  1. %% ``The contents of this file are subject to the Erlang Public License,
  2. %% Version 1.1, (the "License"); you may not use this file except in
  3. %% compliance with the License. You should have received a copy of the
  4. %% Erlang Public License along with this software. If not, it can be
  5. %% retrieved via the world wide web at http://www.erlang.org/.
  6. %%
  7. %% Software distributed under the License is distributed on an "AS IS"
  8. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  9. %% the License for the specific language governing rights and limitations
  10. %% under the License.
  11. %%
  12. %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
  13. %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
  14. %% AB. All Rights Reserved.''
  15. %%
  16. %% @author Ulf Wiger <ulf@wiger.net>
  17. %%
  18. %% @doc Extended process registry
  19. %% This module implements an extended process registry
  20. %%
  21. %% For a detailed description, see
  22. %% <a href="erlang07-wiger.pdf">erlang07-wiger.pdf</a>.
  23. %%
  24. %% <h2>Tuning Gproc performance</h2>
  25. %%
  26. %% Gproc relies on a central server and an ordered-set ets table.
  27. %% Effort is made to perform as much work as possible in the client without
  28. %% sacrificing consistency. A few things can be tuned by setting the following
  29. %% application environment variables in the top application of `gproc'
  30. %% (usually `gproc'):
  31. %%
  32. %% * `{ets_options, list()}' - Currently, the options `{write_concurrency, F}'
  33. %% and `{read_concurrency, F}' are allowed. The default is
  34. %% `[{write_concurrency, true}, {read_concurrency, true}]'
  35. %% * `{server_options, list()}' - These will be passed as spawn options when
  36. %% starting the `gproc' and `gproc_dist' servers. Default is `[]'. It is
  37. %% likely that `{priority, high | max}' and/or increasing `min_heap_size'
  38. %% will improve performance.
  39. %%
  40. %% @end
  41. %% @type type() = n | p | c | a. n = name; p = property; c = counter;
  42. %% a = aggregate_counter
  43. %% @type scope() = l | g. l = local registration; g = global registration
  44. %%
  45. %% @type reg_id() = {type(), scope(), any()}.
  46. %% @type unique_id() = {n | a, scope(), any()}.
  47. %%
  48. %% @type monitor_type() = info | standby | follow.
  49. %%
  50. %% @type sel_scope() = scope | all | global | local.
  51. %% @type sel_type() = type() | names | props | counters | aggr_counters.
  52. %% @type context() = {scope(), type()} | type(). {'all','all'} is the default
  53. %%
  54. %% @type headpat() = {keypat(),pidpat(),ValPat}.
  55. %% @type keypat() = {sel_type() | sel_var(),
  56. %% l | g | sel_var(),
  57. %% any()}.
  58. %% @type pidpat() = pid() | sel_var().
  59. %% @type sel_var() = DollarVar | '_'.
  60. %% @type sel_pattern() = [{headpat(), Guards, Prod}].
  61. %% @type key() = {type(), scope(), any()}.
  62. %%
  63. %% update_counter increment
  64. %% @type ctr_incr() = integer().
  65. %% @type ctr_thr() = integer().
  66. %% @type ctr_setval() = integer().
  67. %% @type ctr_update() = ctr_incr()
  68. %% | {ctr_incr(), ctr_thr(), ctr_setval()}.
  69. %% @type increment() = ctr_incr() | ctr_update() | [ctr_update()].
  70. -module(gproc).
  71. -behaviour(gen_server).
  72. -export([start_link/0,
  73. reg/1, reg/2, unreg/1, set_attributes/2,
  74. reg_or_locate/1, reg_or_locate/2, reg_or_locate/3,
  75. reg_shared/1, reg_shared/2, unreg_shared/1,
  76. set_attributes_shared/2, set_value_shared/2,
  77. mreg/3,
  78. munreg/3,
  79. set_value/2,
  80. get_value/1, get_value/2, get_value_shared/1,
  81. get_attribute/2, get_attribute/3, get_attribute_shared/2,
  82. get_attributes/1, get_attributes/2,
  83. where/1,
  84. await/1, await/2, await/3,
  85. wide_await/3,
  86. nb_wait/1, nb_wait/2,
  87. cancel_wait/2, cancel_wait/3,
  88. cancel_wait_or_monitor/1,
  89. monitor/1, monitor/2,
  90. demonitor/2,
  91. lookup_pid/1,
  92. lookup_pids/1,
  93. lookup_value/1,
  94. lookup_values/1,
  95. update_counter/2, update_counter/3,
  96. update_counters/2,
  97. reset_counter/1,
  98. update_shared_counter/2,
  99. give_away/2,
  100. goodbye/0,
  101. send/2,
  102. bcast/2, bcast/3,
  103. info/1, info/2,
  104. i/0,
  105. select/1, select/2, select/3,
  106. select_count/1, select_count/2,
  107. first/1,
  108. next/2,
  109. prev/2,
  110. last/1,
  111. table/0, table/1, table/2]).
  112. %% Environment handling
  113. -export([get_env/3, get_env/4,
  114. get_set_env/3, get_set_env/4,
  115. set_env/5]).
  116. %% Convenience functions
  117. -export([add_local_name/1,
  118. add_global_name/1,
  119. add_local_property/2,
  120. add_global_property/2,
  121. add_local_counter/2,
  122. add_global_counter/2,
  123. add_local_aggr_counter/1,
  124. add_global_aggr_counter/1,
  125. add_shared_local_counter/2,
  126. lookup_local_name/1,
  127. lookup_global_name/1,
  128. lookup_local_properties/1,
  129. lookup_global_properties/1,
  130. lookup_local_counters/1,
  131. lookup_global_counters/1,
  132. lookup_local_aggr_counter/1,
  133. lookup_global_aggr_counter/1]).
  134. %% Callbacks for behaviour support
  135. -export([whereis_name/1,
  136. register_name/2,
  137. unregister_name/1]).
  138. -export([default/1]).
  139. %%% internal exports
  140. -export([init/1,
  141. handle_cast/2,
  142. handle_call/3,
  143. handle_info/2,
  144. code_change/3,
  145. terminate/2]).
  146. %% this shouldn't be necessary
  147. -export([audit_process/1]).
  148. -include("gproc_int.hrl").
  149. -include("gproc.hrl").
  150. -define(SERVER, ?MODULE).
  151. %%-define(l, l(?LINE)). % when activated, calls a traceable empty function
  152. -define(l, ignore).
  153. -define(CHK_DIST,
  154. case whereis(gproc_dist) of
  155. undefined ->
  156. ?THROW_GPROC_ERROR(local_only);
  157. _ ->
  158. ok
  159. end).
  160. -define(PID_IS_DEAD(Pid),
  161. (node(Pid) == node() andalso is_process_alive(Pid) == false)).
  162. -record(state, {}).
  163. %% @spec () -> {ok, pid()}
  164. %%
  165. %% @doc Starts the gproc server.
  166. %%
  167. %% This function is intended to be called from gproc_sup, as part of
  168. %% starting the gproc application.
  169. %% @end
  170. start_link() ->
  171. _ = create_tabs(),
  172. SpawnOpts = gproc_lib:valid_opts(server_options, []),
  173. gen_server:start_link({local, ?SERVER}, ?MODULE, [],
  174. [{spawn_opt, SpawnOpts}]).
  175. %% spec(Name::any()) -> true
  176. %%
  177. %% @doc Registers a local (unique) name. @equiv reg({n,l,Name})
  178. %% @end
  179. %%
  180. add_local_name(Name) -> ?CATCH_GPROC_ERROR(reg1({n,l,Name}, undefined), [Name]).
  181. %% spec(Name::any()) -> true
  182. %%
  183. %% @doc Registers a global (unique) name. @equiv reg({n,g,Name})
  184. %% @end
  185. %%
  186. add_global_name(Name) -> ?CATCH_GPROC_ERROR(reg1({n,g,Name}, undefined), [Name]).
  187. %% spec(Name::any(), Value::any()) -> true
  188. %%
  189. %% @doc Registers a local (non-unique) property. @equiv reg({p,l,Name},Value)
  190. %% @end
  191. %%
  192. add_local_property(Name , Value) ->
  193. ?CATCH_GPROC_ERROR(reg1({p,l,Name}, Value), [Name, Value]).
  194. %% spec(Name::any(), Value::any()) -> true
  195. %%
  196. %% @doc Registers a global (non-unique) property. @equiv reg({p,g,Name},Value)
  197. %% @end
  198. %%
  199. add_global_property(Name, Value) ->
  200. ?CATCH_GPROC_ERROR(reg1({p,g,Name}, Value), [Name, Value]).
  201. %% spec(Name::any(), Initial::integer()) -> true
  202. %%
  203. %% @doc Registers a local (non-unique) counter. @equiv reg({c,l,Name},Value)
  204. %% @end
  205. %%
  206. add_local_counter(Name, Initial) when is_integer(Initial) ->
  207. ?CATCH_GPROC_ERROR(reg1({c,l,Name}, Initial), [Name, Initial]).
  208. %% spec(Name::any(), Initial::integer()) -> true
  209. %%
  210. %% @doc Registers a local shared (unique) counter.
  211. %% @equiv reg_shared({c,l,Name},Value)
  212. %% @end
  213. %%
  214. add_shared_local_counter(Name, Initial) when is_integer(Initial) ->
  215. reg_shared({c,l,Name}, Initial).
  216. %% spec(Name::any(), Initial::integer()) -> true
  217. %%
  218. %% @doc Registers a global (non-unique) counter. @equiv reg({c,g,Name},Value)
  219. %% @end
  220. %%
  221. add_global_counter(Name, Initial) when is_integer(Initial) ->
  222. ?CATCH_GPROC_ERROR(reg1({c,g,Name}, Initial), [Name, Initial]).
  223. %% spec(Name::any()) -> true
  224. %%
  225. %% @doc Registers a local (unique) aggregated counter.
  226. %% @equiv reg({a,l,Name})
  227. %% @end
  228. %%
  229. add_local_aggr_counter(Name) -> ?CATCH_GPROC_ERROR(reg1({a,l,Name}), [Name]).
  230. %% spec(Name::any()) -> true
  231. %%
  232. %% @doc Registers a global (unique) aggregated counter.
  233. %% @equiv reg({a,g,Name})
  234. %% @end
  235. %%
  236. add_global_aggr_counter(Name) -> ?CATCH_GPROC_ERROR(reg1({a,g,Name}), [Name]).
  237. %% @spec (Name::any()) -> pid()
  238. %%
  239. %% @doc Lookup a local unique name. Fails if there is no such name.
  240. %% @equiv where({n,l,Name})
  241. %% @end
  242. %%
  243. lookup_local_name(Name) -> where({n,l,Name}).
  244. %% @spec (Name::any()) -> pid()
  245. %%
  246. %% @doc Lookup a global unique name. Fails if there is no such name.
  247. %% @equiv where({n,g,Name})
  248. %% @end
  249. %%
  250. lookup_global_name(Name) -> where({n,g,Name}).
  251. %% @spec (Name::any()) -> integer()
  252. %%
  253. %% @doc Lookup a local (unique) aggregated counter and returns its value.
  254. %% Fails if there is no such object.
  255. %% @equiv where({a,l,Name})
  256. %% @end
  257. %%
  258. lookup_local_aggr_counter(Name) -> lookup_value({a,l,Name}).
  259. %% @spec (Name::any()) -> integer()
  260. %%
  261. %% @doc Lookup a global (unique) aggregated counter and returns its value.
  262. %% Fails if there is no such object.
  263. %% @equiv lookup_value({a,g,Name})
  264. %% @end
  265. %%
  266. lookup_global_aggr_counter(Name) -> lookup_value({a,g,Name}).
  267. %% @spec (Property::any()) -> [{pid(), Value}]
  268. %%
  269. %% @doc Look up all local (non-unique) instances of a given Property.
  270. %% Returns a list of {Pid, Value} tuples for all matching objects.
  271. %% @equiv lookup_values({p, l, Property})
  272. %% @end
  273. %%
  274. lookup_local_properties(P) -> lookup_values({p,l,P}).
  275. %% @spec (Property::any()) -> [{pid(), Value}]
  276. %%
  277. %% @doc Look up all global (non-unique) instances of a given Property.
  278. %% Returns a list of {Pid, Value} tuples for all matching objects.
  279. %% @equiv lookup_values({p, g, Property})
  280. %% @end
  281. %%
  282. lookup_global_properties(P) -> lookup_values({p,g,P}).
  283. %% @spec (Counter::any()) -> [{pid(), Value::integer()}]
  284. %%
  285. %% @doc Look up all local (non-unique) instances of a given Counter.
  286. %% Returns a list of {Pid, Value} tuples for all matching objects.
  287. %% @equiv lookup_values({c, l, Counter})
  288. %% @end
  289. %%
  290. lookup_local_counters(P) -> lookup_values({c,l,P}).
  291. %% @spec (Counter::any()) -> [{pid(), Value::integer()}]
  292. %%
  293. %% @doc Look up all global (non-unique) instances of a given Counter.
  294. %% Returns a list of {Pid, Value} tuples for all matching objects.
  295. %% @equiv lookup_values({c, g, Counter})
  296. %% @end
  297. %%
  298. lookup_global_counters(P) -> lookup_values({c,g,P}).
  299. %% @spec get_env(Scope::scope(), App::atom(), Key::atom()) -> term()
  300. %% @equiv get_env(Scope, App, Key, [app_env])
  301. get_env(Scope, App, Key) ->
  302. get_env(Scope, App, Key, [app_env]).
  303. %% @spec (Scope::scope(), App::atom(), Key::atom(), Strategy) -> term()
  304. %% Strategy = [Alternative]
  305. %% Alternative = app_env
  306. %% | os_env
  307. %% | inherit | {inherit, pid()} | {inherit, unique_id()}
  308. %% | init_arg
  309. %% | {mnesia, ActivityType, Oid, Pos}
  310. %% | {default, term()}
  311. %% | error
  312. %% @doc Read an environment value, potentially cached as a `gproc_env' property.
  313. %%
  314. %% This function first tries to read the value of a cached property,
  315. %% `{p, Scope, {gproc_env, App, Key}}'. If this fails, it will try the provided
  316. %% alternative strategy. `Strategy' is a list of alternatives, tried in order.
  317. %% Each alternative can be one of:
  318. %%
  319. %% * `app_env' - try `application:get_env(App, Key)'
  320. %% * `os_env' - try `os:getenv(ENV)', where `ENV' is `Key' converted into an
  321. %% uppercase string
  322. %% * `{os_env, ENV}' - try `os:getenv(ENV)'
  323. %% * `inherit' - inherit the cached value, if any, held by the parent process.
  324. %% * `{inherit, Pid}' - inherit the cached value, if any, held by `Pid'.
  325. %% * `{inherit, Id}' - inherit the cached value, if any, held by the process
  326. %% registered in `gproc' as `Id'.
  327. %% * `init_arg' - try `init:get_argument(Key)'; expects a single value, if any.
  328. %% * `{mnesia, ActivityType, Oid, Pos}' - try
  329. %% `mnesia:activity(ActivityType, fun() -> mnesia:read(Oid) end)'; retrieve
  330. %% the value in position `Pos' if object found.
  331. %% * `{default, Value}' - set a default value to return once alternatives have
  332. %% been exhausted; if not set, `undefined' will be returned.
  333. %% * `error' - raise an exception, `erlang:error(gproc_env, [App, Key, Scope])'.
  334. %%
  335. %% While any alternative can occur more than once, the only one that might make
  336. %% sense to use multiple times is `{default, Value}'.
  337. %%
  338. %% The return value will be one of:
  339. %%
  340. %% * The value of the first matching alternative, or `error' eception,
  341. %% whichever comes first
  342. %% * The last instance of `{default, Value}', or `undefined', if there is no
  343. %% matching alternative, default or `error' entry in the list.
  344. %%
  345. %% The `error' option can be used to assert that a value has been previously
  346. %% cached. Alternatively, it can be used to assert that a value is either cached
  347. %% or at least defined somewhere,
  348. %% e.g. `get_env(l, mnesia, dir, [app_env, error])'.
  349. %% @end
  350. get_env(Scope, App, Key, Strategy)
  351. when Scope==l, is_atom(App), is_atom(Key);
  352. Scope==g, is_atom(App), is_atom(Key) ->
  353. do_get_env(Scope, App, Key, Strategy, false).
  354. %% @spec get_set_env(Scope::scope(), App::atom(), Key::atom()) -> term()
  355. %% @equiv get_set_env(Scope, App, Key, [app_env])
  356. get_set_env(Scope, App, Key) ->
  357. get_set_env(Scope, App, Key, [app_env]).
  358. %% @spec get_set_env(Scope::scope(), App::atom(), Key::atom(), Strategy) ->
  359. %% Value
  360. %% @doc Fetch and cache an environment value, if not already cached.
  361. %%
  362. %% This function does the same thing as {@link get_env/4}, but also updates the
  363. %% cache. Note that the cache will be updated even if the result of the lookup
  364. %% is `undefined'.
  365. %%
  366. %% @see get_env/4.
  367. %% @end
  368. %%
  369. get_set_env(Scope, App, Key, Strategy)
  370. when Scope==l, is_atom(App), is_atom(Key);
  371. Scope==g, is_atom(App), is_atom(Key) ->
  372. do_get_env(Scope, App, Key, Strategy, true).
  373. do_get_env(Context, App, Key, Alternatives, Set) ->
  374. case lookup_env(Context, App, Key, self()) of
  375. undefined ->
  376. check_alternatives(Alternatives, Context, App, Key, undefined, Set);
  377. {ok, Value} ->
  378. Value
  379. end.
  380. %% @spec set_env(Scope::scope(), App::atom(),
  381. %% Key::atom(), Value::term(), Strategy) -> Value
  382. %% Strategy = [Alternative]
  383. %% Alternative = app_env | os_env | {os_env, VAR}
  384. %% | {mnesia, ActivityType, Oid, Pos}
  385. %%
  386. %% @doc Updates the cached value as well as underlying environment.
  387. %%
  388. %% This function should be exercised with caution, as it affects the larger
  389. %% environment outside gproc. This function modifies the cached value, and then
  390. %% proceeds to update the underlying environment (OS environment variable or
  391. %% application environment variable).
  392. %%
  393. %% When the `mnesia' alternative is used, gproc will try to update any existing
  394. %% object, changing only the `Pos' position. If no such object exists, it will
  395. %% create a new object, setting any other attributes (except `Pos' and the key)
  396. %% to `undefined'.
  397. %% @end
  398. %%
  399. set_env(Scope, App, Key, Value, Strategy)
  400. when Scope==l, is_atom(App), is_atom(Key);
  401. Scope==g, is_atom(App), is_atom(Key) ->
  402. case is_valid_set_strategy(Strategy, Value) of
  403. true ->
  404. update_cached_env(Scope, App, Key, Value),
  405. set_strategy(Strategy, App, Key, Value);
  406. false ->
  407. erlang:error(badarg)
  408. end.
  409. check_alternatives([{default, Val}|Alts], Scope, App, Key, _, Set) ->
  410. check_alternatives(Alts, Scope, App, Key, Val, Set);
  411. check_alternatives([H|T], Scope, App, Key, Def, Set) ->
  412. case try_alternative(H, App, Key, Scope) of
  413. undefined ->
  414. check_alternatives(T, Scope, App, Key, Def, Set);
  415. {ok, Value} ->
  416. if Set ->
  417. cache_env(Scope, App, Key, Value),
  418. Value;
  419. true ->
  420. Value
  421. end
  422. end;
  423. check_alternatives([], Scope, App, Key, Def, Set) ->
  424. if Set ->
  425. cache_env(Scope, App, Key, Def);
  426. true ->
  427. ok
  428. end,
  429. Def.
  430. try_alternative(error, App, Key, Scope) ->
  431. erlang:error(gproc_env, [App, Key, Scope]);
  432. try_alternative(inherit, App, Key, Scope) ->
  433. case get('$ancestors') of
  434. [P|_] ->
  435. lookup_env(Scope, App, Key, P);
  436. _ ->
  437. undefined
  438. end;
  439. try_alternative({inherit, P}, App, Key, Scope) when is_pid(P) ->
  440. lookup_env(Scope, App, Key, P);
  441. try_alternative({inherit, P}, App, Key, Scope) ->
  442. case where(P) of
  443. undefined -> undefined;
  444. Pid when is_pid(Pid) ->
  445. lookup_env(Scope, App, Key, Pid)
  446. end;
  447. try_alternative(app_env, App, Key, _Scope) ->
  448. case application:get_env(App, Key) of
  449. undefined -> undefined;
  450. {ok, undefined} -> undefined;
  451. {ok, Value} -> {ok, Value}
  452. end;
  453. try_alternative(os_env, _App, Key, _) ->
  454. case os:getenv(os_env_key(Key)) of
  455. false -> undefined;
  456. Val -> {ok, Val}
  457. end;
  458. try_alternative({os_env, Key}, _, _, _) ->
  459. case os:getenv(Key) of
  460. false -> undefined;
  461. Val -> {ok, Val}
  462. end;
  463. try_alternative(init_arg, _, Key, _) ->
  464. case init:get_argument(Key) of
  465. {ok, [[Value]]} ->
  466. {ok, Value};
  467. error ->
  468. undefined
  469. end;
  470. try_alternative({mnesia,Type,Key,Pos}, _, _, _) ->
  471. case mnesia:activity(Type, fun() -> mnesia:read(Key) end) of
  472. [] -> undefined;
  473. [Found] ->
  474. {ok, element(Pos, Found)}
  475. end.
  476. os_env_key(Key) ->
  477. string:to_upper(atom_to_list(Key)).
  478. lookup_env(Scope, App, Key, P) ->
  479. case ets:lookup(?TAB, {{p, Scope, {gproc_env, App, Key}}, P}) of
  480. [] ->
  481. undefined;
  482. [{_, _, Value}] ->
  483. {ok, Value}
  484. end.
  485. cache_env(Scope, App, Key, Value) ->
  486. ?CATCH_GPROC_ERROR(
  487. reg1({p, Scope, {gproc_env, App, Key}}, Value),
  488. [Scope,App,Key,Value]).
  489. update_cached_env(Scope, App, Key, Value) ->
  490. case lookup_env(Scope, App, Key, self()) of
  491. undefined ->
  492. cache_env(Scope, App, Key, Value);
  493. {ok, _} ->
  494. set_value({p, Scope, {gproc_env, App, Key}}, Value)
  495. end.
  496. is_valid_set_strategy([os_env|T], Value) ->
  497. is_string(Value) andalso is_valid_set_strategy(T, Value);
  498. is_valid_set_strategy([{os_env, _}|T], Value) ->
  499. is_string(Value) andalso is_valid_set_strategy(T, Value);
  500. is_valid_set_strategy([app_env|T], Value) ->
  501. is_valid_set_strategy(T, Value);
  502. is_valid_set_strategy([{mnesia,_Type,_Oid,_Pos}|T], Value) ->
  503. is_valid_set_strategy(T, Value);
  504. is_valid_set_strategy([], _) ->
  505. true;
  506. is_valid_set_strategy(_, _) ->
  507. false.
  508. set_strategy([H|T], App, Key, Value) ->
  509. case H of
  510. app_env ->
  511. application:set_env(App, Key, Value);
  512. os_env ->
  513. os:putenv(os_env_key(Key), Value);
  514. {os_env, ENV} ->
  515. os:putenv(ENV, Value);
  516. {mnesia,Type,Oid,Pos} ->
  517. mnesia:activity(
  518. Type,
  519. fun() ->
  520. Rec = case mnesia:read(Oid) of
  521. [] ->
  522. {Tab,K} = Oid,
  523. Tag = mnesia:table_info(Tab, record_name),
  524. Attrs = mnesia:table_info(Tab, attributes),
  525. list_to_tuple(
  526. [Tag,K |
  527. [undefined || _ <- tl(Attrs)]]);
  528. [Old] ->
  529. Old
  530. end,
  531. mnesia:write(setelement(Pos, Rec, Value))
  532. end)
  533. end,
  534. set_strategy(T, App, Key, Value);
  535. set_strategy([], _, _, Value) ->
  536. Value.
  537. is_string(S) ->
  538. try begin _ = iolist_to_binary(S),
  539. true
  540. end
  541. catch
  542. error:_ ->
  543. false
  544. end.
  545. %% @spec reg(Key::key()) -> true
  546. %%
  547. %% @doc
  548. %% @equiv reg(Key, default(Key))
  549. %% @end
  550. reg(Key) ->
  551. ?CATCH_GPROC_ERROR(reg1(Key), [Key]).
  552. reg1(Key) ->
  553. reg1(Key, default(Key)).
  554. %% @spec reg_or_locate(Key::key()) -> {pid(), NewValue}
  555. %%
  556. %% @doc
  557. %% @equiv reg_or_locate(Key, default(Key))
  558. %% @end
  559. reg_or_locate(Key) ->
  560. ?CATCH_GPROC_ERROR(reg_or_locate1(Key), [Key]).
  561. reg_or_locate1(Key) ->
  562. reg_or_locate1(Key, default(Key), self()).
  563. default({T,_,_}) when T==c -> 0;
  564. default(_) -> undefined.
  565. %% @spec await(Key::key()) -> {pid(),Value}
  566. %% @equiv await(Key,infinity)
  567. %%
  568. await(Key) ->
  569. ?CATCH_GPROC_ERROR(await1(Key, infinity), [Key]).
  570. %% @spec await(Key::key(), Timeout) -> {pid(),Value}
  571. %% Timeout = integer() | infinity
  572. %%
  573. %% @doc Wait for a local name to be registered.
  574. %% The function raises an exception if the timeout expires. Timeout must be
  575. %% either an interger &gt; 0 or 'infinity'.
  576. %% A small optimization: we first perform a lookup, to see if the name
  577. %% is already registered. This way, the cost of the operation will be
  578. %% roughly the same as of where/1 in the case where the name is already
  579. %% registered (the difference: await/2 also returns the value).
  580. %% @end
  581. %%
  582. await(Key, Timeout) ->
  583. ?CATCH_GPROC_ERROR(await1(Key, Timeout), [Key, Timeout]).
  584. %% @spec await(Node::node(), Key::key(), Timeout) -> {pid(),Value}
  585. %% Timeout = integer() | infinity
  586. %%
  587. %% @doc Wait for a local name to be registered on `Node'.
  588. %% This function works exactly like {@link await/2}, but queries a remote
  589. %% node instead. An exception is thrown if `Node' cannot be reached. If gproc
  590. %% is not running on a given node, this is treated the same as the node being
  591. %% down.
  592. %% @end
  593. %%
  594. await(Node, Key, Timeout) when Node == node() ->
  595. await(Key, Timeout);
  596. await(Node, Key, Timeout) when is_atom(Node) ->
  597. ?CATCH_GPROC_ERROR(await1(Node, Key, Timeout), [Node, Key, Timeout]).
  598. await1({n,g,_} = Key, Timeout) ->
  599. ?CHK_DIST,
  600. request_wait(Key, Timeout);
  601. await1({n,l,_} = Key, Timeout) ->
  602. case ets:lookup(?TAB, {Key, n}) of
  603. [{_, Pid, Value}] ->
  604. case is_process_alive(Pid) of
  605. true ->
  606. {Pid, Value};
  607. false ->
  608. %% we can send an asynchronous audit request, since the purpose is
  609. %% only to ensure that the server handles the audit before it serves
  610. %% our 'await' request. Strictly speaking, we could allow the bad Pid
  611. %% to be returned, as there are no guarantees that whatever Pid we return
  612. %% will still be alive when addressed. Still, we don't want to knowingly
  613. %% serve bad data.
  614. nb_audit_process(Pid),
  615. request_wait(Key, Timeout)
  616. end;
  617. _ ->
  618. request_wait(Key, Timeout)
  619. end;
  620. await1(_, _) ->
  621. throw(badarg).
  622. await1(N, {n,l,_} = Key, Timeout) when is_atom(N) ->
  623. request_wait(N, Key, Timeout);
  624. await1(_, _, _) ->
  625. throw(badarg).
  626. request_wait({_,g,_} = Key, Timeout) ->
  627. request_wait(undefined, Key, Timeout);
  628. request_wait(Key, Timeout) ->
  629. request_wait(node(), Key, Timeout).
  630. request_wait(N, {n,C,_} = Key, Timeout) when C==l; C==g ->
  631. TRef = case Timeout of
  632. infinity -> no_timer;
  633. T when is_integer(T), T > 0 ->
  634. erlang:start_timer(T, self(), gproc_timeout);
  635. _ ->
  636. ?THROW_GPROC_ERROR(badarg)
  637. end,
  638. WRef = case {call(N, {await,Key,self()}, C), C} of
  639. {{R, {Kg,Pg,Vg}}, g} ->
  640. self() ! {gproc, R, registered, {Kg,Pg,Vg}},
  641. R;
  642. {R,_} ->
  643. R
  644. end,
  645. receive
  646. {gproc, WRef, registered, {_K, Pid, V}} ->
  647. _ = case TRef of
  648. no_timer -> ignore;
  649. _ -> erlang:cancel_timer(TRef)
  650. end,
  651. {Pid, V};
  652. {timeout, TRef, gproc_timeout} ->
  653. cancel_wait(N, Key, WRef),
  654. ?THROW_GPROC_ERROR(timeout)
  655. end.
  656. %% @spec wide_await(Nodes::[node()], Key::key(), Timeout) -> {pid(),Value}
  657. %% Timeout = integer() | infinity
  658. %%
  659. %% @doc Wait for a local name to be registered on any of `Nodes'.
  660. %% This function works rather like {@link await/2}, but queries all nodes in
  661. %% the `Nodes' list at the same time. The first node to respond with a
  662. %% process registered as `Key' will provide the result. Other results are
  663. %% ignored. `Key' must be a unique name with local scope, i.e. `{n,l,Name}'.
  664. %%
  665. %% An exception is thrown upon timeout, or if no node can be reached (if gproc is
  666. %% not running on a given node, this is treated the same as the node being down).
  667. %% @end
  668. %%
  669. wide_await(Nodes, Key, Timeout) ->
  670. ?CATCH_GPROC_ERROR(wide_await1(Nodes, Key, Timeout), [Nodes, Key, Timeout]).
  671. wide_await1(Nodes, {n,l,_} = Key, Timeout) ->
  672. {_, Ref} = spawn_monitor(fun() ->
  673. wide_request_wait(Nodes, Key, Timeout)
  674. end),
  675. receive
  676. {'DOWN', Ref, _, _, Reason} ->
  677. case Reason of
  678. {ok, {gproc,_,registered,{_,Pid,V}}} ->
  679. {Pid, V};
  680. Other ->
  681. ?THROW_GPROC_ERROR(Other)
  682. end
  683. end;
  684. wide_await1(_, _, _) ->
  685. ?THROW_GPROC_ERROR(badarg).
  686. wide_request_wait(Nodes, {n,l,_} = Key, Timeout) ->
  687. TRef = case Timeout of
  688. infinity -> no_timer;
  689. T when is_integer(T), T > 0 ->
  690. erlang:start_timer(T, self(), gproc_timeout);
  691. _ ->
  692. exit(badarg)
  693. end,
  694. Req = {await, Key, self()},
  695. Refs = lists:map(
  696. fun(Node) ->
  697. S = {?MODULE, Node},
  698. Ref = erlang:monitor(process, S),
  699. catch erlang:send(S, {'$gen_call', {self(), Ref}, Req},
  700. [noconnect]),
  701. {Node, Ref}
  702. end, Nodes),
  703. collect_replies(Refs, Key, TRef).
  704. collect_replies(Refs, Key, TRef) ->
  705. receive
  706. {gproc, _Ref, registered, {_, _, _}} = Result ->
  707. exit({ok, Result});
  708. {'DOWN', Ref, _, _, _} ->
  709. case lists:keydelete(Ref, 2, Refs) of
  710. [] ->
  711. exit(nodedown);
  712. Refs1 ->
  713. collect_replies(Refs1, Key, TRef)
  714. end;
  715. {timeout, TRef, gproc_timeout} ->
  716. exit(timeout);
  717. {Ref, Ref} ->
  718. %% ignore
  719. collect_replies(Refs, Key, TRef)
  720. end.
  721. %% @spec nb_wait(Key::key()) -> Ref
  722. %%
  723. %% @doc Wait for a local name to be registered.
  724. %% The caller can expect to receive a message,
  725. %% {gproc, Ref, registered, {Key, Pid, Value}}, once the name is registered.
  726. %% @end
  727. %%
  728. nb_wait(Key) ->
  729. ?CATCH_GPROC_ERROR(nb_wait1(Key), [Key]).
  730. %% @spec nb_wait(Node::node(), Key::key()) -> Ref
  731. %%
  732. %% @doc Wait for a local name to be registered on `Node'.
  733. %% The caller can expect to receive a message,
  734. %% {gproc, Ref, registered, {Key, Pid, Value}}, once the name is registered.
  735. %% @end
  736. %%
  737. nb_wait(Node, {n,l,_} = Key) when is_atom(Node) ->
  738. ?CATCH_GPROC_ERROR(nb_wait1(Node, Key), [Node, Key]).
  739. nb_wait1({n,g,_} = Key) ->
  740. ?CHK_DIST,
  741. call({await, Key, self()}, g);
  742. nb_wait1({n,l,_} = Key) ->
  743. call({await, Key, self()}, l);
  744. nb_wait1(_) ->
  745. ?THROW_GPROC_ERROR(badarg).
  746. nb_wait1(Node, {n,l,_} = Key) when is_atom(Node) ->
  747. call(Node, {await, Key, self()}, l).
  748. %% @spec cancel_wait(Key::key(), Ref) -> ok
  749. %% Ref = all | reference()
  750. %%
  751. %% @doc Cancels a previous call to nb_wait/1
  752. %%
  753. %% If `Ref = all', all wait requests on `Key' from the calling process
  754. %% are canceled.
  755. %% @end
  756. %%
  757. cancel_wait(Key, Ref) ->
  758. ?CATCH_GPROC_ERROR(cancel_wait1(Key, Ref), [Key, Ref]).
  759. %% @spec cancel_wait(Node::node(), Key::key(), Ref) -> ok
  760. %% Ref = all | reference()
  761. %%
  762. %% @doc Cancels a previous call to nb_wait/2
  763. %%
  764. %% This function works just like {@link cancel_wait/2}, but talks to a remote
  765. %% node.
  766. %% @end
  767. %%
  768. cancel_wait(N, Key, Ref) when N == node() ->
  769. cancel_wait(Key, Ref);
  770. cancel_wait(N, Key, Ref) ->
  771. ?CATCH_GPROC_ERROR(cancel_wait1(N, Key, Ref), [N, Key, Ref]).
  772. cancel_wait1({_,g,_} = Key, Ref) ->
  773. ?CHK_DIST,
  774. cast({cancel_wait, self(), Key, Ref}, g),
  775. ok;
  776. cancel_wait1({_,l,_} = Key, Ref) ->
  777. cast({cancel_wait, self(), Key, Ref}, l),
  778. ok.
  779. cancel_wait1(undefined, {_,g,_} = Key, Ref) ->
  780. cast({cancel_wait, self(), Key, Ref}, g);
  781. cancel_wait1(N, {_,l,_} = Key, Ref) ->
  782. cast(N, {cancel_wait, self(), Key, Ref}, l).
  783. cancel_wait_or_monitor(Key) ->
  784. ?CATCH_GPROC_ERROR(cancel_wait_or_monitor1(Key), [Key]).
  785. cancel_wait_or_monitor1({_,g,_} = Key) ->
  786. ?CHK_DIST,
  787. cast({cancel_wait_or_monitor, self(), Key}, g),
  788. ok;
  789. cancel_wait_or_monitor1({_,l,_} = Key) ->
  790. cast({cancel_wait_or_monitor, self(), Key}, l),
  791. ok.
  792. %% @equiv monitor(Key, info)
  793. monitor(Key) ->
  794. ?CATCH_GPROC_ERROR(monitor1(Key, info), [Key]).
  795. %% @spec monitor(key(), monitor_type()) -> reference()
  796. %%
  797. %% @doc monitor a registered name
  798. %% `monitor(Key, info)' works much like erlang:monitor(process, Pid), but monitors
  799. %% a unique name registered via gproc. A message, `{gproc, unreg, Ref, Key}'
  800. %% will be sent to the requesting process, if the name is unregistered or
  801. %% the registered process dies. If there is a standby monitor (see below), a
  802. %% message `{gproc, {failover, ToPid}, Ref, Key}' is sent to all monitors.
  803. %% If the name is passed to another process using {@link give_away/2}, the event
  804. %% `{gproc, {migrated, ToPid}, Ref, Key}' is sent to all monitors.
  805. %%
  806. %% `monitor(Key, standby)' sets up the monitoring process as a standby for the
  807. %% registered name. If the registered process dies, the first standby process
  808. %% inherits the name, and a message `{gproc, {failover, ToPid}, Ref, Key}' is
  809. %% sent to all monitors, including the one that inherited the name.
  810. %%
  811. %% If the name is not yet registered, the unreg event is sent immediately.
  812. %% If the calling process in this case tried to start a `standby' monitoring,
  813. %% it receives the registered name and the failover event immediately.
  814. %%
  815. %% `monitor(Key, follow)' keeps monitoring the registered name even if it is
  816. %% temporarily unregistered. The messages received are the same as for the other
  817. %% monitor types, but `{gproc, registered, Ref, Key}' is also sent when a new
  818. %% process registers the name.
  819. %% @end
  820. monitor(Key, Type) when Type==info;
  821. Type==follow;
  822. Type==standby ->
  823. ?CATCH_GPROC_ERROR(monitor1(Key, Type), [Key, Type]).
  824. monitor1({T,g,_} = Key, Type) when T==n; T==a ->
  825. ?CHK_DIST,
  826. gproc_dist:monitor(Key, Type);
  827. monitor1({T,l,_} = Key, Type) when T==n; T==a ->
  828. call({monitor, Key, self(), Type}, l);
  829. monitor1(_, _) ->
  830. ?THROW_GPROC_ERROR(badarg).
  831. %% @spec demonitor(key(), reference()) -> ok
  832. %%
  833. %% @doc Remove a monitor on a registered name
  834. %% This function is the reverse of monitor/1. It removes a monitor previously
  835. %% set on a unique name. This function always succeeds given legal input.
  836. %% @end
  837. demonitor(Key, Ref) ->
  838. ?CATCH_GPROC_ERROR(demonitor1(Key, Ref), [Key, Ref]).
  839. demonitor1({T,g,_} = Key, Ref) when T==n; T==a ->
  840. ?CHK_DIST,
  841. gproc_dist:demonitor(Key, Ref);
  842. demonitor1({T,l,_} = Key, Ref) when T==n; T==a ->
  843. call({demonitor, Key, Ref, self()}, l);
  844. demonitor1(_, _) ->
  845. ?THROW_GPROC_ERROR(badarg).
  846. %% @spec reg(Key::key(), Value) -> true
  847. %%
  848. %% @doc Register a name or property for the current process
  849. %%
  850. %%
  851. reg(Key, Value) ->
  852. ?CATCH_GPROC_ERROR(reg1(Key, Value), [Key, Value]).
  853. reg1({_,g,_} = Key, Value) ->
  854. %% anything global
  855. ?CHK_DIST,
  856. gproc_dist:reg(Key, Value);
  857. reg1({p,l,_} = Key, Value) ->
  858. local_reg(Key, Value);
  859. reg1({a,l,_} = Key, undefined) ->
  860. call({reg, Key, undefined});
  861. reg1({c,l,_} = Key, Value) when is_integer(Value) ->
  862. call({reg, Key, Value});
  863. reg1({n,l,_} = Key, Value) ->
  864. call({reg, Key, Value});
  865. reg1(_, _) ->
  866. ?THROW_GPROC_ERROR(badarg).
  867. %% @spec reg_or_locate(Key::key(), Value) -> {pid(), NewValue}
  868. %%
  869. %% @doc Try registering a unique name, or return existing registration.
  870. %%
  871. %% This function tries to register the name `Key', if available.
  872. %% If such a registration object already exists, the pid and value of
  873. %% the current registration is returned instead.
  874. %% @end
  875. reg_or_locate(Key, Value) ->
  876. ?CATCH_GPROC_ERROR(reg_or_locate1(Key, Value, self()), [Key, Value]).
  877. %% @spec reg_or_locate(Key::key(), Value, Fun::fun()) -> {pid(), NewValue}
  878. %%
  879. %% @doc Spawn a process with a registered name, or return existing registration.
  880. %%
  881. %% This function checks whether a local name is registered; if not, it spawns
  882. %% a new process (with `spawn(Fun)') and gives it the name.
  883. %% The pid and value of the resulting registration is returned.
  884. %%
  885. %% When a global name is registered in this fashion, the process is
  886. %% spawned on the caller's node, and the group_leader of the spawned
  887. %% process is set to the group_leader of the calling process.
  888. %% @end
  889. reg_or_locate({n,_,_} = Key, Value, F) when is_function(F, 0) ->
  890. ?CATCH_GPROC_ERROR(reg_or_locate1(Key, Value, F), [Key, Value, F]).
  891. reg_or_locate1({_,g,_} = Key, Value, P) ->
  892. ?CHK_DIST,
  893. gproc_dist:reg_or_locate(Key, Value, P);
  894. reg_or_locate1({n,l,_} = Key, Value, P) ->
  895. call({reg_or_locate, Key, Value, P});
  896. reg_or_locate1(_, _, _) ->
  897. ?THROW_GPROC_ERROR(badarg).
  898. %% @spec reg_shared(Key::key()) -> true
  899. %%
  900. %% @doc Register a resource, but don't tie it to a particular process.
  901. %%
  902. %% `reg_shared({c,l,C}) -> reg_shared({c,l,C}, 0).'
  903. %% `reg_shared({a,l,A}) -> reg_shared({a,l,A}, undefined).'
  904. %% @end
  905. reg_shared(Key) ->
  906. ?CATCH_GPROC_ERROR(reg_shared1(Key), [Key]).
  907. %% @private
  908. reg_shared1({T,_,_} = Key) when T==a; T==p; T==c ->
  909. reg_shared(Key, default(Key)).
  910. %% @spec reg_shared(Key::key(), Value) -> true
  911. %%
  912. %% @doc Register a resource, but don't tie it to a particular process.
  913. %%
  914. %% Shared resources are all unique. They remain until explicitly unregistered
  915. %% (using {@link unreg_shared/1}). The types of shared resources currently
  916. %% supported are `counter' and `aggregated counter'. In listings and query
  917. %% results, shared resources appear as other similar resources, except that
  918. %% `Pid == shared'. To wit, update_counter({c,l,myCounter}, shared, 1) would
  919. %% increment the shared counter `myCounter' with 1, provided it exists.
  920. %%
  921. %% A shared aggregated counter will track updates in exactly the same way as
  922. %% an aggregated counter which is owned by a process.
  923. %% @end
  924. %%
  925. reg_shared(Key, Value) ->
  926. ?CATCH_GPROC_ERROR(reg_shared1(Key, Value), [Key, Value]).
  927. %% @private
  928. reg_shared1({_,g,_} = Key, Value) ->
  929. %% anything global
  930. ?CHK_DIST,
  931. gproc_dist:reg_shared(Key, Value);
  932. reg_shared1({a,l,_} = Key, undefined) ->
  933. call({reg_shared, Key, undefined});
  934. reg_shared1({c,l,_} = Key, Value) when is_integer(Value) ->
  935. call({reg_shared, Key, Value});
  936. reg_shared1({p,l,_} = Key, Value) ->
  937. call({reg_shared, Key, Value});
  938. reg_shared1(_, _) ->
  939. ?THROW_GPROC_ERROR(badarg).
  940. %% @spec mreg(type(), scope(), [{Key::any(), Value::any()}]) -> true
  941. %%
  942. %% @doc Register multiple {Key,Value} pairs of a given type and scope.
  943. %%
  944. %% This function is more efficient than calling {@link reg/2} repeatedly.
  945. %% It is also atomic in regard to unique names; either all names are registered
  946. %% or none are.
  947. %% @end
  948. mreg(T, C, KVL) ->
  949. ?CATCH_GPROC_ERROR(mreg1(T, C, KVL), [T, C, KVL]).
  950. mreg1(T, g, KVL) ->
  951. ?CHK_DIST,
  952. gproc_dist:mreg(T, KVL);
  953. mreg1(T, l, KVL) when T==a; T==n ->
  954. if is_list(KVL) ->
  955. call({mreg, T, l, KVL});
  956. true ->
  957. erlang:error(badarg)
  958. end;
  959. mreg1(p, l, KVL) ->
  960. local_mreg(p, KVL);
  961. mreg1(_, _, _) ->
  962. ?THROW_GPROC_ERROR(badarg).
  963. %% @spec munreg(type(), scope(), [Key::any()]) -> true
  964. %%
  965. %% @doc Unregister multiple Key items of a given type and scope.
  966. %%
  967. %% This function is usually more efficient than calling {@link unreg/1}
  968. %% repeatedly.
  969. %% @end
  970. munreg(T, C, L) ->
  971. ?CATCH_GPROC_ERROR(munreg1(T, C, L), [T, C, L]).
  972. munreg1(T, g, L) ->
  973. ?CHK_DIST,
  974. gproc_dist:munreg(T, existing(T,g,L));
  975. munreg1(T, l, L) when T==a; T==n ->
  976. if is_list(L) ->
  977. call({munreg, T, l, existing(T,l,L)});
  978. true ->
  979. erlang:error(badarg)
  980. end;
  981. munreg1(p, l, L) ->
  982. local_munreg(p, existing(p,l,L));
  983. munreg1(_, _, _) ->
  984. ?THROW_GPROC_ERROR(badarg).
  985. existing(T,Scope,L) ->
  986. Keys = if T==p; T==c ->
  987. [{{T,Scope,K}, self()} || K <- L];
  988. T==a; T==n ->
  989. [{{T,Scope,K}, T} || K <- L]
  990. end,
  991. _ = [case ets:member(?TAB, K) of
  992. false -> erlang:error(badarg);
  993. true -> true
  994. end || K <- Keys],
  995. L.
  996. %% @spec (Key:: key()) -> true
  997. %%
  998. %% @doc Unregister a name or property.
  999. %% @end
  1000. unreg(Key) ->
  1001. ?CATCH_GPROC_ERROR(unreg1(Key), [Key]).
  1002. unreg1(Key) ->
  1003. case Key of
  1004. {_, g, _} ->
  1005. ?CHK_DIST,
  1006. gproc_dist:unreg(Key);
  1007. {T, l, _} when T == n;
  1008. T == a -> call({unreg, Key});
  1009. {_, l, _} ->
  1010. case ets:member(?TAB, {Key,self()}) of
  1011. true ->
  1012. _ = gproc_lib:remove_reg(Key, self(), unreg),
  1013. true;
  1014. false ->
  1015. ?THROW_GPROC_ERROR(badarg)
  1016. end
  1017. end.
  1018. %% @spec (Key::key(), Props::[{atom(), any()}]) -> true
  1019. %%
  1020. %% @doc Add/modify `{Key, Value}' attributes associated with a registration.
  1021. %%
  1022. %% Gproc registration objects can have `{Key, Value}' attributes associated with
  1023. %% them. These are stored in a way that doesn't affect the cost of name lookup.
  1024. %%
  1025. %% Attributs can be retrieved using `gproc:get_attribute/3' or
  1026. %% `gproc:get_attributes/2'.
  1027. %% @end
  1028. set_attributes(Key, Props) ->
  1029. ?CATCH_GPROC_ERROR(set_attributes1(Key, Props), [Key, Props]).
  1030. set_attributes1(Key, Props) ->
  1031. case Key of
  1032. {_, g, _} ->
  1033. ?CHK_DIST,
  1034. gproc_dist:set_attributes(Key, Props);
  1035. {_, l, _} ->
  1036. call({set_attributes, Key, Props})
  1037. end.
  1038. %% @spec (Key:: key()) -> true
  1039. %%
  1040. %% @doc Unregister a shared resource.
  1041. %% @end
  1042. unreg_shared(Key) ->
  1043. ?CATCH_GPROC_ERROR(unreg_shared1(Key), [Key]).
  1044. %% @private
  1045. unreg_shared1(Key) ->
  1046. case Key of
  1047. {_, g, _} ->
  1048. ?CHK_DIST,
  1049. gproc_dist:unreg_shared(Key);
  1050. {T, l, _} when T == c;
  1051. T == a;
  1052. T == p -> call({unreg_shared, Key});
  1053. _ ->
  1054. ?THROW_GPROC_ERROR(badarg)
  1055. end.
  1056. %% @spec (Key::key(), Props::[{K,V}]) -> true
  1057. %% @doc Add/modify `{Key, Value}' attributes associated with a shared registration.
  1058. %%
  1059. %% Gproc registration objects can have `{Key, Value}' attributes associated with
  1060. %% them. These are stored in a way that doesn't affect the cost of name lookup.
  1061. %%
  1062. %% Attributes can be retrieved using `gproc:get_attribute/3' or
  1063. %% `gproc:get_attributes/2'.
  1064. %% @end
  1065. %%
  1066. set_attributes_shared(Key, Attrs) ->
  1067. ?CATCH_GPROC_ERROR(set_attributes_shared1(Key, Attrs), [Key, Attrs]).
  1068. set_attributes_shared1(Key, Attrs) ->
  1069. case Key of
  1070. {_, g, _} ->
  1071. ?CHK_DIST,
  1072. gproc_dist:set_attributes_shared(Key, Attrs);
  1073. {_, l, _} ->
  1074. call({set_attributes_shared, Key, Attrs})
  1075. end.
  1076. %% @spec (key(), pid()) -> yes | no
  1077. %%
  1078. %% @doc Behaviour support callback
  1079. %% @end
  1080. register_name({n,_,_} = Name, Pid) when Pid == self() ->
  1081. try reg(Name), yes
  1082. catch
  1083. error:_ ->
  1084. no
  1085. end.
  1086. %% @equiv unreg/1
  1087. unregister_name(Key) ->
  1088. unreg(Key).
  1089. %% @spec select(Arg) -> [Match] | {[Match], Continuation} | '$end_of_table'
  1090. %% where Arg = Continuation
  1091. %% | sel_pattern()
  1092. %% Match = {Key, Pid, Value}
  1093. %% @doc Perform a select operation on the process registry
  1094. %%
  1095. %% When Arg = Contination, resume a gproc:select/1 operation
  1096. %% (see {@link //stdlib/ets:select/1. ets:select/1}
  1097. %%
  1098. %% When Arg = {@type sel_pattern()}, this function executes a select operation,
  1099. %% emulating ets:select/1
  1100. %%
  1101. %% {@link select/2} offers the opportunity to narrow the search
  1102. %% (by limiting to only global or local scope, or a single type of object).
  1103. %% When only a pattern as single argument is given, both global and local scope,
  1104. %% as well as all types of object can be searched. Note that the pattern may
  1105. %% still limit the select operation so that scanning the entire table is avoided.
  1106. %%
  1107. %% The physical representation in the registry may differ from the above,
  1108. %% but the select patterns are transformed appropriately. The logical
  1109. %% representation for the gproc select operations is given by
  1110. %% {@type headpat()}.
  1111. %% @end
  1112. select({?TAB, _, _, _, _, _, _, _} = Continuation) ->
  1113. ets:select(Continuation);
  1114. select(Pat) ->
  1115. select(all, Pat).
  1116. %% @spec (Context::context(), Pat::sel_pattern()) -> [{Key, Pid, Value}]
  1117. %%
  1118. %% @doc Perform a select operation with limited context on the process registry
  1119. %%
  1120. %% The physical representation in the registry may differ from the above,
  1121. %% but the select patterns are transformed appropriately.
  1122. %%
  1123. %% Note that limiting the context is just a convenience function, allowing you
  1124. %% to write a simpler select pattern and still avoid searching the entire
  1125. %% registry. Whenever variables are used in the head pattern, this will result
  1126. %% in a wider scan, even if the values are restricted through a guard (e.g.
  1127. %% <code>select([{'$1','$2','$3'}, [{'==', {element,1,'$1'}, p}], ...])</code>
  1128. %% will count as a wild pattern on the key and result in a full scan).
  1129. %% In this case, specifying a Context will allow gproc to perform some
  1130. %% variable substitution and ensure that the scan is limited.
  1131. %% @end
  1132. select(Context, Pat) ->
  1133. ets:select(?TAB, pattern(Pat, Context)).
  1134. %% @spec (Context::context(), Pat::sel_patten(), Limit::integer()) ->
  1135. %% {[Match],Continuation} | '$end_of_table'
  1136. %% @doc Like {@link select/2} but returns Limit objects at a time.
  1137. %%
  1138. %% See [http://www.erlang.org/doc/man/ets.html#select-3].
  1139. %% @end
  1140. select(Context, Pat, Limit) ->
  1141. ets:select(?TAB, pattern(Pat, Context), Limit).
  1142. %% @spec (sel_pattern()) -> list(sel_object())
  1143. %% @doc
  1144. %% @equiv select_count(all, Pat)
  1145. %% @end
  1146. select_count(Pat) ->
  1147. select_count(all, Pat).
  1148. %% @spec (Context::context(), Pat::sel_pattern()) -> [{Key, Pid, Value}]
  1149. %%
  1150. %% @doc Perform a select_count operation on the process registry.
  1151. %%
  1152. %% The physical representation in the registry may differ from the above,
  1153. %% but the select patterns are transformed appropriately.
  1154. %% @end
  1155. select_count(Context, Pat) ->
  1156. ets:select_count(?TAB, pattern(Pat, Context)).
  1157. %%% Local properties can be registered in the local process, since
  1158. %%% no other process can interfere.
  1159. %%%
  1160. local_reg(Key, Value) ->
  1161. case gproc_lib:insert_reg(Key, Value, self(), l) of
  1162. false -> ?THROW_GPROC_ERROR(badarg);
  1163. true -> monitor_me()
  1164. end.
  1165. local_mreg(_, []) -> true;
  1166. local_mreg(T, [_|_] = KVL) ->
  1167. case gproc_lib:insert_many(T, l, KVL, self()) of
  1168. false -> ?THROW_GPROC_ERROR(badarg);
  1169. {true,_} -> monitor_me()
  1170. end.
  1171. local_munreg(T, L) when T==p; T==c ->
  1172. _ = [gproc_lib:remove_reg({T,l,K}, self(), unreg) || K <- L],
  1173. true.
  1174. %% @spec (Key :: key(), Value) -> true
  1175. %% @doc Sets the value of the registration given by Key
  1176. %%
  1177. %% Key is assumed to exist and belong to the calling process.
  1178. %% If it doesn't, this function will exit.
  1179. %%
  1180. %% Value can be any term, unless the object is a counter, in which case
  1181. %% it must be an integer.
  1182. %% @end
  1183. %%
  1184. set_value(Key, Value) ->
  1185. ?CATCH_GPROC_ERROR(set_value1(Key, Value), [Key, Value]).
  1186. %% @spec (Key :: key(), Value) -> true
  1187. %% @doc Sets the value of the shared registration given by Key
  1188. %%
  1189. %% Key is assumed to exist as a shared entity.
  1190. %% If it doesn't, this function will exit.
  1191. %%
  1192. %% Value can be any term, unless the object is a counter, in which case
  1193. %% it must be an integer.
  1194. %% @end
  1195. %%
  1196. set_value_shared({T,_,_} = Key, Value) when T == c;
  1197. T == a;
  1198. T == p->
  1199. ?CATCH_GPROC_ERROR(set_value_shared1(Key, Value), [Key, Value]).
  1200. set_value1({_,g,_} = Key, Value) ->
  1201. ?CHK_DIST,
  1202. gproc_dist:set_value(Key, Value);
  1203. set_value1({a,l,_} = Key, Value) when is_integer(Value) ->
  1204. call({set, Key, Value});
  1205. set_value1({n,l,_} = Key, Value) ->
  1206. %% we cannot do this locally, since we have to check that the object
  1207. %% exists first - not an atomic update.
  1208. call({set, Key, Value});
  1209. set_value1({p,l,_} = Key, Value) ->
  1210. %% we _can_ to this locally, since there is no race condition - no
  1211. %% other process can update our properties.
  1212. case gproc_lib:do_set_value(Key, Value, self()) of
  1213. true -> true;
  1214. false ->
  1215. erlang:error(badarg)
  1216. end;
  1217. set_value1({c,l,_} = Key, Value) when is_integer(Value) ->
  1218. gproc_lib:do_set_counter_value(Key, Value, self());
  1219. set_value1(_, _) ->
  1220. ?THROW_GPROC_ERROR(badarg).
  1221. set_value_shared1({_,g,_} = Key, Value) ->
  1222. ?CHK_DIST,
  1223. gproc_dist:set_value_shared(Key, Value);
  1224. set_value_shared1({_,l,_} = Key, Value) ->
  1225. call({set_shared, Key, Value}).
  1226. %% @spec (Key) -> Value
  1227. %% @doc Reads the value stored with a key registered to the current process.
  1228. %%
  1229. %% If no such key is registered to the current process, this function exits.
  1230. %% @end
  1231. get_value(Key) ->
  1232. ?CATCH_GPROC_ERROR(get_value1(Key, self()), [Key]).
  1233. %% @spec (Key) -> Value
  1234. %% @doc Reads the value stored with a shared key.
  1235. %%
  1236. %% If no such shared key is registered, this function exits.
  1237. %% @end
  1238. get_value_shared(Key) ->
  1239. ?CATCH_GPROC_ERROR(get_value1(Key, shared), [Key]).
  1240. %% @spec (Key, Pid) -> Value
  1241. %% @doc Reads the value stored with a key registered to the process Pid.
  1242. %%
  1243. %% If `Pid == shared', the value of a shared key (see {@link reg_shared/1})
  1244. %% will be read.
  1245. %% @end
  1246. %%
  1247. get_value(Key, Pid) ->
  1248. ?CATCH_GPROC_ERROR(get_value1(Key, Pid), [Key, Pid]).
  1249. get_value1({T,_,_} = Key, Pid) when is_pid(Pid) ->
  1250. if T==n orelse T==a ->
  1251. case ets:lookup(?TAB, {Key, T}) of
  1252. [{_, P, Value}] when P == Pid -> Value;
  1253. _ -> ?THROW_GPROC_ERROR(badarg)
  1254. end;
  1255. true ->
  1256. ets:lookup_element(?TAB, {Key, Pid}, 3)
  1257. end;
  1258. get_value1({T,_,_} = K, shared) when T==c; T==a; T==p ->
  1259. Key = case T of
  1260. c -> {K, shared};
  1261. p -> {K, shared};
  1262. a -> {K, a}
  1263. end,
  1264. case ets:lookup(?TAB, Key) of
  1265. [{_, shared, Value}] -> Value;
  1266. _ -> ?THROW_GPROC_ERROR(badarg)
  1267. end;
  1268. get_value1(_, _) ->
  1269. ?THROW_GPROC_ERROR(badarg).
  1270. %% @spec (Key, Attribute::atom()) -> Value
  1271. %% @doc Get attribute value of `Attr' associated with `Key' for most likely Pid.
  1272. %%
  1273. %% The most likely Pid in this case is `self()' for properties and counters,
  1274. %% and the current registration holder in case of names or aggregated counters.
  1275. %% An exception is raised if `Key' is not registered for the given process.
  1276. %% @end
  1277. get_attribute(Key, A) ->
  1278. Pid = case Key of
  1279. {T,_,_} when T==n; T==a ->
  1280. where(Key);
  1281. {T,_,_} when T==p; T==c ->
  1282. self()
  1283. end,
  1284. ?CATCH_GPROC_ERROR(get_attribute1(Key, Pid, A), [Key, A]).
  1285. %% @spec (Key, Pid::pid() | shared, Attr::atom()) -> Value
  1286. %% @doc Get the attribute value of `Attr' associated with `Key' for process Pid.
  1287. %%
  1288. %% If `Pid == shared', the attribute of a shared key (see {@link reg_shared/1})
  1289. %% will be read.
  1290. %% @end
  1291. %%
  1292. get_attribute(Key, Pid, A) ->
  1293. ?CATCH_GPROC_ERROR(get_attribute1(Key, Pid, A), [Key, Pid, A]).
  1294. %% @spec (Key, Attr::atom()) -> Value
  1295. %% @doc Get the attribute value of `Attr' associated with the shared `Key'.
  1296. %%
  1297. %% Equivalent to `get_attribute(Key, shared, Attr)'
  1298. %% (see {@link get_attribute/3}).
  1299. %% @end
  1300. get_attribute_shared(Key, Attr) ->
  1301. ?CATCH_GPROC_ERROR(get_attribute1(Key, shared, Attr), [Key, Attr]).
  1302. %% @private
  1303. get_attribute1({_,_,_} = Key, Pid, A) when is_pid(Pid); Pid==shared ->
  1304. case ets:lookup(?TAB, {Pid, Key}) of
  1305. [{_, Attrs}] ->
  1306. case lists:keyfind(attrs, 1, Attrs) of
  1307. false -> undefined;
  1308. {_, As} ->
  1309. case lists:keyfind(A, 1, As) of
  1310. false -> undefined;
  1311. {_, V} -> V
  1312. end
  1313. end;
  1314. _ -> ?THROW_GPROC_ERROR(badarg)
  1315. end;
  1316. get_attribute1(_, _, _) ->
  1317. ?THROW_GPROC_ERROR(badarg).
  1318. %% @spec get_attributes(Key::key()) -> [{K, V}]
  1319. %% @doc Get attributes associated with registration.
  1320. %% @equiv get_attributes(Key, self())
  1321. %%
  1322. get_attributes(Key) ->
  1323. ?CATCH_GPROC_ERROR(get_attributes1(Key, self()), [Key]).
  1324. %% @spec (Key::key(), Pid::pid() | shared) -> [{K, V}]
  1325. %%
  1326. %% @doc Returns the list of attributes associated with the registration.
  1327. %%
  1328. %% This function raises a `badarg' exception if there is no corresponding
  1329. %% registration.
  1330. %%
  1331. get_attributes(Key, Pid) ->
  1332. ?CATCH_GPROC_ERROR(get_attributes1(Key, Pid), [Key, Pid]).
  1333. get_attributes1({_,_,_} = Key, Pid) when is_pid(Pid); Pid==shared ->
  1334. case ets:lookup(?TAB, {Pid, Key}) of
  1335. [{_, Attrs}] ->
  1336. case lists:keyfind(attrs, 1, Attrs) of
  1337. false -> [];
  1338. {_, As} -> As
  1339. end;
  1340. _ -> ?THROW_GPROC_ERROR(badarg)
  1341. end;
  1342. get_attributes1(_, _) ->
  1343. ?THROW_GPROC_ERROR(badarg).
  1344. %% @spec (Key) -> Pid
  1345. %% @doc Lookup the Pid stored with a key.
  1346. %%
  1347. %% This function raises a `badarg' exception if `Key' is not registered.
  1348. %% @end
  1349. lookup_pid({_T,_,_} = Key) ->
  1350. case where(Key) of
  1351. undefined -> erlang:error(badarg);
  1352. P -> P
  1353. end.
  1354. %% @spec (Key) -> Value
  1355. %% @doc Lookup the value stored with a key.
  1356. %%
  1357. %% This function raises a `badarg' exception if `Key' is not registered.
  1358. %% @end
  1359. lookup_value({T,_,_} = Key) ->
  1360. if T==n orelse T==a ->
  1361. ets:lookup_element(?TAB, {Key,T}, 3);
  1362. true ->
  1363. erlang:error(badarg)
  1364. end.
  1365. %% @spec (Key::key()) -> pid() | undefined
  1366. %%
  1367. %% @doc Returns the pid registered as Key
  1368. %%
  1369. %% The type of registration must be either name or aggregated counter.
  1370. %% Otherwise this function will raise a `badarg' exception.
  1371. %% Use {@link lookup_pids/1} in these cases.
  1372. %% @end
  1373. %%
  1374. where(Key) ->
  1375. ?CATCH_GPROC_ERROR(where1(Key), [Key]).
  1376. where1({T,_,_}=Key) ->
  1377. if T==n orelse T==a ->
  1378. case ets:lookup(?TAB, {Key,T}) of
  1379. [{_, P, _Value}] ->
  1380. case my_is_process_alive(P) of
  1381. true -> P;
  1382. false ->
  1383. undefined
  1384. end;
  1385. _ -> % may be [] or [{Key,Waiters}]
  1386. undefined
  1387. end;
  1388. true ->
  1389. ?THROW_GPROC_ERROR(badarg)
  1390. end.
  1391. %% @equiv where/1
  1392. whereis_name(Key) ->
  1393. ?CATCH_GPROC_ERROR(where1(Key), [Key]).
  1394. %% @spec (Key::key()) -> [pid()]
  1395. %%
  1396. %% @doc Returns a list of pids with the published key Key
  1397. %%
  1398. %% If the type of registration is either name or aggregated counter,
  1399. %% this function will return either an empty list, or a list of one pid.
  1400. %% For non-unique types, the return value can be a list of any length.
  1401. %%
  1402. %% Note: shared resources are not associated with any pid, and will
  1403. %% therefore be excluded.
  1404. %% @end
  1405. %%
  1406. lookup_pids({T,_,_} = Key) ->
  1407. L = if T==n orelse T==a ->
  1408. ets:select(?TAB, [{{{Key,T}, '$1', '_'},
  1409. [{is_pid, '$1'}], ['$1']}]);
  1410. true ->
  1411. ets:select(?TAB, [{{{Key,'_'}, '$1', '_'},
  1412. [{is_pid, '$1'}], ['$1']}])
  1413. end,
  1414. [P || P <- L, my_is_process_alive(P)].
  1415. %% @spec (pid()) -> boolean()
  1416. %%
  1417. my_is_process_alive(P) when node(P) =:= node() ->
  1418. is_process_alive(P);
  1419. my_is_process_alive(_) ->
  1420. %% remote pid - assume true (too costly to find out)
  1421. true.
  1422. %% @spec (Key::key()) -> [{pid(), Value}]
  1423. %%
  1424. %% @doc Retrieve the `{Pid,Value}' pairs corresponding to Key.
  1425. %%
  1426. %% Key refer to any type of registry object. If it refers to a unique
  1427. %% object, the list will be of length 0 or 1. If it refers to a non-unique
  1428. %% object, the return value can be a list of any length.
  1429. %% @end
  1430. %%
  1431. lookup_values({T,_,_} = Key) ->
  1432. L = if T==n orelse T==a ->
  1433. ets:select(?TAB, [{{{Key,T}, '$1', '$2'},[],[{{'$1','$2'}}]}]);
  1434. true ->
  1435. ets:select(?TAB, [{{{Key,'_'}, '$1', '$2'},[],[{{'$1','$2'}}]}])
  1436. end,
  1437. [Pair || {P,_} = Pair <- L, my_is_process_alive(P)].
  1438. %% @ spec (Key::key(), Incr) -> integer() | [integer()]
  1439. %% Incr = IncrVal | UpdateOp | [UpdateOp]
  1440. %% UpdateOp = IncrVal | {IncrVal, Threshold, SetValue}
  1441. %% IncrVal = integer()
  1442. %%
  1443. %% @doc Updates the counter registered as Key for the current process.
  1444. %%
  1445. %% This function works almost exactly like ets:update_counter/3
  1446. %% (see [http://www.erlang.org/doc/man/ets.html#update_counter-3]), but
  1447. %% will fail if the type of object referred to by Key is not a counter or
  1448. %% a unique name (update_counter/2 can be performed on names as well, but they
  1449. %% do not count as counter objects, and do not affect aggregated counters).
  1450. %%
  1451. %% Aggregated counters with the same name will be updated automatically.
  1452. %% The `UpdateOp' patterns are the same as for `ets:update_counter/3', except
  1453. %% that the position is omitted; in gproc, the value position is always `3'.
  1454. %%
  1455. %% If `Key' refers to a unique name, the operation will depend on the value
  1456. %% part of the registration being an integer(). While non-integer values are
  1457. %% not permitted at all for counter objects, it is the user's responsibility to
  1458. %% ensure that a name, on which `update_counter/2' is to be performed, has the
  1459. %% appropriate value type.
  1460. %% @end
  1461. %%
  1462. -spec update_counter(key(), increment()) -> integer().
  1463. update_counter(Key, Incr) ->
  1464. Pid = case Key of
  1465. {n,_,_} -> n;
  1466. {c,_,_} -> self()
  1467. end,
  1468. ?CATCH_GPROC_ERROR(update_counter1(Key, Pid, Incr), [Key, Incr]).
  1469. update_counter(Key, Pid, Incr) when is_pid(Pid);
  1470. Pid == shared; Pid == n ->
  1471. ?CATCH_GPROC_ERROR(update_counter1(Key, Pid, Incr), [Key, Pid, Incr]).
  1472. update_counter1({T,l,_} = Key, Pid, Incr) when T==c; T==n ->
  1473. gproc_lib:update_counter(Key, Incr, Pid);
  1474. update_counter1({T,g,_} = Key, Pid, Incr) when T==c; T==n ->
  1475. ?CHK_DIST,
  1476. gproc_dist:update_counter(Key, Pid, Incr);
  1477. update_counter1(_, _, _) ->
  1478. ?THROW_GPROC_ERROR(badarg).
  1479. %% @doc Update a list of counters
  1480. %%
  1481. %% This function is not atomic, except (in a sense) for global counters. For local counters,
  1482. %% it is more of a convenience function. For global counters, it is much more efficient
  1483. %% than calling `gproc:update_counter/2' for each individual counter.
  1484. %%
  1485. %% The return value is the corresponding list of `[{Counter, Pid, NewValue}]'.
  1486. %% @end
  1487. -spec update_counters(scope(), [{key(), pid(), increment()}]) ->
  1488. [{key(), pid(), integer()}].
  1489. update_counters(_, []) ->
  1490. [];
  1491. update_counters(l, [_|_] = Cs) ->
  1492. ?CATCH_GPROC_ERROR(update_counters1(Cs), [Cs]);
  1493. update_counters(g, [_|_] = Cs) ->
  1494. ?CHK_DIST,
  1495. gproc_dist:update_counters(Cs).
  1496. update_counters1([{{c,l,_} = Key, Pid, Incr}|T]) ->
  1497. [{Key, Pid, gproc_lib:update_counter(Key, Incr, Pid)}|update_counters1(T)];
  1498. update_counters1([]) ->
  1499. [];
  1500. update_counters1(_) ->
  1501. ?THROW_GPROC_ERROR(badarg).
  1502. %% @spec (Key) -> {ValueBefore, ValueAfter}
  1503. %% Key = {c, Scope, Name}
  1504. %% Scope = l | g
  1505. %% ValueBefore = integer()
  1506. %% ValueAfter = integer()
  1507. %%
  1508. %% @doc Reads and resets a counter in a "thread-safe" way
  1509. %%
  1510. %% This function reads the current value of a counter and then resets it to its
  1511. %% initial value. The reset operation is done using {@link update_counter/2},
  1512. %% which allows for concurrent calls to {@link update_counter/2} without losing
  1513. %% updates. Aggregated counters are updated accordingly.
  1514. %% @end
  1515. %%
  1516. reset_counter(Key) ->
  1517. ?CATCH_GPROC_ERROR(reset_counter1(Key), [Key]).
  1518. reset_counter1({c,g,_} = Key) ->
  1519. ?CHK_DIST,
  1520. gproc_dist:reset_counter(Key);
  1521. reset_counter1({c,l,_} = Key) ->
  1522. Current = ets:lookup_element(?TAB, {Key, self()}, 3),
  1523. Initial = case ets:lookup(?TAB, {self(), Key}) of
  1524. [{_, r}] -> 0;
  1525. [{_, Opts}] ->
  1526. proplists:get_value(initial, Opts, 0)
  1527. end,
  1528. {Current, update_counter(Key, Initial - Current)}.
  1529. %% @spec (Key::key(), Incr) -> integer() | [integer()]
  1530. %% Incr = IncrVal | UpdateOp | [UpdateOp]
  1531. %% UpdateOp = IncrVal | {IncrVal, Threshold, SetValue}
  1532. %% IncrVal = integer()
  1533. %%
  1534. %% @doc Updates the shared counter registered as Key.
  1535. %%
  1536. %% This function works almost exactly like ets:update_counter/3
  1537. %% (see [http://www.erlang.org/doc/man/ets.html#update_counter-3]), but
  1538. %% will fail if the type of object referred to by Key is not a counter.
  1539. %%
  1540. %% Aggregated counters with the same name will be updated automatically.
  1541. %% The `UpdateOp' patterns are the same as for `ets:update_counter/3', except
  1542. %% that the position is omitted; in gproc, the value position is always `3'.
  1543. %% @end
  1544. %%
  1545. update_shared_counter(Key, Incr) ->
  1546. ?CATCH_GPROC_ERROR(update_shared_counter1(Key, Incr), [Key, Incr]).
  1547. update_shared_counter1({c,g,_} = Key, Incr) ->
  1548. ?CHK_DIST,
  1549. gproc_dist:update_shared_counter(Key, Incr);
  1550. update_shared_counter1({c,l,_} = Key, Incr) ->
  1551. gproc_lib:update_counter(Key, Incr, shared).
  1552. %% @spec (From::key(), To::pid() | key()) -> undefined | pid()
  1553. %%
  1554. %% @doc Atomically transfers the key `From' to the process identified by `To'.
  1555. %%
  1556. %% This function transfers any gproc key (name, property, counter, aggr counter)
  1557. %% from one process to another, and returns the pid of the new owner.
  1558. %%
  1559. %% `To' must be either a pid or a unique name (name or aggregated counter), but
  1560. %% does not necessarily have to resolve to an existing process. If there is
  1561. %% no process registered with the `To' key, `give_away/2' returns `undefined',
  1562. %% and the `From' key is effectively unregistered.
  1563. %%
  1564. %% It is allowed to give away a key to oneself, but of course, this operation
  1565. %% will have no effect.
  1566. %%
  1567. %% Fails with `badarg' if the calling process does not have a `From' key
  1568. %% registered.
  1569. %% @end
  1570. give_away(Key, ToPid) ->
  1571. ?CATCH_GPROC_ERROR(give_away1(Key, ToPid), [Key, ToPid]).
  1572. give_away1({_,l,_} = Key, ToPid) when is_pid(ToPid), node(ToPid) == node() ->
  1573. call({give_away, Key, ToPid});
  1574. give_away1({_,l,_} = Key, {n,l,_} = ToKey) ->
  1575. call({give_away, Key, ToKey});
  1576. give_away1({_,g,_} = Key, To) ->
  1577. ?CHK_DIST,
  1578. gproc_dist:give_away(Key, To).
  1579. %% @spec () -> ok
  1580. %%
  1581. %% @doc Unregister all items of the calling process and inform gproc
  1582. %% to forget about the calling process.
  1583. %%
  1584. %% This function is more efficient than letting gproc perform these
  1585. %% cleanup operations.
  1586. %% @end
  1587. goodbye() ->
  1588. process_is_down(self()).
  1589. %% @spec (Key::process() | key(), Msg::any()) -> Msg
  1590. %%
  1591. %% @doc Sends a message to the process, or processes, corresponding to Key.
  1592. %%
  1593. %% If Key belongs to a unique object (name or aggregated counter), this
  1594. %% function will send a message to the corresponding process, or fail if there
  1595. %% is no such process. If Key is for a non-unique object type (counter or
  1596. %% property), Msg will be send to all processes that have such an object.
  1597. %%
  1598. %% Key can also be anything that the erlang:send/2, or '!' operator accepts as a process
  1599. %% identifier, namely a pid(), an atom(), or `{Name::atom(), Node::atom()}'.
  1600. %% @end
  1601. %%
  1602. send(P, Msg) when is_pid(P); is_atom(P) ->
  1603. P ! Msg;
  1604. send({Name, Node} = P, Msg) when is_atom(Node), is_atom(Name) ->
  1605. P ! Msg;
  1606. send(Key, Msg) ->
  1607. ?CATCH_GPROC_ERROR(send1(Key, Msg), [Key, Msg]).
  1608. send1({T,C,_} = Key, Msg) when C==l; C==g ->
  1609. if T == n orelse T == a ->
  1610. case ets:lookup(?TAB, {Key, T}) of
  1611. [{_, Pid, _}] ->
  1612. Pid ! Msg;
  1613. _ ->
  1614. ?THROW_GPROC_ERROR(badarg)
  1615. end;
  1616. T==p orelse T==c ->
  1617. %% BUG - if the key part contains select wildcards, we may end up
  1618. %% sending multiple messages to the same pid
  1619. lists:foreach(fun(Pid) ->
  1620. Pid ! Msg
  1621. end, lookup_pids(Key)),
  1622. Msg;
  1623. true ->
  1624. erlang:error(badarg)
  1625. end;
  1626. send1(_, _) ->
  1627. ?THROW_GPROC_ERROR(badarg).
  1628. %% @spec (Key::key(), Msg::any()) -> Msg
  1629. %%
  1630. %% @equiv bcast(nodes(), Key, Msg)
  1631. %% @end
  1632. %%
  1633. bcast(Key, Msg) ->
  1634. bcast(nodes(), Key, Msg).
  1635. %% @spec (Nodes::[atom()], Key::key(), Msg::any()) -> Msg
  1636. %%
  1637. %% @doc Sends a message to processes corresponding to Key on Nodes.
  1638. %%
  1639. %% This function complements `send/2' and works on locally registered resources
  1640. %% that `send/2' supports. Messages are routed via a special broadcast server
  1641. %% on each node to ensure that ordering is preserved. Distributed delivery
  1642. %% is asynchronous and carries the same guarantees as normal message passing
  1643. %% (with the added proviso that the broadcast server also needs to be available).
  1644. %% @see send/2
  1645. %% @end
  1646. %%
  1647. bcast(Ns, Key, Msg) ->
  1648. ?CATCH_GPROC_ERROR(bcast1(Ns, Key, Msg), [Key, Msg]).
  1649. bcast1(Ns, {T,l,_} = Key, Msg) when T==p; T==a; T==c; T==n; T==p ->
  1650. send1(Key, Msg),
  1651. gen_server:abcast(Ns -- [node()], gproc_bcast, {send, Key, Msg}),
  1652. Msg.
  1653. %% @spec (Context :: context()) -> key() | '$end_of_table'
  1654. %%
  1655. %% @doc Behaves as ets:first(Tab) for a given type of registration.
  1656. %%
  1657. %% See [http://www.erlang.org/doc/man/ets.html#first-1].
  1658. %% The registry behaves as an ordered_set table.
  1659. %% @end
  1660. %%
  1661. first(Context) ->
  1662. {S, T} = get_s_t(Context),
  1663. {HeadPat,_} = headpat({S, T}, '_', '_', '_'),
  1664. case ets:select(?TAB, [{HeadPat,[],[{element,1,'$_'}]}], 1) of
  1665. {[First], _} ->
  1666. First;
  1667. _ ->
  1668. '$end_of_table'
  1669. end.
  1670. %% @spec (Context :: context()) -> key() | '$end_of_table'
  1671. %%
  1672. %% @doc Behaves as ets:last(Tab) for a given type of registration.
  1673. %%
  1674. %% See [http://www.erlang.org/doc/man/ets.html#last-1].
  1675. %% The registry behaves as an ordered_set table.
  1676. %% @end
  1677. %%
  1678. last(Context) ->
  1679. {S, T} = get_s_t(Context),
  1680. S1 = if S == '_'; S == l -> m; % 'm' comes after 'l'
  1681. S == g -> h % 'h' comes between 'g' & 'l'
  1682. end,
  1683. Beyond = {{T,S1,[]},[]},
  1684. step(ets:prev(?TAB, Beyond), S, T).
  1685. %% @spec (Context::context(), Key::key()) -> key() | '$end_of_table'
  1686. %%
  1687. %% @doc Behaves as ets:next(Tab,Key) for a given type of registration.
  1688. %%
  1689. %% See [http://www.erlang.org/doc/man/ets.html#next-2].
  1690. %% The registry behaves as an ordered_set table.
  1691. %% @end
  1692. %%
  1693. next(Context, K) ->
  1694. {S,T} = get_s_t(Context),
  1695. {Prev,Unwrap} =
  1696. case K of
  1697. {{_,_,_},_} ->
  1698. {K, false};
  1699. {_,_,_} ->
  1700. {{K,[]}, true} % [] is higher than pid(), shared, p, c...
  1701. end,
  1702. unwrap(Unwrap, step(ets:next(?TAB,Prev), S, T)).
  1703. unwrap(true, {{_,_,_} = R,_}) ->
  1704. R;
  1705. unwrap(_, R) ->
  1706. R.
  1707. %% @spec (Context::context(), Key::key()) -> key() | '$end_of_table'
  1708. %%
  1709. %% @doc Behaves as ets:prev(Tab,Key) for a given type of registration.
  1710. %%
  1711. %% See [http://www.erlang.org/doc/man/ets.html#prev-2].
  1712. %% The registry behaves as an ordered_set table.
  1713. %% @end
  1714. %%
  1715. prev(Context, K) ->
  1716. {S, T} = get_s_t(Context),
  1717. {Prev, Unwrap} =
  1718. case K of
  1719. {{_,_,_},_} -> {K, false};
  1720. {_,_,_} ->
  1721. {{K,1}, true}
  1722. end,
  1723. unwrap(Unwrap, step(ets:prev(?TAB, Prev), S, T)).
  1724. step(Key, '_', '_') ->
  1725. case Key of
  1726. {{_,_,_},_} -> Key;
  1727. _ -> '$end_of_table'
  1728. end;
  1729. step(Key, '_', T) ->
  1730. case Key of
  1731. {{T,_,_},_} -> Key;
  1732. _ -> '$end_of_table'
  1733. end;
  1734. step(Key, S, '_') ->
  1735. case Key of
  1736. {{_, S, _}, _} -> Key;
  1737. _ -> '$end_of_table'
  1738. end;
  1739. step(Key, S, T) ->
  1740. case Key of
  1741. {{T, S, _}, _} -> Key;
  1742. _ -> '$end_of_table'
  1743. end.
  1744. %% @spec (Pid::pid()) -> ProcessInfo
  1745. %% ProcessInfo = [{gproc, [{Key,Value}]} | ProcessInfo]
  1746. %%
  1747. %% @doc Similar to `process_info(Pid)' but with additional gproc info.
  1748. %%
  1749. %% Returns the same information as process_info(Pid), but with the
  1750. %% addition of a `gproc' information item, containing the `{Key,Value}'
  1751. %% pairs registered to the process.
  1752. %% @end
  1753. info(Pid) when is_pid(Pid) ->
  1754. Items = [?MODULE | [ I || {I,_} <- process_info(self())]],
  1755. [info(Pid,I) || I <- Items].
  1756. %% @spec (Pid::pid(), Item::atom()) -> {Item, Info}
  1757. %%
  1758. %% @doc Similar to process_info(Pid, Item), but with additional gproc info.
  1759. %%
  1760. %% For `Item = gproc', this function returns a list of `{Key, Value}' pairs
  1761. %% registered to the process Pid. For other values of Item, it returns the
  1762. %% same as [http://www.erlang.org/doc/man/erlang.html#process_info-2].
  1763. %% @end
  1764. info(Pid, gproc) ->
  1765. gproc_info(Pid, '_');
  1766. info(Pid, {gproc, Pat}) ->
  1767. gproc_info(Pid, Pat);
  1768. info(Pid, current_function) ->
  1769. {_, T} = process_info(Pid, backtrace),
  1770. info_cur_f(T, process_info(Pid, current_function));
  1771. info(Pid, I) ->
  1772. process_info(Pid, I).
  1773. %% We don't want to return the internal gproc:info() function as current
  1774. %% function, so we grab the 'backtrace' and extract the call stack from it,
  1775. %% filtering out the functions gproc:info/_ and gproc:'-info/1-lc...' entries.
  1776. %%
  1777. %% This is really an indication that wrapping the process_info() BIF was a
  1778. %% bad idea to begin with... :P
  1779. %%
  1780. info_cur_f(T, Default) ->
  1781. {match, Matches} = re:run(T,<<"\\(([^\\)]+):(.+)/([0-9]+)">>,
  1782. [global,{capture,[1,2,3],list}]),
  1783. case lists:dropwhile(fun(["gproc","info",_]) -> true;
  1784. (["gproc","'-info/1-lc" ++ _, _]) -> true;
  1785. (_) -> false
  1786. end, Matches) of
  1787. [] ->
  1788. Default;
  1789. [[M,F,A]|_] ->
  1790. {current_function,
  1791. {to_atom(M), to_atom(F), list_to_integer(A)}}
  1792. end.
  1793. to_atom(S) ->
  1794. case erl_scan:string(S) of
  1795. {ok, [{atom,_,A}|_],_} ->
  1796. A;
  1797. _ ->
  1798. list_to_atom(S)
  1799. end.
  1800. gproc_info(Pid, Pat) ->
  1801. Keys = ets:select(?TAB, [{ {{Pid,Pat}, '_'}, [], [{element,2,
  1802. {element,1,'$_'}}] }]),
  1803. {?MODULE, lists:zf(
  1804. fun(K) ->
  1805. try V = get_value(K, Pid),
  1806. {true, {K,V}}
  1807. catch
  1808. error:_ ->
  1809. false
  1810. end
  1811. end, Keys)}.
  1812. %% @spec () -> ok
  1813. %%
  1814. %% @doc Similar to the built-in shell command `i()' but inserts information
  1815. %% about names and properties registered in Gproc, where applicable.
  1816. %% @end
  1817. i() ->
  1818. gproc_info:i().
  1819. %%% ==========================================================
  1820. %% @hidden
  1821. handle_cast({monitor_me, Pid}, S) ->
  1822. erlang:monitor(process, Pid),
  1823. {noreply, S};
  1824. handle_cast({audit_process, Pid}, S) ->
  1825. case is_process_alive(Pid) of
  1826. false ->
  1827. process_is_down(Pid);
  1828. true ->
  1829. ignore
  1830. end,
  1831. {noreply, S};
  1832. handle_cast({cancel_wait, Pid, {T,_,_} = Key, Ref}, S) ->
  1833. _ = case ets:lookup(?TAB, {Key,T}) of
  1834. [{_, Waiters}] ->
  1835. gproc_lib:remove_wait(Key, Pid, Ref, Waiters);
  1836. _ ->
  1837. ignore
  1838. end,
  1839. {noreply, S};
  1840. handle_cast({cancel_wait_or_monitor, Pid, {T,_,_} = Key}, S) ->
  1841. _ = case ets:lookup(?TAB, {Key, T}) of
  1842. [{_, Waiters}] ->
  1843. gproc_lib:remove_wait(Key, Pid, all, Waiters);
  1844. [{_, OtherPid, _}] ->
  1845. gproc_lib:remove_monitors(Key, OtherPid, Pid);
  1846. _ ->
  1847. ok
  1848. end,
  1849. {noreply, S}.
  1850. %% @hidden
  1851. handle_call({reg, {_T,l,_} = Key, Val}, {Pid,_}, S) ->
  1852. case try_insert_reg(Key, Val, Pid) of
  1853. true ->
  1854. _ = gproc_lib:ensure_monitor(Pid,l),
  1855. {reply, true, S};
  1856. false ->
  1857. {reply, badarg, S}
  1858. end;
  1859. handle_call({set_attributes, {_,l,_} = Key, Attrs}, {Pid,_}, S) ->
  1860. case gproc_lib:insert_attr(Key, Attrs, Pid, l) of
  1861. false -> {reply, badarg, S};
  1862. L when is_list(L) ->
  1863. {reply, true, S}
  1864. end;
  1865. handle_call({set_attributes_shared, {_,l,_} = Key, Attrs}, _, S) ->
  1866. case gproc_lib:insert_attr(Key, Attrs, shared, l) of
  1867. false ->
  1868. {reply, badarg, S};
  1869. L when is_list(L) ->
  1870. {reply, true, S}
  1871. end;
  1872. handle_call({reg_or_locate, {T,l,_} = Key, Val, P}, _, S) ->
  1873. Reg = fun() ->
  1874. Pid = if is_function(P, 0) ->
  1875. spawn(P);
  1876. is_pid(P) ->
  1877. P
  1878. end,
  1879. true = gproc_lib:insert_reg(Key, Val, Pid, l),
  1880. _ = gproc_lib:ensure_monitor(Pid, l),
  1881. {reply, {Pid, Val}, S}
  1882. end,
  1883. case ets:lookup(?TAB, {Key, T}) of
  1884. [] ->
  1885. Reg();
  1886. [{_, _Waiters}] ->
  1887. Reg();
  1888. [{_, OtherPid, OtherValue}] ->
  1889. {reply, {OtherPid, OtherValue}, S}
  1890. end;
  1891. handle_call({monitor, {T,l,_} = Key, Pid, Type}, _From, S)
  1892. when T==n; T==a ->
  1893. Ref = make_ref(),
  1894. Lookup = ets:lookup(?TAB, {Key, T}),
  1895. IsRegged = is_regged(Lookup),
  1896. _ = case {IsRegged, Type} of
  1897. {false, info} ->
  1898. Pid ! {gproc, unreg, Ref, Key};
  1899. {false, follow} ->
  1900. Pid ! {gproc, unreg, Ref, Key},
  1901. _ = gproc_lib:ensure_monitor(Pid, l),
  1902. case Lookup of
  1903. [{K, Waiters}] ->
  1904. NewWaiters = [{Pid,Ref,follow}|Waiters],
  1905. ets:insert(?TAB, {K, NewWaiters}),
  1906. ets:insert_new(?TAB, {{Pid,Key}, []});
  1907. [] ->
  1908. ets:insert(?TAB, {{Key,T}, [{Pid,Ref,follow}]}),
  1909. ets:insert_new(?TAB, {{Pid,Key}, []})
  1910. end;
  1911. {false, standby} ->
  1912. Evt = {failover, Pid},
  1913. true = gproc_lib:insert_reg(Key, undefined, Pid, l, Evt),
  1914. Pid ! {gproc, Evt, Ref, Key},
  1915. _ = gproc_lib:ensure_monitor(Pid, l);
  1916. {true, _} ->
  1917. [{_, RegPid, _}] = Lookup,
  1918. _ = gproc_lib:ensure_monitor(Pid, l),
  1919. case ets:lookup(?TAB, {RegPid, Key}) of
  1920. [{K,r}] ->
  1921. ets:insert(?TAB, {K, [{monitor, [{Pid,Ref,Type}]}]}),
  1922. ets:insert_new(?TAB, {{Pid,Key}, []});
  1923. [{K, Opts}] ->
  1924. ets:insert(?TAB, {K, gproc_lib:add_monitor(
  1925. Opts, Pid, Ref, Type)}),
  1926. ets:insert_new(?TAB, {{Pid,Key}, []})
  1927. end
  1928. end,
  1929. {reply, Ref, S};
  1930. handle_call({demonitor, {T,l,_} = Key, Ref, Pid}, _From, S)
  1931. when T==n; T==a ->
  1932. _ = case where(Key) of
  1933. undefined ->
  1934. ok; % be nice
  1935. RegPid ->
  1936. case ets:lookup(?TAB, {RegPid, Key}) of
  1937. [{_K,r}] ->
  1938. ok; % be nice
  1939. [{K, Opts}] ->
  1940. ets:insert(?TAB, {K, gproc_lib:remove_monitor(
  1941. Opts, Pid, Ref)})
  1942. end
  1943. end,
  1944. {reply, ok, S};
  1945. handle_call({reg_shared, {_T,l,_} = Key, Val}, _From, S) ->
  1946. case try_insert_reg(Key, Val, shared) of
  1947. %% case try_insert_shared(Key, Val) of
  1948. true ->
  1949. {reply, true, S};
  1950. false ->
  1951. {reply, badarg, S}
  1952. end;
  1953. handle_call({unreg, {_,l,_} = Key}, {Pid,_}, S) ->
  1954. case ets:lookup(?TAB, {Pid,Key}) of
  1955. [{_, r}] ->
  1956. _ = gproc_lib:remove_reg(Key, Pid, unreg, []),
  1957. {reply, true, S};
  1958. [{_, Opts}] when is_list(Opts) ->
  1959. _ = gproc_lib:remove_reg(Key, Pid, unreg, Opts),
  1960. {reply, true, S};
  1961. [] ->
  1962. {reply, badarg, S}
  1963. end;
  1964. handle_call({unreg_shared, {_,l,_} = Key}, _, S) ->
  1965. _ = case ets:lookup(?TAB, {shared, Key}) of
  1966. [{_, r}] ->
  1967. _ = gproc_lib:remove_reg(Key, shared, unreg, []);
  1968. [{_, Opts}] ->
  1969. _ = gproc_lib:remove_reg(Key, shared, unreg, Opts);
  1970. [] ->
  1971. %% don't crash if shared key already unregged.
  1972. ok
  1973. end,
  1974. {reply, true, S};
  1975. handle_call({await, {_,l,_} = Key, Pid}, From, S) ->
  1976. %% Passing the pid explicitly is needed when leader_call is used,
  1977. %% since the Pid given as From in the leader is the local gen_leader
  1978. %% instance on the calling node.
  1979. case gproc_lib:await(Key, Pid, From) of
  1980. noreply ->
  1981. {noreply, S};
  1982. {reply, Reply, _} ->
  1983. {reply, Reply, S}
  1984. end;
  1985. handle_call({mreg, T, l, L}, {Pid,_}, S) ->
  1986. try gproc_lib:insert_many(T, l, L, Pid) of
  1987. {true,_} -> {reply, true, S};
  1988. false -> {reply, badarg, S}
  1989. catch
  1990. error:_ -> {reply, badarg, S}
  1991. end;
  1992. handle_call({munreg, T, l, L}, {Pid,_}, S) ->
  1993. _ = gproc_lib:remove_many(T, l, L, Pid),
  1994. {reply, true, S};
  1995. handle_call({set, {_,l,_} = Key, Value}, {Pid,_}, S) ->
  1996. case gproc_lib:do_set_value(Key, Value, Pid) of
  1997. true ->
  1998. {reply, true, S};
  1999. false ->
  2000. {reply, badarg, S}
  2001. end;
  2002. handle_call({set_shared, {_,l,_} = Key, Value}, {_,_}, S) ->
  2003. case gproc_lib:do_set_value(Key, Value, shared) of
  2004. true ->
  2005. {reply, true, S};
  2006. false ->
  2007. {reply, badarg, S}
  2008. end;
  2009. handle_call({audit_process, Pid}, _, S) ->
  2010. _ = case is_process_alive(Pid) of
  2011. false ->
  2012. process_is_down(Pid);
  2013. true ->
  2014. ignore
  2015. end,
  2016. {reply, ok, S};
  2017. handle_call({give_away, Key, To}, {Pid,_}, S) ->
  2018. Reply = do_give_away(Key, To, Pid),
  2019. {reply, Reply, S};
  2020. handle_call(_, _, S) ->
  2021. {reply, badarg, S}.
  2022. %% @hidden
  2023. handle_info({'DOWN', _MRef, process, Pid, _}, S) ->
  2024. _ = process_is_down(Pid),
  2025. {noreply, S};
  2026. handle_info(_, S) ->
  2027. {noreply, S}.
  2028. %% @hidden
  2029. code_change(_FromVsn, S, _Extra) ->
  2030. %% We have changed local monitor markers from {Pid} to {Pid,l}.
  2031. _ = case ets:select(?TAB, [{{'$1'},[],['$1']}]) of
  2032. [] ->
  2033. ok;
  2034. Pids ->
  2035. ets:insert(?TAB, [{P,l} || P <- Pids]),
  2036. ets:select_delete(?TAB, [{{'_'},[],[true]}])
  2037. end,
  2038. {ok, S}.
  2039. %% @hidden
  2040. terminate(_Reason, _S) ->
  2041. ok.
  2042. call(Req) ->
  2043. call(Req, l).
  2044. call(Req, l) ->
  2045. chk_reply(gen_server:call(?MODULE, Req));
  2046. call(Req, g) ->
  2047. chk_reply(gproc_dist:leader_call(Req)).
  2048. call(N, Req, l) ->
  2049. chk_reply(gen_server:call({?MODULE, N}, Req));
  2050. call(undefined, Req, g) ->
  2051. %% we always call the leader
  2052. chk_reply(gproc_dist:leader_call(Req)).
  2053. chk_reply(Reply) ->
  2054. case Reply of
  2055. badarg -> ?THROW_GPROC_ERROR(badarg);
  2056. _ -> Reply
  2057. end.
  2058. cast(Msg) ->
  2059. cast(Msg, l).
  2060. cast(Msg, l) ->
  2061. gen_server:cast(?MODULE, Msg);
  2062. cast(Msg, g) ->
  2063. gproc_dist:leader_cast(Msg).
  2064. cast(N, Msg, l) ->
  2065. gen_server:cast({?MODULE, N}, Msg).
  2066. try_insert_reg({T,l,_} = Key, Val, Pid) ->
  2067. case gproc_lib:insert_reg(Key, Val, Pid, l) of
  2068. false ->
  2069. case ets:lookup(?TAB, {Key,T}) of
  2070. %% In this particular case, the lookup cannot result in
  2071. %% [{_, Waiters}], since the insert_reg/4 function would
  2072. %% have succeeded then.
  2073. [{_, OtherPid, _}] ->
  2074. case is_process_alive(OtherPid) of
  2075. true ->
  2076. false;
  2077. false ->
  2078. process_is_down(OtherPid), % may result in failover
  2079. try_insert_reg(Key, Val, Pid)
  2080. end;
  2081. [] ->
  2082. false
  2083. end;
  2084. true ->
  2085. true
  2086. end.
  2087. %% try_insert_shared({c,l,_} = Key, Val) ->
  2088. %% ets:insert_new(?TAB, [{{Key,shared}, shared, Val}, {{shared, Key}, []}]);
  2089. %% try_insert_shared({a,l,_} = Key, Val) ->
  2090. %% ets:insert_new(?TAB, [{{Key, a}, shared, Val}, {{shared, Key}, []}]).
  2091. -spec audit_process(pid()) -> ok.
  2092. audit_process(Pid) when is_pid(Pid) ->
  2093. ok = gen_server:call(gproc, {audit_process, Pid}, infinity).
  2094. nb_audit_process(Pid) when is_pid(Pid) ->
  2095. ok = gen_server:cast(gproc, {audit_process, Pid}).
  2096. -spec process_is_down(pid()) -> ok.
  2097. process_is_down(Pid) when is_pid(Pid) ->
  2098. %% delete the monitor marker
  2099. %% io:fwrite(user, "process_is_down(~p) - ~p~n", [Pid,ets:tab2list(?TAB)]),
  2100. Marker = {Pid,l},
  2101. case ets:member(?TAB, Marker) of
  2102. false ->
  2103. ok;
  2104. true ->
  2105. Revs = ets:select(?TAB, [{{{Pid,'$1'}, '$2'},
  2106. [{'==',{element,2,'$1'},l}],
  2107. [{{'$1','$2'}}]}]),
  2108. lists:foreach(
  2109. fun({{n,l,_}=K, R}) ->
  2110. Key = {K,n},
  2111. case ets:lookup(?TAB, Key) of
  2112. [{_, Pid, V}] ->
  2113. ets:delete(?TAB, Key),
  2114. opt_notify(R, K, Pid, V);
  2115. [{_, Waiters}] ->
  2116. case [W || W <- Waiters,
  2117. element(1,W) =/= Pid] of
  2118. [] ->
  2119. ets:delete(?TAB, Key);
  2120. Waiters1 ->
  2121. ets:insert(?TAB, {Key, Waiters1})
  2122. end;
  2123. [{_, OtherPid, _}] when Pid =/= OtherPid ->
  2124. case ets:lookup(?TAB, {OtherPid, K}) of
  2125. [{RK, Opts}] when is_list(Opts) ->
  2126. Opts1 = gproc_lib:remove_monitor_pid(
  2127. Opts, Pid),
  2128. ets:insert(?TAB, {RK, Opts1});
  2129. _ ->
  2130. true
  2131. end;
  2132. [] ->
  2133. true
  2134. end;
  2135. ({{c,l,C} = K, _}) ->
  2136. Key = {K, Pid},
  2137. [{_, _, Value}] = ets:lookup(?TAB, Key),
  2138. ets:delete(?TAB, Key),
  2139. gproc_lib:update_aggr_counter(l, C, -Value);
  2140. ({{a,l,_} = K, R}) ->
  2141. case ets:lookup(?TAB, {K,a}) of
  2142. [{_, Pid, V}] ->
  2143. ets:delete(?TAB, {K,a}),
  2144. opt_notify(R, K, Pid, V);
  2145. [{_, OtherPid, _}] when Pid =/= OtherPid ->
  2146. case ets:lookup(?TAB, {OtherPid, K}) of
  2147. [{RK, Opts}] when is_list(Opts) ->
  2148. Opts1 = gproc_lib:remove_monitor_pid(
  2149. Opts, Pid),
  2150. ets:insert(?TAB, {RK, Opts1});
  2151. _ ->
  2152. true
  2153. end;
  2154. [] ->
  2155. opt_notify(R, K, Pid, undefined)
  2156. end;
  2157. ({{p,_,_} = K, _}) ->
  2158. ets:delete(?TAB, {K, Pid})
  2159. end, Revs),
  2160. ets:select_delete(?TAB, [{{{Pid,{'_',l,'_'}},'_'}, [], [true]}]),
  2161. ets:delete(?TAB, Marker),
  2162. ok
  2163. end.
  2164. opt_notify(r, _, _, _) ->
  2165. ok;
  2166. opt_notify(Opts, {T,_,_} = Key, Pid, Value) ->
  2167. case gproc_lib:standbys(Opts) of
  2168. [] ->
  2169. keep_followers(Opts, Key),
  2170. gproc_lib:notify(unreg, Key, Opts);
  2171. SBs ->
  2172. case pick_standby(SBs) of
  2173. false ->
  2174. keep_followers(Opts, Key),
  2175. gproc_lib:notify(unreg, Key, Opts),
  2176. ok;
  2177. {ToPid, Ref} ->
  2178. ets:insert(?TAB, [{{Key,T}, ToPid, Value},
  2179. {{ToPid, Key},
  2180. gproc_lib:remove_monitor(
  2181. Opts, ToPid, Ref)}]),
  2182. _ = gproc_lib:remove_reverse_mapping(
  2183. {failover,ToPid}, Pid, Key),
  2184. _ = gproc_lib:ensure_monitor(ToPid, l),
  2185. ok
  2186. end
  2187. end.
  2188. keep_followers(Opts, {T,_,_} = Key) ->
  2189. case gproc_lib:followers(Opts) of
  2190. [] ->
  2191. ok;
  2192. [_|_] = F ->
  2193. ets:insert(?TAB, {{Key,T}, F})
  2194. end.
  2195. pick_standby([{Pid, Ref, standby}|T]) when node(Pid) =:= node() ->
  2196. case is_process_alive(Pid) of
  2197. true ->
  2198. {Pid, Ref};
  2199. false ->
  2200. pick_standby(T)
  2201. end;
  2202. pick_standby([_|T]) ->
  2203. pick_standby(T);
  2204. pick_standby([]) ->
  2205. false.
  2206. do_give_away({T,l,_} = K, To, Pid) when T==n; T==a ->
  2207. Key = {K, T},
  2208. case ets:lookup(?TAB, Key) of
  2209. [{_, Pid, Value}] ->
  2210. %% Pid owns the reg; allowed to give_away
  2211. case pid_to_give_away_to(To) of
  2212. Pid ->
  2213. %% Give away to ourselves? Why not? We'll allow it,
  2214. %% but nothing needs to be done.
  2215. Pid;
  2216. ToPid when is_pid(ToPid) ->
  2217. ets:insert(?TAB, [{Key, ToPid, Value},
  2218. {{ToPid, K}, []}]),
  2219. _ = gproc_lib:remove_reverse_mapping({migrated,ToPid}, Pid, K),
  2220. _ = gproc_lib:ensure_monitor(ToPid, l),
  2221. ToPid;
  2222. undefined ->
  2223. _ = gproc_lib:remove_reg(K, Pid, unreg),
  2224. undefined
  2225. end;
  2226. _ ->
  2227. badarg
  2228. end;
  2229. do_give_away({T,l,_} = K, To, Pid) when T==c; T==p ->
  2230. Key = {K, Pid},
  2231. case ets:lookup(?TAB, Key) of
  2232. [{_, Pid, Value}] ->
  2233. case pid_to_give_away_to(To) of
  2234. ToPid when is_pid(ToPid) ->
  2235. ToKey = {K, ToPid},
  2236. case ets:member(?TAB, ToKey) of
  2237. true ->
  2238. badarg;
  2239. false ->
  2240. ets:insert(?TAB, [{ToKey, ToPid, Value},
  2241. {{ToPid, K}, []}]),
  2242. ets:delete(?TAB, {Pid, K}),
  2243. ets:delete(?TAB, Key),
  2244. _ = gproc_lib:ensure_monitor(ToPid, l),
  2245. ToPid
  2246. end;
  2247. undefined ->
  2248. _ = gproc_lib:remove_reg(K, Pid, {migrated, undefined}),
  2249. undefined
  2250. end;
  2251. _ ->
  2252. badarg
  2253. end.
  2254. pid_to_give_away_to(P) when is_pid(P), node(P) == node() ->
  2255. P;
  2256. pid_to_give_away_to({T,l,_} = Key) when T==n; T==a ->
  2257. case ets:lookup(?TAB, {Key, T}) of
  2258. [{_, Pid, _}] ->
  2259. Pid;
  2260. _ ->
  2261. undefined
  2262. end.
  2263. create_tabs() ->
  2264. Opts = gproc_lib:valid_opts(ets_options, [{write_concurrency,true},
  2265. {read_concurrency, true}]),
  2266. case ets:info(?TAB, name) of
  2267. undefined ->
  2268. ets:new(?TAB, [ordered_set, public, named_table | Opts]);
  2269. _ ->
  2270. ok
  2271. end.
  2272. %% @hidden
  2273. init([]) ->
  2274. set_monitors(),
  2275. {ok, #state{}}.
  2276. set_monitors() ->
  2277. set_monitors(ets:select(?TAB, [{{{'$1',l}},[],['$1']}], 100)).
  2278. set_monitors('$end_of_table') ->
  2279. ok;
  2280. set_monitors({Pids, Cont}) ->
  2281. _ = [erlang:monitor(process,Pid) || Pid <- Pids],
  2282. set_monitors(ets:select(Cont)).
  2283. monitor_me() ->
  2284. case ets:insert_new(?TAB, {{self(),l}}) of
  2285. false -> true;
  2286. true ->
  2287. cast({monitor_me,self()}),
  2288. true
  2289. end.
  2290. pattern([{'_', Gs, As}], T) ->
  2291. ?l,
  2292. {HeadPat, Vs} = headpat(T, '$1', '$2', '$3'),
  2293. [{HeadPat, rewrite(Gs,Vs), rewrite(As,Vs)}];
  2294. pattern([{{A,B,C},Gs,As}], Scope) ->
  2295. ?l,
  2296. {HeadPat, Vars} = headpat(Scope, A,B,C),
  2297. [{HeadPat, rewrite(Gs,Vars), rewrite(As,Vars)}];
  2298. pattern([{Head, Gs, As}], Scope) ->
  2299. ?l,
  2300. {S, T} = get_s_t(Scope),
  2301. case is_var(Head) of
  2302. {true,_N} ->
  2303. HeadPat = {{{T,S,'_'},'_'},'_','_'},
  2304. Vs = [{Head, obj_prod()}],
  2305. %% the headpat function should somehow verify that Head is
  2306. %% consistent with Scope (or should we add a guard?)
  2307. [{HeadPat, rewrite(Gs, Vs), rewrite(As, Vs)}];
  2308. false ->
  2309. erlang:error(badarg)
  2310. end.
  2311. %% This is the expression to use in guards and the RHS to address the whole
  2312. %% object, in its logical representation.
  2313. obj_prod() ->
  2314. {{ {element,1,{element,1,'$_'}},
  2315. {element,2,'$_'},
  2316. {element,3,'$_'} }}.
  2317. obj_prod_l() ->
  2318. [ {element,1,{element,1,'$_'}},
  2319. {element,2,'$_'},
  2320. {element,3,'$_'} ].
  2321. headpat({S, T}, V1,V2,V3) ->
  2322. headpat(type(T), scope(S), V1,V2,V3);
  2323. headpat(T, V1, V2, V3) when is_atom(T) ->
  2324. headpat(type(T), l, V1, V2, V3);
  2325. headpat(_, _, _, _) -> erlang:error(badarg).
  2326. headpat(T, C, V1,V2,V3) ->
  2327. Rf = fun(Pos) ->
  2328. {element,Pos,{element,1,{element,1,'$_'}}}
  2329. end,
  2330. K2 = if T==n orelse T==a -> T;
  2331. true -> '_'
  2332. end,
  2333. {Kp,Vars} = case V1 of
  2334. {Vt,Vc,Vn} ->
  2335. ?l,
  2336. {T1,Vs1} = subst(T,Vt,fun() -> Rf(1) end,[]),
  2337. {C1,Vs2} = subst(C,Vc,fun() -> Rf(2) end,Vs1),
  2338. {{T1,C1,Vn}, Vs2};
  2339. '_' ->
  2340. ?l,
  2341. {{T,C,'_'}, []};
  2342. _ ->
  2343. ?l,
  2344. case is_var(V1) of
  2345. {true,_} ->
  2346. {{T,C,V1}, [{V1, {element,1,
  2347. {element,1,'$_'}}}]};
  2348. false ->
  2349. erlang:error(badarg)
  2350. end
  2351. end,
  2352. {{{Kp,K2},V2,V3}, Vars}.
  2353. %% l(L) -> L.
  2354. subst(X, '_', _F, Vs) ->
  2355. {X, Vs};
  2356. subst(X, V, F, Vs) ->
  2357. case is_var(V) of
  2358. {true,_} ->
  2359. {X, [{V,F()}|Vs]};
  2360. false ->
  2361. {V, Vs}
  2362. end.
  2363. scope('_') -> '_';
  2364. scope(all) -> '_';
  2365. scope(global) -> g;
  2366. scope(local) -> l;
  2367. scope(S) when S==l; S==g -> S.
  2368. type('_') -> '_';
  2369. type(all) -> '_';
  2370. type(T) when T==n; T==p; T==c; T==a -> T;
  2371. type(names) -> n;
  2372. type(props) -> p;
  2373. type(counters) -> c;
  2374. type(aggr_counters) -> a.
  2375. rev_keypat(Context) ->
  2376. {S,T} = get_s_t(Context),
  2377. {T,S,'_'}.
  2378. get_s_t({S,T}) -> {scope(S), type(T)};
  2379. get_s_t(T) when is_atom(T) ->
  2380. {scope(all), type(T)}.
  2381. is_var('$1') -> {true,1};
  2382. is_var('$2') -> {true,2};
  2383. is_var('$3') -> {true,3};
  2384. is_var('$4') -> {true,4};
  2385. is_var('$5') -> {true,5};
  2386. is_var('$6') -> {true,6};
  2387. is_var('$7') -> {true,7};
  2388. is_var('$8') -> {true,8};
  2389. is_var('$9') -> {true,9};
  2390. is_var(X) when is_atom(X) ->
  2391. case atom_to_list(X) of
  2392. "\$" ++ Tl ->
  2393. try N = list_to_integer(Tl),
  2394. {true,N}
  2395. catch
  2396. error:_ ->
  2397. false
  2398. end;
  2399. _ ->
  2400. false
  2401. end;
  2402. is_var(_) -> false.
  2403. rewrite(Gs, R) ->
  2404. [rewrite1(G, R) || G <- Gs].
  2405. rewrite1('$_', _) ->
  2406. obj_prod();
  2407. rewrite1('$$', _) ->
  2408. obj_prod_l();
  2409. rewrite1(Guard, R) when is_tuple(Guard) ->
  2410. list_to_tuple([rewrite1(G, R) || G <- tuple_to_list(Guard)]);
  2411. rewrite1(Exprs, R) when is_list(Exprs) ->
  2412. [rewrite1(E, R) || E <- Exprs];
  2413. rewrite1(V, R) when is_atom(V) ->
  2414. case is_var(V) of
  2415. {true,_N} ->
  2416. case lists:keysearch(V, 1, R) of
  2417. {value, {_, V1}} ->
  2418. V1;
  2419. false ->
  2420. V
  2421. end;
  2422. false ->
  2423. V
  2424. end;
  2425. rewrite1(Expr, _) ->
  2426. Expr.
  2427. %% @spec () -> any()
  2428. %%
  2429. %% @doc
  2430. %% @equiv table({all, all})
  2431. %% @end
  2432. table() ->
  2433. table({all, all}).
  2434. %% @spec (Context::context()) -> any()
  2435. %%
  2436. %% @doc
  2437. %% @equiv table(Context, [])
  2438. %% @end
  2439. %%
  2440. table(Context) ->
  2441. table(Context, []).
  2442. %% @spec (Context::context(), Opts) -> any()
  2443. %%
  2444. %% @doc QLC table generator for the gproc registry.
  2445. %% Context specifies which subset of the registry should be queried.
  2446. %% See [http://www.erlang.org/doc/man/qlc.html].
  2447. %%
  2448. %% NOTE: By default, the gproc table generator will not filter out entries
  2449. %% belonging to processes that have just died, but which have yet to be cleared
  2450. %% out of the registry. Use the option `check_pids' (or `{check_pids, true}')
  2451. %% if you want to filter out dead entries already in the query. There will be
  2452. %% some overhead associated with doing so, and given that the process monitoring
  2453. %% is asynchronous, there can never be any guarantee that there are no dead
  2454. %% entries in the list by the time your program processes it.
  2455. %%
  2456. %% @end
  2457. table(Context, Opts) ->
  2458. Ctxt = {_, Type} = get_s_t(Context),
  2459. [Traverse, NObjs] = [proplists:get_value(K,Opts,Def) ||
  2460. {K,Def} <- [{traverse,select}, {n_objects,100}]],
  2461. CheckPids = proplists:get_bool(check_pids, Opts),
  2462. TF = case Traverse of
  2463. first_next ->
  2464. fun() -> qlc_next(Ctxt, first(Ctxt), CheckPids) end;
  2465. last_prev -> fun() -> qlc_prev(Ctxt, last(Ctxt), CheckPids) end;
  2466. select ->
  2467. fun(MS) -> qlc_select(
  2468. CheckPids,
  2469. select(Ctxt, wrap_qlc_ms_prod(CheckPids, MS),
  2470. NObjs))
  2471. end;
  2472. {select,MS} ->
  2473. fun() -> qlc_select(
  2474. CheckPids,
  2475. select(Ctxt, wrap_qlc_ms_prod(CheckPids, MS),
  2476. NObjs))
  2477. end;
  2478. _ ->
  2479. erlang:error(badarg, [Ctxt,Opts])
  2480. end,
  2481. InfoFun = fun(indices) -> [2];
  2482. (is_unique_objects) -> is_unique(Type);
  2483. (keypos) -> 1;
  2484. (is_sorted_key) -> true;
  2485. (num_of_objects) ->
  2486. %% this is just a guesstimate.
  2487. trunc(ets:info(?TAB,size) / 2.5)
  2488. end,
  2489. LookupFun =
  2490. case Traverse of
  2491. {select, _MS} -> undefined;
  2492. _ -> fun(Pos, Ks) -> qlc_lookup(Ctxt, Pos, Ks, CheckPids) end
  2493. end,
  2494. qlc:table(TF, [{info_fun, InfoFun},
  2495. {lookup_fun, LookupFun}] ++ [{K,V} || {K,V} <- Opts,
  2496. K =/= traverse,
  2497. K =/= n_objects]).
  2498. wrap_qlc_ms_prod(false, Pats) ->
  2499. Pats;
  2500. wrap_qlc_ms_prod(true, Pats) ->
  2501. [ wrap_qlc_ms_prod_(P) || P <- Pats ].
  2502. wrap_qlc_ms_prod_({H, Gs, [P]}) ->
  2503. {H, Gs, [{{ {element, 2, '$_'}, P }}]}.
  2504. qlc_lookup(_Scope, 1, Keys, Check) ->
  2505. lists:flatmap(
  2506. fun(Key) ->
  2507. remove_dead(
  2508. Check,
  2509. ets:select(?TAB, [{ {{Key,'_'},'_','_'}, [],
  2510. [{{ {element,1,{element,1,'$_'}},
  2511. {element,2,'$_'},
  2512. {element,3,'$_'} }}] }]))
  2513. end, Keys);
  2514. qlc_lookup(Scope, 2, Pids, Check) ->
  2515. lists:flatmap(fun(Pid) ->
  2516. qlc_lookup_pid(Pid, Scope, Check)
  2517. end, Pids).
  2518. remove_dead(false, Objs) ->
  2519. Objs;
  2520. remove_dead(true, Objs) ->
  2521. [ Reg || {_, Pid, _} = Reg <- Objs,
  2522. not ?PID_IS_DEAD(Pid) ].
  2523. %% While it may seem obsessive not to do the sensible pid liveness check here
  2524. %% every time, we make it optional for consistency; this way, we can devise
  2525. %% a test case that verifies the difference between having the option and not.
  2526. qlc_lookup_pid(Pid, Scope, Check) ->
  2527. case Check andalso ?PID_IS_DEAD(Pid) of
  2528. true ->
  2529. [];
  2530. false ->
  2531. Found =
  2532. ets:select(?TAB, [{{{Pid, rev_keypat(Scope)}, '_'},
  2533. [], ['$_']}]),
  2534. lists:flatmap(
  2535. fun({{_,{T,_,_}=K}, _}) ->
  2536. K2 = if T==n orelse T==a -> T;
  2537. true -> Pid
  2538. end,
  2539. case ets:lookup(?TAB, {K,K2}) of
  2540. [{{Key,_},_,Value}] ->
  2541. [{Key, Pid, Value}];
  2542. [] ->
  2543. []
  2544. end
  2545. end, Found)
  2546. end.
  2547. qlc_next(_, '$end_of_table', _) -> [];
  2548. qlc_next(Scope, K, Check) ->
  2549. case ets:lookup(?TAB, K) of
  2550. [{{Key,_}, Pid, V}] ->
  2551. case Check andalso ?PID_IS_DEAD(Pid) of
  2552. true ->
  2553. qlc_next(Scope, next(Scope, K), Check);
  2554. false ->
  2555. [{Key,Pid,V}] ++ fun() ->
  2556. qlc_next(Scope, next(Scope, K),
  2557. Check)
  2558. end
  2559. end;
  2560. [] ->
  2561. qlc_next(Scope, next(Scope, K), Check)
  2562. end.
  2563. qlc_prev(_, '$end_of_table', _) -> [];
  2564. qlc_prev(Scope, K, Check) ->
  2565. case ets:lookup(?TAB, K) of
  2566. [{{Key,_},Pid,V}] ->
  2567. case Check andalso ?PID_IS_DEAD(Pid) of
  2568. true ->
  2569. qlc_prev(Scope, prev(Scope, K), Check);
  2570. false ->
  2571. [{Key,Pid,V}] ++ fun() ->
  2572. qlc_prev(Scope, prev(Scope, K),
  2573. Check)
  2574. end
  2575. end;
  2576. [] ->
  2577. qlc_prev(Scope, prev(Scope, K), Check)
  2578. end.
  2579. qlc_select(_, '$end_of_table') ->
  2580. [];
  2581. qlc_select(true, {Objects, Cont}) ->
  2582. case [O || {Pid,O} <- Objects,
  2583. not ?PID_IS_DEAD(Pid)] of
  2584. [] ->
  2585. %% re-run search
  2586. qlc_select(true, ets:select(Cont));
  2587. Found ->
  2588. Found ++ fun() -> qlc_select(true, ets:select(Cont)) end
  2589. end;
  2590. qlc_select(false, {Objects, Cont}) ->
  2591. Objects ++ fun() -> qlc_select(false, ets:select(Cont)) end.
  2592. is_unique(n) -> true;
  2593. is_unique(a) -> true;
  2594. is_unique(_) -> false.
  2595. is_regged([{_, _, _}]) ->
  2596. true;
  2597. is_regged(_) ->
  2598. false.