public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: LIU Hao <lh_mouse@126.com>
To: Andrew Pinski <pinskia@gmail.com>
Cc: gcc@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: why does gccgit require pthread?
Date: Mon, 7 Nov 2022 14:50:25 +0800	[thread overview]
Message-ID: <98c69fc7-713f-a09a-8ca7-c3e64bdfd309@126.com> (raw)
In-Reply-To: <CA+=Sn1ncp6hN8MnziZ+2f-6_UUqMwXeVGQLmft2NUeuXGOaBoA@mail.gmail.com>


[-- Attachment #1.1.1: Type: text/plain, Size: 838 bytes --]

在 2022-11-07 12:37, Andrew Pinski 写道:
> 
> 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.

The win32 thread model does not have `std::mutex`; but there is no `pthread_mutex_t` either, so it 
does not build either way.

Tested bootstrapping GCC on `{i686,x86_64}-w64-mingw32` with languages 
`c,lto,c++,fortran,objc,obj-c++` and with the `mcf` thread model; no errors observed. The built 
`libgccjit-0.dll` does not have imports from winpthread any more.

Please review.


-- 
Best regards,
LIU Hao


[-- Attachment #1.1.2: 9005-gcc-jit-Use-C-11-mutex-instead-of-pthread-s.patch --]
[-- Type: text/plain, Size: 4067 bytes --]

From ceb65f21b5ac23ce218efee82f40f641ebe44361 Mon Sep 17 00:00:00 2001
From: LIU Hao <lh_mouse@126.com>
Date: Mon, 7 Nov 2022 13:00:12 +0800
Subject: [PATCH] gcc/jit: Use C++11 mutex instead of pthread's

This allows JIT to be built with a different thread model from `posix`
where pthread isn't available

gcc/jit/ChangeLog:

	* jit-playback.cc: Use `std::mutex` instead of `pthread_mutex_t`
	(playback::context::acquire_mutex): Likewise
	(playback::context::release_mutex): Likewise
	* jit-recording.cc: Remove the unused `INCLUDE_PTHREAD_H`
	* libgccjit.cc: Use `std::mutex` instead of `pthread_mutex_t`
---
 gcc/jit/jit-playback.cc  | 9 +++++----
 gcc/jit/jit-recording.cc | 1 -
 gcc/jit/libgccjit.cc     | 8 ++++----
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/gcc/jit/jit-playback.cc b/gcc/jit/jit-playback.cc
index d227d36283a..17ff98c149b 100644
--- a/gcc/jit/jit-playback.cc
+++ b/gcc/jit/jit-playback.cc
@@ -19,7 +19,6 @@ along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
-#define INCLUDE_PTHREAD_H
 #include "system.h"
 #include "coretypes.h"
 #include "target.h"
@@ -51,6 +50,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "jit-w32.h"
 #endif
 
+#include <mutex>
+
 /* Compare with gcc/c-family/c-common.h: DECL_C_BIT_FIELD,
    SET_DECL_C_BIT_FIELD.
    These are redefined here to avoid depending from the C frontend.  */
@@ -2662,7 +2663,7 @@ 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 = PTHREAD_MUTEX_INITIALIZER;
+static std::mutex jit_mutex;
 
 /* Acquire jit_mutex and set "this" as the active playback ctxt.  */
 
@@ -2673,7 +2674,7 @@ playback::context::acquire_mutex ()
 
   /* Acquire the big GCC mutex. */
   JIT_LOG_SCOPE (get_logger ());
-  pthread_mutex_lock (&jit_mutex);
+  jit_mutex.lock ();
   gcc_assert (active_playback_ctxt == NULL);
   active_playback_ctxt = this;
 }
@@ -2687,7 +2688,7 @@ playback::context::release_mutex ()
   JIT_LOG_SCOPE (get_logger ());
   gcc_assert (active_playback_ctxt == this);
   active_playback_ctxt = 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-recording.cc b/gcc/jit/jit-recording.cc
index f78daed2d71..6ae5a667e90 100644
--- a/gcc/jit/jit-recording.cc
+++ b/gcc/jit/jit-recording.cc
@@ -19,7 +19,6 @@ along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
-#define INCLUDE_PTHREAD_H
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
diff --git a/gcc/jit/libgccjit.cc b/gcc/jit/libgccjit.cc
index ca862662777..a5105fbc1f9 100644
--- a/gcc/jit/libgccjit.cc
+++ b/gcc/jit/libgccjit.cc
@@ -19,7 +19,6 @@ along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
-#define INCLUDE_PTHREAD_H
 #include "system.h"
 #include "coretypes.h"
 #include "timevar.h"
@@ -30,6 +29,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "jit-recording.h"
 #include "jit-result.h"
 
+#include <mutex>
+
 /* The opaque types used by the public API are actually subclasses
    of the gcc::jit::recording classes.  */
 
@@ -4060,7 +4061,7 @@ gcc_jit_context_new_rvalue_from_vector (gcc_jit_context *ctxt,
    Ideally this would be within parse_basever, but the mutex is only needed
    by libgccjit.  */
 
-static pthread_mutex_t version_mutex = PTHREAD_MUTEX_INITIALIZER;
+static std::mutex version_mutex;
 
 struct jit_version_info
 {
@@ -4068,9 +4069,8 @@ struct jit_version_info
      guarded by version_mutex.  */
   jit_version_info ()
   {
-    pthread_mutex_lock (&version_mutex);
+    std::lock_guard<std::mutex> g (version_mutex);
     parse_basever (&major, &minor, &patchlevel);
-    pthread_mutex_unlock (&version_mutex);
   }
 
   int major;
-- 
2.38.1


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

       reply	other threads:[~2022-11-07  6:50 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <8f15b063-9ec8-59e0-590b-20e416f68cb4@126.com>
     [not found] ` <CA+=Sn1ncp6hN8MnziZ+2f-6_UUqMwXeVGQLmft2NUeuXGOaBoA@mail.gmail.com>
2022-11-07  6:50   ` LIU Hao [this message]
2022-11-07  7:03     ` Andrew Pinski
2022-11-07  7:10       ` LIU Hao
2022-11-07 12:57     ` Jonathan Wakely
2022-11-07 13:33       ` LIU Hao
2022-11-07 13:51         ` Jonathan Wakely
2022-11-11 17:16           ` Jonathan Wakely
2022-11-11 18:27             ` Jonathan Wakely
2022-11-14 10:54               ` LIU Hao
2022-11-15 18:50             ` why does gcc jit " David Malcolm
2022-11-15 19:01               ` Jonathan Wakely
2022-11-19 11:27                 ` Jonathan Wakely
2022-11-19 12:51                   ` LIU Hao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=98c69fc7-713f-a09a-8ca7-c3e64bdfd309@126.com \
    --to=lh_mouse@126.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gcc@gcc.gnu.org \
    --cc=pinskia@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).