[erlang-questions] Request for help on creating BIF (or finding existing one)

=Bill.Barnhill xmlarchitect@REDACTED
Tue Mar 10 23:39:34 CET 2009

Hello all,

I was recently asked how to have a process get its registered name. AFAIK
there is no BIF for this. It can be accomplished via:
[ N || N <- global:registered_names(), global:whereis_name(N) == self()]
which will either return [] if it's not registered or [Name] if it is.

A BIF along the lines of name() that returned the name would avoid the
overhead of cycling through the process list in the above code. Roberto
Ostinelli added that returning {global|local, Name} would be good, not sure
how to do that.

To that end I've started trying to put together a function to do that, but I
don't have my head around the Erlang sources yet so I'd appreciate help on
the following:

1) Is there a BIF already that does this?
2) Is this the correct way?
3) How do I return an Eterm value of 'none'?
4) How do I hook the function in as a BIF?
5) How would I make it so it returns {local|global, Name} instead of just

What I've got so far is:

   1. Eterm erts_thr_self_name(void) {
   2.         struct process * self_info  = erts_get_current_process();
   3.         struct reg_proc * self_reg_info = self_info->reg;
   4.         if (self_reg_info != NULL) { /* ie if we're actually
   registered */
   5.                 Eterm name = self_reg_info->name;
   6.                 return name;
   7.         }
   8.         else {
   9.                 return /* Where to find 'none' Eterm value?? */
   10.         }
   11. }

For convenience it's also at pastebin: http://pastebin.com/m2e5816e6

Any help much appreciated,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090310/0a55decf/attachment.htm>

More information about the erlang-questions mailing list