From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id CB7AF1F47C for ; Sun, 8 Jan 2023 23:43:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1673221401; bh=adlp95DRGq9bSNR/BIP0pQ4N9WsTMxRkfzXGpmUWn7c=; h=From:To:Subject:Date:From; b=C0vswhfyhOh1fGqQQ5XvYcF05u+c8rVJXadvcXR+rcHRi5KqFPUGL/8G6hIMr/nIU L1f81j3k9XXrArPWYA39FdYI7Ia/wfLJ34f9lutORgLrAmoJcNR+BEOFikrJ+OYygH f+XySJ1bFW0NiQqHt9FJZ8D7yWGBjB5IfrjyOW/c= From: Eric Wong To: Subject: [PATCH] make Ruby headers less intrusive Date: Sun, 8 Jan 2023 23:43:21 +0000 Message-Id: <20230108234321.16994-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: By including them at the bottom. This will be done for Perl headers in the future, too, since they break assert(). --- ext/mwrap/httpd.h | 8 ---- ext/mwrap/mwrap_core.h | 104 +++++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 59 deletions(-) diff --git a/ext/mwrap/httpd.h b/ext/mwrap/httpd.h index da7ff6d..03aef9f 100644 --- a/ext/mwrap/httpd.h +++ b/ext/mwrap/httpd.h @@ -43,14 +43,6 @@ #define TYPE_CSV "text/csv" #define TYPE_PLAIN "text/plain" -/* - * C ruby defines snprintf to ruby_snprintf, we can't have that in - * non-ruby processes spawned by C ruby - */ -#if MWRAP_RUBY && defined(snprintf) -# undef snprintf -#endif - enum mw_qev { MW_QEV_IGNORE = 0, MW_QEV_RD = POLLIN, diff --git a/ext/mwrap/mwrap_core.h b/ext/mwrap/mwrap_core.h index 721e5d3..48669d5 100644 --- a/ext/mwrap/mwrap_core.h +++ b/ext/mwrap/mwrap_core.h @@ -46,13 +46,6 @@ # include "ppport.h" #endif -#if MWRAP_RUBY -# undef _GNU_SOURCE /* ruby.h redefines it */ -# include /* defines HAVE_RUBY_RACTOR_H on 3.0+ */ -# include -# include -#endif - /* * XXH3 (truncated to 32-bits) seems to provide a ~2% speedup. * XXH32 doesn't show improvements over jhash despite rculfhash @@ -90,44 +83,11 @@ static size_t *root_locating; /* determines if PL_curcop is our thread */ #endif /* MWRAP_PERL */ #if MWRAP_RUBY -const char *rb_source_location_cstr(int *line); /* requires 2.6.0dev or later */ - -# ifdef HAVE_RUBY_RACTOR_H /* Ruby 3.0+ */ -extern MWRAP_TSD void * __attribute__((weak)) ruby_current_ec; -# else /* Ruby 2.6-2.7 */ -extern void * __attribute__((weak)) ruby_current_execution_context_ptr; -# define ruby_current_ec ruby_current_execution_context_ptr -# endif /* HAVE_RUBY_RACTOR_H */ - -extern void * __attribute__((weak)) ruby_current_vm_ptr; /* for rb_gc_count */ -extern size_t __attribute__((weak)) rb_gc_count(void); -int __attribute__((weak)) ruby_thread_has_gvl_p(void); - -/* - * rb_source_location_cstr relies on GET_EC(), and it's possible - * to have a native thread but no EC during the early and late - * (teardown) phases of the Ruby process - */ -static int has_ec_p(void) -{ - return ruby_thread_has_gvl_p && ruby_thread_has_gvl_p() && - ruby_current_vm_ptr && ruby_current_ec; -} - -static void set_generation(size_t *gen, size_t size) -{ - if (rb_gc_count) { - uatomic_add_return(&total_bytes_inc, size); - if (has_ec_p()) - *gen = rb_gc_count(); - } else { - *gen = uatomic_add_return(&total_bytes_inc, size); - } -} -# define SET_GENERATION(gen, size) set_generation(gen, size) +static void mw_ruby_set_generation(size_t *, size_t); +# define SET_GENERATION(gen, size) mw_ruby_set_generation(gen, size) #endif /* MWRAP_RUBY */ -#ifndef SET_GENERATION +#ifndef SET_GENERATION /* C-only builds w/o Perl|Ruby */ # define SET_GENERATION(gen, size) \ *gen = uatomic_add_return(&total_bytes_inc, size) #endif /* !SET_GENERATION */ @@ -438,14 +398,7 @@ static const char *mw_perl_src_file_cstr(unsigned *lineno) #endif /* MWRAP_PERL */ #if MWRAP_RUBY -static const char *mw_ruby_src_file_cstr(unsigned *lineno) -{ - if (!has_ec_p()) return NULL; - int line; - const char *fn = rb_source_location_cstr(&line); - *lineno = line < 0 ? UINT_MAX : (unsigned)line; - return fn; -} +static const char *mw_ruby_src_file_cstr(unsigned *lineno); # define SRC_FILE_CSTR(lineno) mw_ruby_src_file_cstr(lineno) #endif /* MWRAP_RUBY */ @@ -1088,3 +1041,52 @@ __attribute__((constructor)) static void mwrap_ctor(void) } --locating; } + +#if MWRAP_RUBY +# undef _GNU_SOURCE /* ruby.h redefines it */ +# include /* defines HAVE_RUBY_RACTOR_H on 3.0+ */ +# include +# include +# ifdef HAVE_RUBY_RACTOR_H /* Ruby 3.0+ */ +extern MWRAP_TSD void * __attribute__((weak)) ruby_current_ec; +# else /* Ruby 2.6-2.7 */ +extern void * __attribute__((weak)) ruby_current_execution_context_ptr; +# define ruby_current_ec ruby_current_execution_context_ptr +# endif /* HAVE_RUBY_RACTOR_H */ + +extern void * __attribute__((weak)) ruby_current_vm_ptr; /* for rb_gc_count */ +extern size_t __attribute__((weak)) rb_gc_count(void); +int __attribute__((weak)) ruby_thread_has_gvl_p(void); + +const char *rb_source_location_cstr(int *line); /* requires 2.6.0dev or later */ +/* + * rb_source_location_cstr relies on GET_EC(), and it's possible + * to have a native thread but no EC during the early and late + * (teardown) phases of the Ruby process + */ +static int has_ec_p(void) +{ + return ruby_thread_has_gvl_p && ruby_thread_has_gvl_p() && + ruby_current_vm_ptr && ruby_current_ec; +} + +static void mw_ruby_set_generation(size_t *gen, size_t size) +{ + if (rb_gc_count) { + uatomic_add_return(&total_bytes_inc, size); + if (has_ec_p()) + *gen = rb_gc_count(); + } else { + *gen = uatomic_add_return(&total_bytes_inc, size); + } +} + +static const char *mw_ruby_src_file_cstr(unsigned *lineno) +{ + if (!has_ec_p()) return NULL; + int line; + const char *fn = rb_source_location_cstr(&line); + *lineno = line < 0 ? UINT_MAX : (unsigned)line; + return fn; +} +#endif /* !MWRAP_RUBY */