Gauche:Windows/MinGW:pthread対応

Gauche:Windows/MinGW:pthread対応

齊藤 (2010/03/09 22:42:32 PST)

Mingw 版 Gauche を pthread 対応にする手順についての記述です。

msys + mingw の環境は用意できている前提とします。

まず必要な作業は pthread ライブラリの導入です。 Windows 自体は pthread 互換層を提供していません。 以下のサイトから POSIX Threads for Win32 をダウンードして下さい。

http://sourceforge.net/projects/mingw/files/

必要なファイルはlibpthread-2.8.0-3-mingw32-dll-2.tar.lzma と pthreads-w32-2.8.0-3-mingw32-dev.tar.lzma のふたつですので適切にインストールして下さい。

次に Gauche のソースコードを取得します。

% svn co https://gauche.svn.sourceforge.net/svnroot/gauche/Gauche/trunk Gauche
(中略)
Checked out revision 7075.

取得したソースコードに以下のパッチをあてます。

Index: src/Makefile.in
===================================================================
--- src/Makefile.in     (revision 7075)
+++ src/Makefile.in     (working copy)
@@ -188,7 +188,7 @@
        $(LINK) $(gosh_LDFLAGS) -o gosh$(EXEEXT) $(gosh_OBJECTS) $(gosh_LDADD) $(LIBS)
 
 relink :
-       $(RELINK) $(libgauche_LDFLAGS) $(LIBGAUCHE).$(SOEXT) $(libgauche_OBJECTS) ../gc/.libs/*.o $(LIBS)
+       $(RELINK) $(libgauche_LDFLAGS) $(LIBGAUCHE).$(SOEXT) $(libgauche_OBJECTS) ../gc/.libs/*.a $(LIBS)
        $(RELINK) $(gosh_LDFLAGS) -o gosh$(EXEEXT)  $(gosh_OBJECTS) $(gosh_LDADD) $(LIBS)
 
 $(OBJECTS) : $(HEADERS)
@@ -228,7 +228,7 @@
 paths.$(OBJEXT) : getdir_win.c getdir_dummy.c getdir_darwin.c
 
 $(LIBGAUCHE).$(SOEXT) : $(libgauche_OBJECTS) $(GCLIB)
-       $(LINK) $(libgauche_LDFLAGS) $(LIBGAUCHE).$(SOEXT) $(libgauche_OBJECTS) ../gc/.libs/*.o $(LIBS)
+       $(LINK) $(libgauche_LDFLAGS) $(LIBGAUCHE).$(SOEXT) $(libgauche_OBJECTS) ../gc/.libs/*.a $(LIBS)
        $(POSTBUILD)
 
 gauche-config$(EXEEXT) : gauche-config.$(OBJEXT)
@@ -299,7 +299,7 @@
            objlib.c compile.c autoloads.c builtin-syms.c \
            gauche/builtin-syms.h vminsn.c gauche/vminsn.h \
             ../lib/gauche/vm/insn.scm
-CONFIG_GENERATED = Makefile genconfig makeverslink gauche/config.h
+CONFIG_GENERATED = Makefile genconfig makeverslink config.h gauche/config.h
 
 clean :
        rm -rf core core.[0-9]* gosh$(EXEEXT) gauche-config$(EXEEXT) \
Index: src/mingw-dist.sh
===================================================================
--- src/mingw-dist.sh   (revision 7075)
+++ src/mingw-dist.sh   (working copy)
@@ -10,8 +10,8 @@
 # make sure we're going to use Mingw gcc
 case `gcc --version` in
   *mingw*) ;;
-  *) echo "Set PATH to have MinGW bin directory first"
-     exit 1;;
+#  *) echo "Set PATH to have MinGW bin directory first"
+#     exit 1;;
 esac
 
 # build
Index: src/system.c
===================================================================
--- src/system.c        (revision 7075)
+++ src/system.c        (working copy)
@@ -123,6 +123,26 @@
 #endif /* defined(MSVC) && defined(UNICODE) */
 
 /*===============================================================
+ * Windows specific - fakes for pthread.
+ */
+#if defined(GAUCHE_WINDOWS) && defined(GC_WIN32_PTHREADS)
+int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) {
+  switch (how) {
+   case SIG_BLOCK:
+   case SIG_UNBLOCK:
+   case SIG_SETMASK:
+    break;
+   default:
+    errno = EINVAL;
+    return -1;
+  }
+
+  errno = ENOSYS;
+  return -1;
+}
+#endif /* defined(GAUCHE_WINDOWS) && defined(GC_WIN32_PTHREADS) */
+
+/*===============================================================
  * OBSOLETED: Wrapper to the system call to handle signals.
  * Use SCM_SYSCALL_{I|P} macro instead.
  */
