bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] New modules nproc, pthread.


From: Paul Eggert
Subject: Re: [PATCH] New modules nproc, pthread.
Date: Tue, 13 Jul 2010 16:01:40 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.10) Gecko/20100527 Thunderbird/3.0.5

On 07/13/10 02:07, Pádraig Brady wrote:

> The coreutils build just failed on our integration server
> as it doesn't have <pthread.h>

Ah.  A dinosaur, or a purposeful torture-test?

> So currently the coreutils code would need more ifdefs
> which I would prefer not to do, or we could expand the
> emulation in pthread.h.in

The latter sounds much better, yes.  I installed the following into gnulib
and tested it by configuring coreutils with "./configure 
ac_cv_header_pthread_h=no".
Does it work for you?

>From 7f2ece896984db0787f92ce2d95462d41eef442f Mon Sep 17 00:00:00 2001
From: Paul R. Eggert <address@hidden>
Date: Tue, 13 Jul 2010 15:55:36 -0700
Subject: [PATCH] pthread: Add enough so that coreutils/src/sort.c compiles.

* lib/pthread.in.h: Add self to author comment.  Conditionalize on
_GL_PTHREAD_H, not PTHREAD_H_, for consistency with the rest of
gnulib. Include <sched.h> and <time.h>, as per POSIX.
Include <sys/types.h>, in case it defines pthread_t.
(pthread_t, pthread_attr_t, pthread_barrier_t, pthread_barrierattr_t):
(pthread_cond_t, pthread_condattr_t, pthread_key_t, pthread_mutex_t):
(pthread_mutexattr_t, pthread_once_t, pthread_rwlock_t):
(pthread_rwlockattr_t, pthread_spinlock_t):
New typedefs, if HAVE_PTHREAD_T is not defined.
(PTHREAD_COND_INITIALIZER, PTHREAD_MUTEX_INITIALIZER):
(PTHREAD_ONCE_INIT, PTHREAD_RWLOCK_INITIALIZER):
(PTHREAD_BARRIER_SERIAL_THREAD, PTHREAD_CANCEL_DEFERRED):
(PTHREAD_CANCEL_ASYNCHRONOUS, PTHREAD_CANCEL_ENABLE):
(PTHREAD_CANCEL_DISABLE, PTHREAD_CANCELED, PTHREAD_CREATE_JOINABLE):
(PTHREAD_CREATE_DETACHED, PTHREAD_INHERIT_SCHED):
(PTHREAD_EXPLICIT_SCHED, PTHREAD_MUTEX_DEFAULT, PTHREAD_MUTEX_NORMAL):
(PTHREAD_MUTEX_ERRORCHECK, PTHREAD_MUTEX_RECURSIVE):
(PTHREAD_MUTEX_STALLED, PTHREAD_MUTEX_ROBUST, PTHREAD_PRIO_NONE):
(PTHREAD_PRIO_INHERIT, PTHREAD_PRIO_PROTECT, PTHREAD_PROCESS_PRIVATE):
(PTHREAD_PROCESS_SHARED, PTHREAD_SCOPE_SYSTEM, PTHREAD_SCOPE_PROCESS):
New macros.
(pthread_cond_destroy, pthread_cond_init, pthread_cond_signal):
(pthread_cond_wait, pthread_exit, pthread_mutex_destroy):
(pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock):
(pthread_spin_init, pthread_spin_lock, pthread_spin_trylock);
(pthread_spin_unlock): New dummy functions.
(pthread_create): Return EAGAIN; don't set errno.
* m4/pthread.m4 (gl_PTHREAD_CHECK): Check for pthread_t, and
require AC_C_INLINE.
* modules/pthread (Depends-on): Add sched, time.
(pthread.h): Use AM_V_GEN.
---
 ChangeLog        |   35 ++++++++++
 lib/pthread.in.h |  198 +++++++++++++++++++++++++++++++++++++++++++++++++-----
 m4/pthread.m4    |    2 +
 modules/pthread  |    4 +-
 4 files changed, 221 insertions(+), 18 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3ffe6b5..b8c73c5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,38 @@
