]> git.sesse.net Git - vlc/commitdiff
vlc_mutex_locker: automatic mutex locking for C++
authorRémi Denis-Courmont <rdenis@simphalempin.com>
Mon, 20 Oct 2008 19:45:16 +0000 (22:45 +0300)
committerRémi Denis-Courmont <rdenis@simphalempin.com>
Mon, 20 Oct 2008 20:15:51 +0000 (23:15 +0300)
include/vlc_threads.h

index 4ab92c2d03f26c9f2094a3f465d9ba62df811f36..7b66e5b07cbee6dd6f90be6f2258f3ae84ae098a 100644 (file)
@@ -448,4 +448,27 @@ static inline void barrier (void)
 #define vlc_thread_join( P_THIS )                                           \
     __vlc_thread_join( VLC_OBJECT(P_THIS) )
 
+#ifdef __cplusplus
+/**
+ * Helper C++ class to lock a mutex.
+ * The mutex is locked when the object is created, and unlocked when the object
+ * is destroyed.
+ */
+class vlc_mutex_locker
+{
+    private:
+        vlc_mutex_t *lock;
+    public:
+        vlc_mutex_locker (vlc_mutex_t *m) : lock (m)
+        {
+            vlc_mutex_lock (lock);
+        }
+
+        ~vlc_mutex_locker (void)
+        {
+            vlc_mutex_unlock (lock);
+        }
+};
+#endif
+
 #endif /* !_VLC_THREADS_H */