+oc_mutex oc_mutex_new_recursive(void)
+{
+ oc_mutex retVal = NULL;
+ int ret = -1;
+
+ // Allocate new mutex.
+ oc_mutex_internal *mutexInfo = (oc_mutex_internal*) OICMalloc(sizeof(oc_mutex_internal));
+ if (NULL == mutexInfo)
+ {
+ OIC_LOG_V(ERROR, TAG, "%s Failed to allocate mutex!", __func__);
+ goto exit;
+ }
+
+ // Set up the mutex attributes.
+ pthread_mutexattr_t ma;
+ ret = pthread_mutexattr_init(&ma);
+ if (0 != ret)
+ {
+ OIC_LOG_V(ERROR, TAG, "%s Failed in pthread_mutexattr_init - error %d!",
+ __func__, ret);
+ goto exit;
+ }
+
+ ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE);
+ if (0 != ret)
+ {
+ OIC_LOG_V(ERROR, TAG, "%s Failed in pthread_mutexattr_settype - error %d!",
+ __func__, ret);
+ pthread_mutexattr_destroy(&ma);
+ goto exit;
+ }
+
+ // Initialize the mutex and destroy the attributes.
+ ret = pthread_mutex_init(&(mutexInfo->mutex), &ma);
+ OC_VERIFY(0 == pthread_mutexattr_destroy(&ma));
+ if (0 != ret)
+ {
+ OIC_LOG_V(ERROR, TAG, "%s Failed in pthread_mutex_init - error %d!",
+ __func__, ret);
+ goto exit;
+ }
+
+#ifndef NDEBUG
+ mutexInfo->owner = OC_INVALID_THREAD_ID;
+ mutexInfo->recursionCount = 0;
+#endif
+
+exit:
+ if (0 == ret)
+ {
+ retVal = (oc_mutex) mutexInfo;
+ }
+ else
+ {
+ OICFree(mutexInfo);
+ }
+
+ return retVal;
+}
+