+2010-07-13  Paul R. Eggert  <address@hidden>
+
+       pthread: Add enough so that coreutils/src/sort.c compiles.
+       * lib/pthread.in.h: Add self to author comment.  Conditionalize on
+       _GL_PTHREAD_H, not PTHREAD_H_, for consistency with the rest of
+       gnulib. Include <sched.h> and <time.h>, as per POSIX.
+       Include <sys/types.h>, in case it defines pthread_t.
+       (pthread_t, pthread_attr_t, pthread_barrier_t, pthread_barrierattr_t):
+       (pthread_cond_t, pthread_condattr_t, pthread_key_t, pthread_mutex_t):
+       (pthread_mutexattr_t, pthread_once_t, pthread_rwlock_t):
+       (pthread_rwlockattr_t, pthread_spinlock_t):
+       New typedefs, if HAVE_PTHREAD_T is not defined.
+       (PTHREAD_COND_INITIALIZER, PTHREAD_MUTEX_INITIALIZER):
+       (PTHREAD_ONCE_INIT, PTHREAD_RWLOCK_INITIALIZER):
+       (PTHREAD_BARRIER_SERIAL_THREAD, PTHREAD_CANCEL_DEFERRED):
+       (PTHREAD_CANCEL_ASYNCHRONOUS, PTHREAD_CANCEL_ENABLE):
+       (PTHREAD_CANCEL_DISABLE, PTHREAD_CANCELED, PTHREAD_CREATE_JOINABLE):
+       (PTHREAD_CREATE_DETACHED, PTHREAD_INHERIT_SCHED):
+       (PTHREAD_EXPLICIT_SCHED, PTHREAD_MUTEX_DEFAULT, PTHREAD_MUTEX_NORMAL):
+       (PTHREAD_MUTEX_ERRORCHECK, PTHREAD_MUTEX_RECURSIVE):
+       (PTHREAD_MUTEX_STALLED, PTHREAD_MUTEX_ROBUST, PTHREAD_PRIO_NONE):
+       (PTHREAD_PRIO_INHERIT, PTHREAD_PRIO_PROTECT, PTHREAD_PROCESS_PRIVATE):
+       (PTHREAD_PROCESS_SHARED, PTHREAD_SCOPE_SYSTEM, PTHREAD_SCOPE_PROCESS):
+       New macros.
+       (pthread_cond_destroy, pthread_cond_init, pthread_cond_signal):
+       (pthread_cond_wait, pthread_exit, pthread_mutex_destroy):
+       (pthread_mutex_init, pthread_mutex_lock, pthread_mutex_unlock):
+       (pthread_spin_init, pthread_spin_lock, pthread_spin_trylock);
+       (pthread_spin_unlock): New dummy functions.
+       (pthread_create): Return EAGAIN; don't set errno.
+       * m4/pthread.m4 (gl_PTHREAD_CHECK): Check for pthread_t, and
+       require AC_C_INLINE.
+       * modules/pthread (Depends-on): Add sched, time.
+       (pthread.h): Use AM_V_GEN.
+
 2010-07-13  Bruno Haible  <address@hidden>
 
        striconveh: Don't malloc memory if the result buffer is sufficient.
diff --git a/lib/pthread.in.h b/lib/pthread.in.h
index 89fbc03..bb201a2 100644
--- a/lib/pthread.in.h
+++ b/lib/pthread.in.h
@@ -1,4 +1,4 @@
-/* Implement a trivial subset of the pthreads library.
+/* Implement a trivial subset of POSIX 1003.1-2008 pthread.h.
 
    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
 
@@ -16,31 +16,195 @@
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
-/* Written by Glen Lenker.  */
+/* Written by Paul Eggert and Glen Lenker.  */
 
-#ifndef PTHREAD_H_
-# define PTHREAD_H_
+#ifndef _GL_PTHREAD_H_
+#define _GL_PTHREAD_H_
 
