MULTIPLICITY与PERL_IMPLICIT_CONTEXT的关系

The relationship between MULTIPLICITY and PERL_IMPLICIT_CONTEXT

Perl API 宏 MULTIPLICITYPERL_IMPLICIT_CONTEXT 之间有什么关系?

根据perlguts

One macro controls the major Perl build flavor: MULTIPLICITY. The MULTIPLICITY build has a C structure that packages all the interpreter state. With multiplicity-enabled perls, PERL_IMPLICIT_CONTEXT is also normally defined, and enables the support for passing in a "hidden" first argument that represents all three data structures.

(顺便问一下,这里指的是哪个"three data structures"?)

我注意到当我使用 usethreads:

构建 perl 时
./Configure -des -Dusethreads

PERL_IMPLICIT_CONTEXTMULTIPLICITY 都将被设置(定义)。 此外,在 embedvar.h 中有一条可能相关的评论:

The following combinations of MULTIPLICITY and PERL_IMPLICIT_CONTEXT are supported:
1) none
2) MULTIPLICITY # supported for compatibility
3) MULTIPLICITY && PERL_IMPLICIT_CONTEXT

All other combinations of these flags are errors.

only #3 is supported directly, while #2 is a special case of #3 (supported by redefining vTHX appropriately).

这是我目前的发现。 运行 sh Configure -des 创建 header config.h。此 header 文件将:

  • 定义 USE_ITHREADS 当且仅当 Configure 被赋予标志 -Dusethreads,例如:

    sh Configure -des -Dusethreads
    
  • 定义 MULTIPLICITY 当且仅当 Configure 被赋予标志 -Dusemultiplicity:

    sh Configure -des -Dusemultiplicity
    
  • 通过 ccflags 设置 MULTIPLICITY 不会 config.h 中设置 MULTIPLICITY,例如:

    sh Configure -des -Accflags="-DMULTIPLICITY"
    
  • Configure没有PERL_IMPLICIT_CONTEXT-D标志,通过ccflags定义它不会config.h.

  • 中定义

生成的 config.h header 是由 perl.h 生成的 #included。请注意,后者 header 通常也包含在 Perl XS 扩展文件(.xs-文件)中。 在 perl.h 的第 59 行,我们有:

#ifdef USE_ITHREADS  
#  if !defined(MULTIPLICITY)
#    define MULTIPLICITY
#  endif
#endif

#ifdef PERL_GLOBAL_STRUCT_PRIVATE
#  ifndef PERL_GLOBAL_STRUCT
#    define PERL_GLOBAL_STRUCT
#  endif
#endif

#ifdef PERL_GLOBAL_STRUCT
#  ifndef MULTIPLICITY
#    define MULTIPLICITY
#  endif
#endif

#ifdef MULTIPLICITY
#  ifndef PERL_IMPLICIT_CONTEXT
#    define PERL_IMPLICIT_CONTEXT
#  endif
#endif

这意味着:

  • 如果给出-Dusethreads,则USE_ITHREADSMULTIPLICITYPERL_IMPLICIT_CONTEXT都会被定义。

  • 如果给出-DusemultiplicityMULTIPLICITYPERL_IMPLICIT_CONTEXT将被定义,而USE_ITHREADS将是未定义的。

  • if none of -Dusethreads or -Dusemultiplicity is given USE_ITHREADS, MULTIPLICITY, and PERL_IMPLICIT_CONTEXT will all未定义。

  • 不可能有 MULTIPLICITY 定义和 PERL_IMPLICIT_CONTEXT 未定义(除非使用 ccflags,但这只会在 perl 构建期间发生。包含 perl.h 的 XS 扩展模块不会看到这个)

所以扩展模块通常可以假设:

  • MULTIPLICITYPERL_IMPLICIT_CONTEXT 都已定义,或者
  • MULTIPLICITYPERL_IMPLICIT_CONTEXT 均未定义。