From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) by sourceware.org (Postfix) with ESMTPS id 0372B3858D20 for ; Fri, 28 Jul 2023 15:36:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0372B3858D20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-31765aee31bso2294862f8f.1 for ; Fri, 28 Jul 2023 08:36:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690558591; x=1691163391; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LBfcoKCDov8PAe5X4CnAeQM9y+VswnMO+dyUYqKjMkg=; b=WNhQbAOIS4IgawC8st7dJzZM6v4xmk9Ld2LSBdZ1/EwxbyAoySxhM2kXTL70FDmj7/ kzk6UnsgncuZG+p8lXjJ7oHLWy0DgGEQAKULBmJZna2FEomieVp2dYxIMuqtHciLJ/SD 7lHjBPb3QuVXr9aQUyALmRhTwHFBBhpYctb38001xGjusuenYV2HnQCU6YWF7pDZLseg Jf8CeW02xGimsWWSy0zqiqD9PoC6vKJ4apFqXyR+Yj9v9lI+Sprt1zOleHSj8mlyMh5i UzF3GUZsN39YG/3ul/NgBKHEOSK83OVp3ywcOdWxhxb/BSyHKVRx1adIim+x1Ds/nHQX /l9Q== X-Gm-Message-State: ABy/qLYkJaX482GWMt3Sa1lpmnOLSrDxUCysuscPe+YKTQh43I35jJiP TXIMf2LAv4tugM9n0fMta4o= X-Google-Smtp-Source: APBJJlG5ifJ4k0Hhg6DVlpHXLN0ksSdXNylBBtmtLVKYsGCfPEoVj5KlJqHyCmzOH9qik9ElYuos5g== X-Received: by 2002:a5d:50cf:0:b0:317:7068:4997 with SMTP id f15-20020a5d50cf000000b0031770684997mr1988012wrt.60.1690558590771; Fri, 28 Jul 2023 08:36:30 -0700 (PDT) Received: from ?IPV6:2001:8a0:f922:de00:5b94:75a9:c970:55df? ([2001:8a0:f922:de00:5b94:75a9:c970:55df]) by smtp.gmail.com with ESMTPSA id s7-20020adff807000000b00313e2abfb8dsm5053068wrp.92.2023.07.28.08.36.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Jul 2023 08:36:30 -0700 (PDT) Message-ID: <591a3e4a-ac32-805b-75e4-c378ef641bdd@palves.net> Date: Fri, 28 Jul 2023 16:36:27 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH 2/2] [gdb] Initialize main_thread_id earlier Content-Language: en-US To: Tom de Vries , gdb-patches@sourceware.org References: <20230728105945.13909-1-tdevries@suse.de> <20230728105945.13909-2-tdevries@suse.de> From: Pedro Alves In-Reply-To: <20230728105945.13909-2-tdevries@suse.de> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,GIT_PATCH_0,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 2023-07-28 11:59, Tom de Vries via Gdb-patches wrote: > I wrote a patch using is_main_thread (), and found it returning false in the > main thread due to main_thread_id not being initialized yet. > > Initialization currently takes place in _initialize_run_on_main_thread, but > that's too late for earlier uses. > > Fix this by also initializing on first use. > > Tested on x86_64-linux. > --- > gdb/run-on-main-thread.c | 17 ++++++++++++++++- > 1 file changed, 16 insertions(+), 1 deletion(-) > > diff --git a/gdb/run-on-main-thread.c b/gdb/run-on-main-thread.c > index 91d25dae28f..bee5885d9a6 100644 > --- a/gdb/run-on-main-thread.c > +++ b/gdb/run-on-main-thread.c > @@ -94,12 +94,27 @@ run_on_main_thread (std::function &&func) > serial_event_set (runnable_event); > } > > +#if CXX_STD_THREAD > +static void > +initialize_main_thread_id (void) > +{ > + static bool initialized = false; > + > + if (initialized) > + return; > + initialized = true; > + > + main_thread_id = std::this_thread::get_id (); > +} > +#endif > + > /* See run-on-main-thread.h. */ > > bool > is_main_thread () > { > #if CXX_STD_THREAD > + initialize_main_thread_id (); This is assuming that is_main_thread() will always be called once by the main thread, before any other thread could call it. Otherwise if is_main_thread is called by some other thread first, is_main_thread -> initialize_main_thread_id will record the wrong thread id in the main_thread_id. At least a comment somewhere would be warranted. I think putting: gdb_assert (is_main_thread ()); somewhere in the early main code would be good. It would prevent such situation from ever happening undetected.