[PATCH] default to normal pthread mutexes

Yaakov Selkowitz yselkowitz@cygwin.com
Sun Jul 13 23:38:00 GMT 2014


Defaulting to ERRORCHECK mutexes (with the various stringencies it 
implies) does not match the behaviour on Linux, where NORMAL mutexes are 
the default.  I have been testing this locally for some time, and I 
believe it affects a lot of software.  Patch and STC attached.


Yaakov
-------------- next part --------------
2014-07-13  Yaakov Selkowitz  <yselkowitz@...>

	* thread.cc (pthread_mutex::pthread_mutex): Change default type
	to PTHREAD_MUTEX_NORMAL.
	(pthread_mutexattr::pthread_mutexattr): Ditto.
	(pthread_mutex_unlock): Do not fail if mutex is a normal mutex
	initializer.
	* include/pthread.h (PTHREAD_MUTEX_INITIALIZER): Redefine as
	PTHREAD_NORMAL_MUTEX_INITIALIZER_NP.

Index: thread.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/thread.cc,v
retrieving revision 1.287
diff -u -p -r1.287 thread.cc
--- thread.cc	1 Dec 2013 10:27:16 -0000	1.287
+++ thread.cc	26 Mar 2014 04:24:08 -0000
@@ -1708,7 +1708,7 @@ pthread_mutex::pthread_mutex (pthread_mu
   tid (0),
 #endif
   recursion_counter (0), condwaits (0),
-  type (PTHREAD_MUTEX_ERRORCHECK),
+  type (PTHREAD_MUTEX_NORMAL),
   pshared (PTHREAD_PROCESS_PRIVATE)
 {
   win32_obj_id = ::CreateEvent (&sec_none_nih, false, false, NULL);
@@ -1850,7 +1850,7 @@ pthread_mutex::_fixup_after_fork ()
 }
 
 pthread_mutexattr::pthread_mutexattr ():verifyable_object (PTHREAD_MUTEXATTR_MAGIC),
-pshared (PTHREAD_PROCESS_PRIVATE), mutextype (PTHREAD_MUTEX_ERRORCHECK)
+pshared (PTHREAD_PROCESS_PRIVATE), mutextype (PTHREAD_MUTEX_NORMAL)
 {
 }
 
@@ -3158,7 +3158,7 @@ extern "C" int
 pthread_mutex_unlock (pthread_mutex_t *mutex)
 {
   if (pthread_mutex::is_initializer (mutex))
-    return EPERM;
+    pthread_mutex::init (mutex, NULL, *mutex);
   if (!pthread_mutex::is_good_object (mutex))
     return EINVAL;
   return (*mutex)->unlock ();
Index: include/pthread.h
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/include/pthread.h,v
retrieving revision 1.39
diff -u -p -r1.39 pthread.h
--- include/pthread.h	26 Feb 2013 10:32:36 -0000	1.39
+++ include/pthread.h	26 Mar 2014 04:24:08 -0000
@@ -49,7 +49,7 @@ extern "C"
 #define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP (pthread_mutex_t)18
 #define PTHREAD_NORMAL_MUTEX_INITIALIZER_NP (pthread_mutex_t)19
 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP (pthread_mutex_t)20
-#define PTHREAD_MUTEX_INITIALIZER PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
+#define PTHREAD_MUTEX_INITIALIZER PTHREAD_NORMAL_MUTEX_INITIALIZER_NP
 #define PTHREAD_ONCE_INIT { PTHREAD_MUTEX_INITIALIZER, 0 }
 #if defined(_POSIX_THREAD_PRIO_INHERIT) && _POSIX_THREAD_PRIO_INHERIT >= 0
 #define PTHREAD_PRIO_NONE 0
-------------- next part --------------
#include <pthread.h>
#include <stdio.h>
#include <error.h>

int
main(void)
{
  int ret;
  pthread_mutexattr_t attr;
  pthread_mutex_t mutex;
#ifdef __CYGWIN__
  pthread_mutex_t mutexstatic = PTHREAD_NORMAL_MUTEX_INITIALIZER_NP;
#else
  pthread_mutex_t mutexstatic = PTHREAD_MUTEX_INITIALIZER;
#endif

  pthread_mutexattr_init(&attr);
  {
    int type;
    printf ("PTHREAD_MUTEX_NORMAL = %d\n", PTHREAD_MUTEX_NORMAL);
    printf ("PTHREAD_MUTEX_ERRORCHECK = %d\n", PTHREAD_MUTEX_ERRORCHECK);
    printf ("PTHREAD_MUTEX_DEFAULT = %d\n", PTHREAD_MUTEX_DEFAULT);

    pthread_mutexattr_gettype (&attr, &type);
    printf ("pthread_mutexattr_init(NULL) type is: %d\n", type);
  }
  pthread_mutex_init(&mutex, &attr);
  pthread_mutexattr_destroy(&attr);
  if ((ret = pthread_mutex_unlock(&mutex)))
    error(0, ret, "pthread_mutex_unlock(mutex)");

//  pthread_mutex_init(&mutexstatic, NULL);
  if ((ret = pthread_mutex_unlock(&mutexstatic)))
    error(0, ret, "pthread_mutex_unlock(mutexstatic)");
  return 0;
}


More information about the Cygwin-patches mailing list