Index: src/gauche/pthread.h
===================================================================
--- src/gauche/pthread.h        (revision 7075)
+++ src/gauche/pthread.h        (working copy)
@@ -42,8 +42,13 @@
 
 /* Thread */
 typedef pthread_t ScmInternalThread;
+#ifdef __MINGW32__
+#define SCM_INTERNAL_THREAD_INIT(thr)   ((thr) = (pthread_t){.p=NULL, .x=0})
+#define SCM_INTERNAL_THREAD_INITIALIZED_P(thr)  ((thr.x) != 0)
+#else
 #define SCM_INTERNAL_THREAD_INIT(thr)   ((thr) = (pthread_t)NULL)
 #define SCM_INTERNAL_THREAD_INITIALIZED_P(thr)  ((thr) != (pthread_t)NULL)
+#endif
 
 /* Mutex */
 typedef pthread_mutex_t ScmInternalMutex;
Index: src/gauche/config.h.in
===================================================================
--- src/gauche/config.h.in      (revision 7075)
+++ src/gauche/config.h.in      (working copy)
@@ -61,6 +61,9 @@
 /* Define to use Win32 threads */
 #undef GC_WIN32_THREADS
 
+/* Define to use Win32 pthreads */
+#undef GC_WIN32_PTHREADS
+
 /* Define number of args gethostbyaddr_r takes */
 #undef GETHOSTBYADDR_R_NUMARGS
 
Index: configure.ac
===================================================================
--- configure.ac        (revision 7075)
+++ configure.ac        (working copy)
@@ -2,7 +2,7 @@
 AC_PREREQ(2.54)
 AC_INIT(Gauche, 0.9.1_pre1, shiro@acm.org)
 AC_CANONICAL_SYSTEM
-AC_CONFIG_HEADER(src/gauche/config.h ext/auxsys/auxsysconf.h)
+AC_CONFIG_HEADER(src/config.h src/gauche/config.h ext/auxsys/auxsysconf.h)
 
 # ABI_VERSION is set to major.minor of Gauche version by default.
 # It can be overridden by uncommenting the following line.
@@ -192,6 +192,12 @@
       THREADLIBS="-lpthread"
       GAUCHE_THREAD_TYPE=pthreads
       ;;
+    *-*-mingw*)
+      AC_DEFINE(GC_WIN32_PTHREADS,1,[Define to use Win32 pthreads])
+      THREADLIBS="-lpthread"
+      CFLAGS="$CFLAGS -mthreads"
+      GAUCHE_THREAD_TYPE=pthreads
+      ;;
     *-*-darwin*)
       AC_DEFINE(GC_DARWIN_THREADS,1,[Define to use Darwin threads])
       AC_DEFINE(GC_PTHREADS,1,[Define to use pthreads])
Index: gc/configure.ac
===================================================================
--- gc/configure.ac     (revision 7075)
+++ gc/configure.ac     (working copy)
@@ -177,6 +177,12 @@
         THREADDLLIBS=""
        win32_threads=true
        ;;
+     *-*-mingw*)
+       AC_DEFINE(GC_WIN32_PTHREADS)
+       AC_DEFINE(THREAD_LOCAL_ALLOC)
+       THREADDLLIBS="-lpthread"
+       win32_threads=true
+       ;;
      *-*-darwin*)
        AC_DEFINE(GC_DARWIN_THREADS)
        AC_DEFINE(THREAD_LOCAL_ALLOC)
@@ -381,6 +387,9 @@
  alpha-*-openbsd*)
      enable_shared=no
      ;;
+ *-*-mingw*)
+     enable_shared=no
+     ;;
  *)
      ;;
 esac

その後は通常の手順でビルドするだけです。

% cd Gauche
% ./DIST gen
% ./configure --prefix=/gauche --enable-threads=pthreads
% make
% make install
More ...