-# include <errno.h>
-# include <stdlib.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <sched.h>
+#include <sys/types.h>
+#include <time.h>
 
-typedef int pthread_t;
-typedef int pthread_attr_t;
+#ifndef HAVE_PTHREAD_T
+ typedef int pthread_t;
+ typedef int pthread_attr_t;
+ typedef int pthread_barrier_t;
+ typedef int pthread_barrierattr_t;
+ typedef int pthread_cond_t;
+ typedef int pthread_condattr_t;
+ typedef int pthread_key_t;
+ typedef int pthread_mutex_t;
+ typedef int pthread_mutexattr_t;
+ typedef int pthread_once_t;
+ typedef int pthread_rwlock_t;
+ typedef int pthread_rwlockattr_t;
+ typedef int pthread_spinlock_t;
+#endif
 
-static int
+#define PTHREAD_COND_INITIALIZER { 0 }
+#define PTHREAD_MUTEX_INITIALIZER { 0 }
+#define PTHREAD_ONCE_INIT { 0 }
+#define PTHREAD_RWLOCK_INITIALIZER { 0 }
+
+#define PTHREAD_BARRIER_SERIAL_THREAD (-1)
+
+#define PTHREAD_CANCEL_DEFERRED 0
+#define PTHREAD_CANCEL_ASYNCHRONOUS 1
+
+#define PTHREAD_CANCEL_ENABLE 0
+#define PTHREAD_CANCEL_DISABLE 1
+
+#define PTHREAD_CANCELED ((void *) -1)
+
+#define PTHREAD_CREATE_JOINABLE 0
+#define PTHREAD_CREATE_DETACHED 1
+
+#define PTHREAD_INHERIT_SCHED 0
+#define PTHREAD_EXPLICIT_SCHED 1
+
+#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
+#define PTHREAD_MUTEX_NORMAL 0
+#define PTHREAD_MUTEX_ERRORCHECK 1
+#define PTHREAD_MUTEX_RECURSIVE 2
+
+#define PTHREAD_MUTEX_STALLED 0
+#define PTHREAD_MUTEX_ROBUST 1
+
+#define PTHREAD_PRIO_NONE 0
+#define PTHREAD_PRIO_INHERIT 1
+#define PTHREAD_PRIO_PROTECT 2
+
+#define PTHREAD_PROCESS_PRIVATE 0
+#define PTHREAD_PROCESS_SHARED 1
+
+#define PTHREAD_SCOPE_SYSTEM 0
+#define PTHREAD_SCOPE_PROCESS 1
+
+/* Provide substitutes for the thread functions that should work
+   adequately on a single-threaded implementation, where
+   pthread_create always fails.  The goal is to let programs compile
+   on non-pthread hosts with minimal runtime overhead.
+
+   Omit interfaces that have not been analyzed and for which we do not
+   know what to do, so that they elicit a compile-time error for
+   now.  */
+
+static inline int
+pthread_cond_destroy (pthread_cond_t *cond)
+{
+  /* COND is never seriously used.  */
+  return 0;
+}
+
+static inline int
+pthread_cond_init (pthread_cond_t *restrict cond,
+                  pthread_condattr_t const *restrict attr)
+{
+  /* COND is never seriously used.  */
+  return 0;
+}
+
+static inline int
+pthread_cond_signal (pthread_cond_t *cond)
+{
+  /* No threads can currently be blocked on COND.  */
+  return 0;
+}
+
+static inline int
+pthread_cond_wait (pthread_cond_t *restrict cond,
+                  pthread_mutex_t *restrict mutex)
+{
+  /* Properly-written applications never come here.  */
+  abort ();
+  return 0;
+}
+
+static inline int
 pthread_create (pthread_t *restrict thread,
-                const pthread_attr_t *restrict attr,
-                void *(*start_routine)(void*), void *restrict arg)
+                pthread_attr_t const *restrict attr,
+                void * (*start_routine) (void*), void *restrict arg)
 {
-  errno = EAGAIN;
-  return -1;
+  /* Do not create a thread.  */
+  return EAGAIN;
 }
 
