From 6ed3918317cb9b20ece88a46daaccc52d974b7bd Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 4 May 2015 11:16:28 +0200 Subject: Replace boost by C++11 std::thread doesn't support interruption like boost::thread, which was used in the ALSA input. Everything else should be equivalent. --- src/AlsaInput.cpp | 2 +- src/AlsaInput.h | 10 +++++----- src/SampleQueue.h | 20 +++++++++++--------- src/VLCInput.cpp | 39 ++++++++++++++++++++------------------- src/VLCInput.h | 5 +++-- 5 files changed, 40 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/AlsaInput.cpp b/src/AlsaInput.cpp index 4b0da93..492ff62 100644 --- a/src/AlsaInput.cpp +++ b/src/AlsaInput.cpp @@ -128,7 +128,7 @@ void AlsaInputThreaded::start() } else { m_running = true; - m_thread = boost::thread(&AlsaInputThreaded::process, this); + m_thread = std::thread(&AlsaInputThreaded::process, this); } } diff --git a/src/AlsaInput.h b/src/AlsaInput.h index bd3a800..4f63ab5 100644 --- a/src/AlsaInput.h +++ b/src/AlsaInput.h @@ -21,9 +21,10 @@ #define __ALSA_H_ #include #include +#include +#include #include -#include #include "SampleQueue.h" @@ -107,7 +108,6 @@ class AlsaInputThreaded : public AlsaInput { if (m_running) { m_running = false; - m_thread.interrupt(); m_thread.join(); } } @@ -124,9 +124,9 @@ class AlsaInputThreaded : public AlsaInput void process(); - bool m_fault; - bool m_running; - boost::thread m_thread; + std::atomic m_fault; + std::atomic m_running; + std::thread m_thread; SampleQueue& m_queue; diff --git a/src/SampleQueue.h b/src/SampleQueue.h index 778526e..2df1934 100644 --- a/src/SampleQueue.h +++ b/src/SampleQueue.h @@ -1,8 +1,8 @@ /* - Copyright (C) 2013, 2014 + Copyright (C) 2013, 2014, 2015 Matthias P. Braendli, matthias.braendli@mpb.li - An implementation for a threadsafe queue using boost thread library + An implementation for a threadsafe queue using the C++11 thread library for audio samples. */ @@ -11,10 +11,12 @@ #define DEBUG_SAMPLE_QUEUE 0 -#include +#include #include - -#include +#include +#include +#include +#include /* This queue is meant to be used by two threads. One producer * that pushes elements into the queue, and one consumer that @@ -53,7 +55,7 @@ public: /* Push a bunch of samples into the buffer */ size_t push(const T *val, size_t len) { - boost::mutex::scoped_lock lock(m_mutex); + std::lock_guard lock(m_mutex); assert(len % (m_channels * m_bytes_per_sample) == 0); @@ -81,7 +83,7 @@ public: size_t size() const { - boost::mutex::scoped_lock lock(m_mutex); + std::lock_guard lock(m_mutex); return m_queue.size(); } @@ -103,7 +105,7 @@ public: */ size_t pop(T* buf, size_t len, size_t* overruns) { - boost::mutex::scoped_lock lock(m_mutex); + std::lock_guard lock(m_mutex); assert(len % (m_channels * m_bytes_per_sample) == 0); @@ -163,7 +165,7 @@ public: private: std::deque m_queue; - mutable boost::mutex m_mutex; + mutable std::mutex m_mutex; unsigned int m_channels; unsigned int m_bytes_per_sample; diff --git a/src/VLCInput.cpp b/src/VLCInput.cpp index d7b691f..5115c53 100644 --- a/src/VLCInput.cpp +++ b/src/VLCInput.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "VLCInput.h" @@ -26,7 +27,6 @@ #if HAVE_VLC #include -#include using namespace std; @@ -124,7 +124,7 @@ int VLCInput::prepare() for (int timeout = 0; timeout < 100; timeout++) { st = libvlc_media_get_state(media); - boost::this_thread::sleep(boost::posix_time::milliseconds(10)); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); if (st != libvlc_NothingSpecial) { ret = 0; break; @@ -140,22 +140,23 @@ void VLCInput::preRender_cb(uint8_t** pp_pcm_buffer, size_t size) const size_t max_length = 20 * size; for (;;) { - boost::mutex::scoped_lock lock(m_queue_mutex); + { + std::lock_guard lock(m_queue_mutex); - if (m_queue.size() < max_length) { - m_current_buf.resize(size); - *pp_pcm_buffer = &m_current_buf[0]; - return; + if (m_queue.size() < max_length) { + m_current_buf.resize(size); + *pp_pcm_buffer = &m_current_buf[0]; + return; + } } - lock.unlock(); - boost::this_thread::sleep(boost::posix_time::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } void VLCInput::exit_cb() { - boost::mutex::scoped_lock lock(m_queue_mutex); + std::lock_guard lock(m_queue_mutex); fprintf(stderr, "VLC exit, restarting...\n"); @@ -182,7 +183,7 @@ void VLCInput::cleanup() void VLCInput::postRender_cb() { - boost::mutex::scoped_lock lock(m_queue_mutex); + std::lock_guard lock(m_queue_mutex); size_t queue_size = m_queue.size(); m_queue.resize(m_queue.size() + m_current_buf.size()); @@ -194,18 +195,18 @@ ssize_t VLCInput::m_read(uint8_t* buf, size_t length) { ssize_t err = 0; for (;;) { - boost::mutex::scoped_lock lock(m_queue_mutex); + { + std::lock_guard lock(m_queue_mutex); - if (m_queue.size() >= length) { - std::copy(m_queue.begin(), m_queue.begin() + length, buf); + if (m_queue.size() >= length) { + std::copy(m_queue.begin(), m_queue.begin() + length, buf); - m_queue.erase(m_queue.begin(), m_queue.begin() + length); + m_queue.erase(m_queue.begin(), m_queue.begin() + length); - return length; + return length; + } } - - lock.unlock(); - boost::this_thread::sleep(boost::posix_time::milliseconds(1)); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); libvlc_media_t *media = libvlc_media_player_get_media(m_mp); libvlc_state_t st = libvlc_media_get_state(media); diff --git a/src/VLCInput.h b/src/VLCInput.h index ec7c80e..048c931 100644 --- a/src/VLCInput.h +++ b/src/VLCInput.h @@ -27,9 +27,10 @@ #include #include #include +#include +#include #include -#include #include "SampleQueue.h" @@ -93,7 +94,7 @@ class VLCInput std::vector m_current_buf; - boost::mutex m_queue_mutex; + mutable std::mutex m_queue_mutex; std::deque m_queue; std::string m_uri; -- cgit v1.2.3