pthread/mutex: add an option to support both unsafe and robust mutexes
This patch adds an option to support both unsafe and robust mutexes via
pthread_mutexattr_get/setrobust(). All credits should go to Gregory Nutt
who wrote the original commit.
[Song: backported from NuttX]
@@ -114,6 +114,13 @@ int pthread_mutex_init(FAR pthread_mutex_t *mutex,
#endif
#ifdef CONFIG_PRIORITY_INHERITANCE
uint8_t proto = PTHREAD_PRIO_INHERIT;
+#endif
+#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
+#ifdef CONFIG_PTHREAD_MUTEX_DEFAULT_UNSAFE
+ uint8_t robust = PTHREAD_MUTEX_STALLED;
+#else
+ uint8_t robust = PTHREAD_MUTEX_ROBUST;
+#endif
#endif
int ret = OK;
int status;
@@ -132,6 +139,9 @@ int pthread_mutex_init(FAR pthread_mutex_t *mutex,
#endif
#ifdef CONFIG_MUTEX_TYPES
type = attr->type;
+#endif
+#ifdef CONFIG_PTHREAD_MUTEX_BOTH
+ robust = attr->robust;
#endif
}
@@ -159,7 +169,7 @@ int pthread_mutex_init(FAR pthread_mutex_t *mutex,
/* Initial internal fields of the mutex */
mutex->flink = NULL;
- mutex->flags = 0;
+ mutex->flags = (robust == PTHREAD_MUTEX_ROBUST ? _PTHREAD_MFLAGS_ROBUST : 0);
#endif
#ifdef CONFIG_MUTEX_TYPES
