Print this page
9959 pthread_mutex_init should initialize mutex appropriately for robust mutex_init
Reviewed by: Jason King <jason.king@joyent.com>
Reviewed by: Robert Mustacchi <rm@joyent.com>

@@ -224,10 +224,27 @@
         } else {
                 type = PTHREAD_PROCESS_PRIVATE | PTHREAD_MUTEX_DEFAULT |
                     PTHREAD_PRIO_NONE | PTHREAD_MUTEX_STALLED;
         }
 
+        /*
+         * POSIX mutexes (this interface) make no guarantee about the state of
+         * the mutex before pthread_mutex_init(3C) is called.  Sun mutexes, upon
+         * which these are built and which mutex_init(3C) below represents
+         * require that a robust mutex be initialized to all 0s _prior_ to
+         * mutex_init() being called, and that mutex_init() of an initialized
+         * mutex return EBUSY.
+         *
+         * We respect both these behaviors by zeroing the mutex here in the
+         * POSIX implementation if and only if the mutex magic is incorrect,
+         * and the mutex is robust.
+         */
+        if (((type & PTHREAD_MUTEX_ROBUST) != 0) &&
+            (((mutex_t *)mutex)->mutex_magic != MUTEX_MAGIC)) {
+                (void) memset(mutex, 0, sizeof (*mutex));
+        }
+
         return (mutex_init((mutex_t *)mutex, type, &prioceiling));
 }
 
 /*
  * pthread_mutex_setprioceiling: sets the prioceiling.