Browse Source

libobs/util: pthread_mutex_init_recursive helper

Add helper for creating a recursive mutex because it's easy to forget to
destroy the pthread_mutexattr_t.
jpark37 4 years ago
parent
commit
b9657f6239
1 changed files with 16 additions and 0 deletions
  1. 16 0
      libobs/util/threading.h

+ 16 - 0
libobs/util/threading.h

@@ -53,6 +53,22 @@ static inline void pthread_mutex_init_value(pthread_mutex_t *mutex)
 	*mutex = init_val;
 }
 
+static inline int pthread_mutex_init_recursive(pthread_mutex_t *mutex)
+{
+	pthread_mutexattr_t attr;
+	int ret = pthread_mutexattr_init(&attr);
+	if (ret == 0) {
+		ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+		if (ret == 0) {
+			ret = pthread_mutex_init(mutex, &attr);
+		}
+
+		pthread_mutexattr_destroy(&attr);
+	}
+
+	return ret;
+}
+
 enum os_event_type {
 	OS_EVENT_TYPE_AUTO,
 	OS_EVENT_TYPE_MANUAL,