-static int
-pthread_join (pthread_t thread, void **value_ptr)
+static inline void
+pthread_exit (void *value)
 {
+  /* There is just one thread, so the process exits.  */
+  exit (0);
+}
+
+static inline int
+pthread_join (pthread_t thread, void **pvalue)
+{
+  /* Properly-written applications never come here.  */
   abort ();
-  return -1;
+  return 0;
+}
+
+static inline int
+pthread_mutex_destroy (pthread_mutex_t *mutex)
+{
+  /* MUTEX is never seriously used.  */
+  return 0;
+}
+
+static inline int
+pthread_mutex_init (pthread_mutex_t *restrict mutex,
+                   pthread_mutexattr_t const *restrict attr)
+{
+  /* MUTEX is never seriously used.  */
+  return 0;
+}
+
+static inline int
+pthread_mutex_lock (pthread_mutex_t *mutex)
+{
+  /* There is only one thread, so it always gets the lock.  This
+     implementation does not support PTHREAD_MUTEX_ERRORCHECK.  */
+  return 0;
+}
+
+static inline int
+pthread_mutex_unlock (pthread_mutex_t *mutex)
+{
+  /* There is only one thread, so it always unlocks successfully.
+     This implementation does not support robust mutexes or
+     PTHREAD_MUTEX_ERRORCHECK.  */
+  return 0;
+}
+
+static inline int
+pthread_spin_init (pthread_spinlock_t *lock, int pshared)
+{
+  /* LOCK is never seriously used.  */
+  return 0;
+}
+
+static inline int
+pthread_spin_lock (pthread_spinlock_t *lock)
+{
+  /* Only one thread, so it always gets the lock.  */
+  return 0;
+}
+
+static inline int
+pthread_spin_trylock (pthread_spinlock_t *lock)
+{
+  /* Only one thread, so it always gets the lock.  Assume that a
+     thread never tries a lock that it already holds.  */
+  return 0;
+}
+
+static inline int
+pthread_spin_unlock (pthread_spinlock_t *lock)
+{
+  /* Only one thread, so spin locks are no-ops.  */
+  return 0;
 }
 
-#endif /* PTHREAD_H_ */
+#endif /* _GL_PTHREAD_H_ */
diff --git a/m4/pthread.m4 b/m4/pthread.m4
index 31d2b22..69866cb 100644
--- a/m4/pthread.m4
+++ b/m4/pthread.m4
@@ -17,11 +17,13 @@ AC_DEFUN([gl_PTHREAD_CHECK],
         fi])
      LIBS="$gl_saved_libs"
    else
+     AC_CHECK_TYPES([pthread_t])
      PTHREAD_H='pthread.h'
    fi
 
    AC_SUBST([LIB_PTHREAD])
    AC_SUBST([PTHREAD_H])
 
+   AC_REQUIRE([AC_C_INLINE])
    AC_REQUIRE([AC_C_RESTRICT])
 ])
diff --git a/modules/pthread b/modules/pthread
index 447dea5..9b930b1 100644
--- a/modules/pthread
+++ b/modules/pthread
@@ -6,6 +6,8 @@ lib/pthread.in.h
 m4/pthread.m4
 
 Depends-on:
+sched
+time
 
 configure.ac:
 gl_PTHREAD_CHECK
@@ -16,7 +18,7 @@ BUILT_SOURCES += $(PTHREAD_H)
 # We need the following in order to create <pthread.h> when the system
 # doesn't have one that works with the given compiler.
 pthread.h: pthread.in.h
-       ln -f pthread.in.h $@ || cp pthread.in.h $@
+       $(AM_V_GEN)ln -f pthread.in.h $@ || cp pthread.in.h $@
 MOSTLYCLEANFILES += pthread.h
 
 Include:
-- 
1.7.1





reply via email to

[Prev in Thread] Current Thread [Next in Thread]