c11/threads: create mutexattrs only when needed
authorEmil Velikov <emil.velikov@collabora.com>
Sun, 24 Apr 2016 15:14:04 +0000 (16:14 +0100)
committerEmil Velikov <emil.l.velikov@gmail.com>
Mon, 23 May 2016 11:07:45 +0000 (12:07 +0100)
If the mutexattrs are the default one can just pass NULL to
pthread_mutex_init. As the compiler does not know this detail it
unnecessarily creates/destroys the attrs.

Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
include/c11/threads_posix.h

index ce9853b18b3b1f969e6e2aafcaafb3401d70ac29..11d36e462399161e4a0614d4dc833928f34aa6cb 100644 (file)
@@ -180,9 +180,14 @@ mtx_init(mtx_t *mtx, int type)
       && type != (mtx_timed|mtx_recursive)
       && type != (mtx_try|mtx_recursive))
         return thrd_error;
+
+    if ((type & mtx_recursive) == 0) {
+        pthread_mutex_init(mtx, NULL);
+        return thrd_success;
+    }
+
     pthread_mutexattr_init(&attr);
-    if ((type & mtx_recursive) != 0)
-        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
     pthread_mutex_init(mtx, &attr);
     pthread_mutexattr_destroy(&attr);
     return thrd_success;