- Perl ›
- Language implementation ›
- here
perlvars.h
perlvars.h defines Perl global variable.
/* perlvars.h */ / * global state */# if defined(USE_ITHREADS) PERLVAR (G, op_mutex, perl_mutex)/* Mutex for op refcounting */# endif PERLVARI (G, curinterp, PerlInterpreter *, NULL) / * currently running interpreter * (initial parent interpreter under under * useithreads) */# if defined(USE_ITHREADS) PERLVAR (G, thr_key, perl_key)/* key to retrieve per-thread struct */# endif
Perl has an interpreter variable for each interpreter, but there are also global variable. For example, the interpreter currently running, the mutex to control thread contention, etc. are defined as global variable.
The declaration of global variable is described using the macros "PERLVAR" and "PERLVARI". It is defined in "perl.h".
=====
/*perl.h */ / * Set up PERLVAR macros for populating structs */# define PERLVAR (prefix, var, type) type prefix # # var; / *'var' is an array of length'n' */# define PERLVARA (prefix, var, n, type) type prefix # # var [n]; / * initialize'var' to init' */# define PERLVARI (prefix, var, type, init) type prefix # # var; / * like PERLVARI, but make'var' a const */# define PERLVARIC (prefix, var, type, init) type prefix # # var;
For example, "PERLVAR (G, op_mutex, perl_mutex)" expands as follows:
perl_mutex Gop_mutex;
This global variable is not used directly on the source, but is accessed in a wrapped form such as "PL_op_mutex".
PL_op_mutex = ...;
The reason for this wrapping is that the storage method of global variable differs depending on the compile option "PERL_GLOBAL_STRUCT". If "PERL_GLOBAL_STRUCT" is defined, the global variable is stored in the structure, otherwise the global variable is written directly.
Let's see how "PL_op_mutex" is expanded on this premise. First, from "embedvar.h"
# if defined(PERL_GLOBAL_STRUCT) # define PL_op_mutex (my_vars->Gop_mutex) # endif
"My_vars" is defined in "perlmain.c".
/* perlmain.c */struct perl_vars * my_vars = init_global_struct ();
Let's also look at the definition of a structure called "perl_vars". You can see that it is a structure that has the contents of "perlvars.h" as a member variable.
struct perl_vars { # include "perlvars.h" };