From 583db88299bff9b95cd147e4340cf2f5227494d2 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 22 Oct 2016 21:58:45 -0400 Subject: [PATCH] Added a dispatch queue-powered Apple implementation of the async task queue, removed any mention of skip_samples in the AY since it isn't implemented. --- Components/AY38910/AY38910.cpp | 6 ------ Components/AY38910/AY38910.hpp | 1 - Concurrency/AsyncTaskQueue.cpp | 21 ++++++++++++++++++++- Concurrency/AsyncTaskQueue.hpp | 8 ++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Components/AY38910/AY38910.cpp b/Components/AY38910/AY38910.cpp index fc6c2fd3a..9e2aec75d 100644 --- a/Components/AY38910/AY38910.cpp +++ b/Components/AY38910/AY38910.cpp @@ -168,12 +168,6 @@ void AY38910::evaluate_output_volume() ); } -void AY38910::skip_samples(unsigned int number_of_samples) -{ - // TODO -// printf("Skip %d\n", number_of_samples); -} - void AY38910::select_register(uint8_t r) { _selected_register = r & 0xf; diff --git a/Components/AY38910/AY38910.hpp b/Components/AY38910/AY38910.hpp index 96d3724c1..82dc913af 100644 --- a/Components/AY38910/AY38910.hpp +++ b/Components/AY38910/AY38910.hpp @@ -49,7 +49,6 @@ class AY38910: public ::Outputs::Filter { // to satisfy ::Outputs::Speaker (included via ::Outputs::Filter; not for public consumption void get_samples(unsigned int number_of_samples, int16_t *target); - void skip_samples(unsigned int number_of_samples); private: int _selected_register; diff --git a/Concurrency/AsyncTaskQueue.cpp b/Concurrency/AsyncTaskQueue.cpp index 1aa544c5e..939cd016a 100644 --- a/Concurrency/AsyncTaskQueue.cpp +++ b/Concurrency/AsyncTaskQueue.cpp @@ -10,8 +10,14 @@ using namespace Concurrency; -AsyncTaskQueue::AsyncTaskQueue() : should_destruct_(false) +AsyncTaskQueue::AsyncTaskQueue() +#ifndef __APPLE__ + : should_destruct_(false) +#endif { +#ifdef __APPLE__ + serial_dispatch_queue_ = dispatch_queue_create("com.thomasharte.clocksignal.asyntaskqueue", DISPATCH_QUEUE_SERIAL); +#else thread_.reset(new std::thread([this]() { while(!should_destruct_) { @@ -39,25 +45,37 @@ AsyncTaskQueue::AsyncTaskQueue() : should_destruct_(false) } } })); +#endif } AsyncTaskQueue::~AsyncTaskQueue() { +#ifdef __APPLE__ + dispatch_release(serial_dispatch_queue_); +#else should_destruct_ = true; enqueue([](){}); thread_->join(); thread_.reset(); +#endif } void AsyncTaskQueue::enqueue(std::function function) { +#ifdef __APPLE__ + dispatch_async(serial_dispatch_queue_, ^{function();}); +#else std::lock_guard lock(queue_mutex_); pending_tasks_.push_back(function); processing_condition_.notify_all(); +#endif } void AsyncTaskQueue::flush() { +#ifdef __APPLE__ + dispatch_sync(serial_dispatch_queue_, ^{}); +#else std::shared_ptr flush_mutex(new std::mutex); std::shared_ptr flush_condition(new std::condition_variable); std::unique_lock lock(*flush_mutex); @@ -66,4 +84,5 @@ void AsyncTaskQueue::flush() flush_condition->notify_all(); }); flush_condition->wait(lock); +#endif } diff --git a/Concurrency/AsyncTaskQueue.hpp b/Concurrency/AsyncTaskQueue.hpp index 77485265a..aa53dd954 100644 --- a/Concurrency/AsyncTaskQueue.hpp +++ b/Concurrency/AsyncTaskQueue.hpp @@ -14,6 +14,10 @@ #include #include +#ifdef __APPLE__ +#include +#endif + namespace Concurrency { /*! @@ -42,12 +46,16 @@ class AsyncTaskQueue { void flush(); private: +#ifdef __APPLE__ + dispatch_queue_t serial_dispatch_queue_; +#else std::unique_ptr thread_; std::mutex queue_mutex_; std::list> pending_tasks_; std::condition_variable processing_condition_; std::atomic_bool should_destruct_; +#endif }; }