I.e., not a process ID, or a thread ID, but an Interpreter ID.

As I understand it (from my reading of perlguts/etc), whenvever a new interpreter is created or cloned, there's a whole structure created that contains interpreter-specific stuff, and it seems to me there ought to be something in there that is unique to that interpreter instance and easy to get at....

... and perhaps even accessible from pure Perl (is there a magic variable I've overlooked?)

To be sure, I imagine I could write an XS function that, say, takes the address of PL_modglobal or Perl_get_context(), returns it as an integer and use that to distinguish my interpreters, but

  1. I'm not that well versed in XS and would prefer to stay in pure Perl if I possibly can.
  2. I'd like this to be as OS-independent as possible.
  3. Would taking those addresses even work?
    I.e., if the garbage collector is allowed to arbitrarily relocate interpreter structures, then that approach is doomed. (... granted, I would have thought interpreter relocation would be Very Hard to do given what (little) I know of Perl's current architecture, but maybe not; we'll skip the Small Matter of how this all changes in Perl 6...)
  4. Or, if taking these addresses would work, and there is no pure Perl way to do this, any idea which would be better to do? (i.e., thoughts about &PL_modglobal vs. &Perl_get_context vs. Something Else?)

. . . . .

For the curious, here's the problem I'm actually trying to solve, for which being able to get at an Interpreter ID seems the obvious, easy answer at the moment:

Consider a script that is running possibly in multiple processes and/or multiple threads at the same time (think mod_perl on MPM-Worker, but I don't want to limit myself to that particular situation).

The script needs to contain a counter method, e.g.,

my @values = (0, $$, time(), other_initialization_stuff() ); sub counter { ++$values[0]; return @values; }

the idea being that every invocation of this method must return a distinct result, no matter which process or thread the invocation occurs on. (Why do I need this? Cryptography.)

It's also important to be able to do this without having to consult any kind of shared cache.

So what's the minimal data other_initialization_stuff() can be returning so that this is actually the case? Bonus points if it's something that's guaranteed to work on all OS's (though I'm happy enough if I can cover Linux and Win32).

Including $$ distinguishes different processes (**) (***); so it's really about how to deal with multiple interpreters in the same process.

Note that Thread ID does not help us because in some cases (e.g., mod_perl) there is not a fixed correspondence between threads and interpreters -- we cannot rule out multiple interpreters getting (consecutively) loaded into the same thread to run the counter initialization code.

My theory in this is that there will be a one-to-one correspondence between interpreter IDs and the distinct instances of @values within a given process, which is what I want.


In reply to How do I get a unique Perl Interpreter ID? by wrog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.