From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by sourceware.org (Postfix) with ESMTPS id 6C1153858C78; Mon, 7 Nov 2022 12:57:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6C1153858C78 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x52f.google.com with SMTP id 21so17432072edv.3; Mon, 07 Nov 2022 04:57:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=tkTKSwkLo+PQ5jQ5O/xaERTnbWqvbSZ66ZnfMfX2qi8=; b=EeLJeWn0O6S/NyOKThgOVqaNJ3hYWQkAVlUjpAOq4roiHTMbgVzl8uuwPxTrGvxZP9 6YmNxeEec+ZzzA4gnIOdFHsgq63qiSC9Eb5NIYqjhEbB7ERf/ilXJZ8TNQegJcLqoic6 rc1uIPPk3pWFHEDXDC24oqTdrm2OyAtIysFLVIsfkV/7XqE/ll7dBRyimD0zI2Yzlp8N 25DHjOZbv58jHsBeLvtVovV7QA0r9pmqF0zCA9ttiJtQNiks0Mg+9xWCU6X2uGRonr3K ZecjI//E5m2ukv3PL9V8OqH6SSlkC4leXoS1S7h/m924yawVBpFQwBQu+8lbuWpF02Ve rEOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tkTKSwkLo+PQ5jQ5O/xaERTnbWqvbSZ66ZnfMfX2qi8=; b=u8RF+OA73ri+YlDyJVpkId8I717rH4nYrkuGDHPZPbb1lTK5nZiVUdZgTjzaWuHD6f HxNZRZJATeiQAjSXzF1vKr6cEAVA31c2Lofqi3HJVvqp7kTkGlvHG/c+SpkEy7yBUTuN CnorpVNMav2uT/pWuadf9oQywUib7kTGyxAaUUteXDlkzunD4R9Dev5ID8FtC9qux7WW b2PXQH7BmE4ceX0rBJEQunPLqrhYebJqrB/JQHN8HdOgnCp9sphNcnfyE3i3TGVEv87/ ST0CedsLfH9B4DxR49F7zwDAZQVaSjwD7uk5Z8wHw9zQyXRa0f/fvDmv2/4Vx4K2A33t zwKg== X-Gm-Message-State: ACrzQf2LYVxPl3GGKfNedjfIFbhoLrP7ZiGGuo5MvjYoC93rwDeRRdIC XliWA6nXfxDBIOx0fmbM3TnAJZmuUE/6WBxVm80= X-Google-Smtp-Source: AMsMyM6lcFPEipaXEtZubz9/d9A3Nfo3jx9igq8R5kalDePuHbVEUUij/MUY0N/RG9Hf7wGqvM2FepRsaAFWcpkHZ34= X-Received: by 2002:a05:6402:358e:b0:461:ea80:fb61 with SMTP id y14-20020a056402358e00b00461ea80fb61mr49735291edc.356.1667825875169; Mon, 07 Nov 2022 04:57:55 -0800 (PST) MIME-Version: 1.0 References: <8f15b063-9ec8-59e0-590b-20e416f68cb4@126.com> <98c69fc7-713f-a09a-8ca7-c3e64bdfd309@126.com> In-Reply-To: <98c69fc7-713f-a09a-8ca7-c3e64bdfd309@126.com> From: Jonathan Wakely Date: Mon, 7 Nov 2022 12:57:44 +0000 Message-ID: Subject: Re: why does gccgit require pthread? To: LIU Hao Cc: Andrew Pinski , gcc@gcc.gnu.org, gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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 Mon, 7 Nov 2022 at 06:51, LIU Hao via Gcc wrote: > > =E5=9C=A8 2022-11-07 12:37, Andrew Pinski =E5=86=99=E9=81=93: > > > > The original code which used pthread was added in GCC 5 way before GCC > > moved to being written in C++11 which was only in the last 3 years. > > pthread_* functions were the best choice at the time (2014) but now > > GCC is written in C++11, I don't see any reason not to move them over > > to using C++11 threading code. > > > > > > Attached is the proposed patch. It would be a lot nicer if playback::context met the C++ Lockable requirements, and playback::context::compile () could just take a scoped lock on *this: --- a/gcc/jit/jit-playback.cc +++ b/gcc/jit/jit-playback.cc @@ -2353,15 +2353,12 @@ compile () m_recording_ctxt->get_all_requested_dumps (&requested_dumps); /* Acquire the JIT mutex and set "this" as the active playback ctxt. */ - acquire_mutex (); + std::lock_guard lock(*this); auto_string_vec fake_args; make_fake_args (&fake_args, ctxt_progname, &requested_dumps); if (errors_occurred ()) - { - release_mutex (); - return; - } + return; /* This runs the compiler. */ toplev toplev (get_timer (), /* external_timer */ @@ -2388,10 +2385,7 @@ compile () followup activities use timevars, which are global state. */ if (errors_occurred ()) - { - release_mutex (); - return; - } + return; if (get_bool_option (GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE)) dump_generated_code (); @@ -2403,8 +2397,6 @@ compile () convert the .s file to the requested output format, and copy it to a given file (playback::compile_to_file). */ postprocess (ctxt_progname); - - release_mutex (); } /* Implementation of class gcc::jit::playback::compile_to_memory, @@ -2662,18 +2654,18 @@ playback::compile_to_file::copy_file (const char *src_path, /* This mutex guards gcc::jit::recording::context::compile, so that only one thread can be accessing the bulk of GCC's state at once. */ -static pthread_mutex_t jit_mutex =3D PTHREAD_MUTEX_INITIALIZER; +static std::mutex jit_mutex; /* Acquire jit_mutex and set "this" as the active playback ctxt. */ void -playback::context::acquire_mutex () +playback::context::lock () { auto_timevar tv (get_timer (), TV_JIT_ACQUIRING_MUTEX); /* Acquire the big GCC mutex. */ JIT_LOG_SCOPE (get_logger ()); - pthread_mutex_lock (&jit_mutex); + jit_mutex.lock(); gcc_assert (active_playback_ctxt =3D=3D NULL); active_playback_ctxt =3D this; } @@ -2681,13 +2673,13 @@ playback::context::acquire_mutex () /* Release jit_mutex and clear the active playback ctxt. */ void -playback::context::release_mutex () +playback::context::unlock () { /* Release the big GCC mutex. */ JIT_LOG_SCOPE (get_logger ()); gcc_assert (active_playback_ctxt =3D=3D this); active_playback_ctxt =3D NULL; - pthread_mutex_unlock (&jit_mutex); + jit_mutex.unlock(); } /* Callback used by gcc::jit::playback::context::make_fake_args when diff --git a/gcc/jit/jit-playback.h b/gcc/jit/jit-playback.h index 3ba02a0451a..bd2bc389f53 100644 --- a/gcc/jit/jit-playback.h +++ b/gcc/jit/jit-playback.h @@ -314,8 +314,8 @@ private: /* Functions for implementing "compile". */ - void acquire_mutex (); - void release_mutex (); + void lock (); + void unlock (); void make_fake_args (vec *argvec,