[vlc-devel] [PATCH 1/6] cxx helpers: Add wrappers for mutex/cond/sem
Hugo Beauzée-Luyssen
hugo at beauzee.fr
Mon Aug 20 14:38:15 CEST 2018
I'm not sure if this belongs more into cxx_helpers.hpp or in
vlc_threads.h
vlc_threads.h would prevent checking for VLC_THREADS_H_, which is a bit
strange, however we don't want to force vlc_threads.h to be included to
allow vlc_cxx_helpers to be used
vlc_cxx_helpers.hpp on the other hand feels like the correct place to
add C++ helpers.
---
include/vlc_cxx_helpers.hpp | 140 ++++++++++++++++++++++++++++++++++++
1 file changed, 140 insertions(+)
diff --git a/include/vlc_cxx_helpers.hpp b/include/vlc_cxx_helpers.hpp
index 425ae878b3..6b6381eed0 100644
--- a/include/vlc_cxx_helpers.hpp
+++ b/include/vlc_cxx_helpers.hpp
@@ -115,6 +115,146 @@ inline std::unique_ptr<T[], void (*)(void*)> wrap_carray( T* ptr ) noexcept
} // anonymous namespace
+// Don't require pre-inclusion of vlc_threads.h:
+#if defined(VLC_THREADS_H_)
+
+namespace threads
+{
+
+class mutex
+{
+public:
+ mutex() noexcept
+ {
+ vlc_mutex_init( &m_mutex );
+ }
+ ~mutex()
+ {
+ vlc_mutex_destroy( &m_mutex );
+ }
+
+ mutex( const mutex& ) = delete;
+ mutex& operator=( const mutex& ) = delete;
+ mutex( mutex&& ) = delete;
+ mutex& operator=( mutex&& ) = delete;
+
+ void lock() noexcept
+ {
+ vlc_mutex_lock( &m_mutex );
+ }
+ void unlock() noexcept
+ {
+ vlc_mutex_unlock( &m_mutex );
+ }
+
+private:
+ vlc_mutex_t m_mutex;
+ friend class condition_variable;
+ friend class mutex_locker;
+};
+
+class condition_variable
+{
+public:
+ condition_variable() noexcept
+ {
+ vlc_cond_init( &m_cond );
+ }
+ ~condition_variable()
+ {
+ vlc_cond_destroy( &m_cond );
+ }
+ void signal() noexcept
+ {
+ vlc_cond_signal( &m_cond );
+ }
+ void broadcast() noexcept
+ {
+ vlc_cond_broadcast( &m_cond );
+ }
+ void wait( mutex& mutex ) noexcept
+ {
+ vlc_cond_wait( &m_cond, &mutex.m_mutex );
+ }
+ int timedwait( mutex& mutex, vlc_tick_t deadline ) noexcept
+ {
+ return vlc_cond_timedwait( &m_cond, &mutex.m_mutex, deadline );
+ }
+
+private:
+ vlc_cond_t m_cond;
+};
+
+class mutex_locker
+{
+public:
+ mutex_locker( vlc_mutex_t* m ) noexcept
+ : m_mutex( m )
+ {
+ vlc_mutex_lock( m_mutex );
+ }
+ mutex_locker( mutex& m ) noexcept
+ : mutex_locker( &m.m_mutex )
+ {
+ }
+ ~mutex_locker()
+ {
+ vlc_mutex_unlock( m_mutex );
+ }
+ mutex_locker( const mutex_locker& ) = delete;
+ mutex_locker& operator=( const mutex_locker& ) = delete;
+ mutex_locker( mutex_locker&& ) = delete;
+ mutex_locker& operator=( mutex_locker&& ) = delete;
+
+private:
+ vlc_mutex_t* m_mutex;
+};
+
+class semaphore
+{
+public:
+ semaphore() noexcept
+ {
+ vlc_sem_init( &m_sem, 0 );
+ }
+ semaphore( unsigned int count ) noexcept
+ {
+ vlc_sem_init( &m_sem, count );
+ }
+ ~semaphore()
+ {
+ vlc_sem_destroy( &m_sem );
+ }
+
+ semaphore( const semaphore& ) = delete;
+ semaphore& operator=( const semaphore& ) = delete;
+ semaphore( semaphore&& ) = delete;
+ semaphore& operator=( semaphore&& ) = delete;
+
+ int post() noexcept
+ {
+ return vlc_sem_post( &m_sem );
+ }
+ void wait() noexcept
+ {
+ vlc_sem_wait( &m_sem );
+ }
+
+#ifdef VLC_INTERRUPT_H
+ int wait_i11e() noexcept
+ {
+ return vlc_sem_wait_i11e( &m_sem );
+ }
+#endif
+
+private:
+ vlc_sem_t m_sem;
+};
+
+}
+
+#endif
+
} // namespace vlc
#endif
--
2.18.0
More information about the vlc-devel
mailing list