From b9699fe122b127e746fe2c0616d0c8999d7783f7 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Wed, 18 Oct 2017 07:47:30 -0700 Subject: [PATCH] Make alure-local aliases for promise/future stuff --- include/AL/alure2.h | 46 ++++++++++++++++++++++++++++------------------ src/context.cpp | 39 +++++++++++++++++---------------------- src/context.h | 10 +++++----- 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/include/AL/alure2.h b/include/AL/alure2.h index 6b7a8ff..b63b1df 100644 --- a/include/AL/alure2.h +++ b/include/AL/alure2.h @@ -115,6 +115,16 @@ constexpr inline UniquePtr MakeUnique(Args&&... args) #endif } +// A Promise/Future (+SharedFuture) implementation, defaults to C++11's +// std::promise, std::future, and std::shared_future. If this is changed, you +// must recompile the library. +template +using Promise = std::promise; +template +using Future = std::future; +template +using SharedFuture = std::shared_future; + // A Vector implementation, defaults to C++'s std::vector. If this is changed, // you must recompile the library. template @@ -697,17 +707,17 @@ public: /** * Asynchronously prepares a cached Buffer for the given audio file or * resource name. Multiple calls with the same name will return multiple - * shared futures for the same Buffer object. Once called, the buffer must + * SharedFutures for the same Buffer object. Once called, the buffer must * be freed using removeBuffer before destroying the context, even if you - * never get the Buffer from the shared_future. + * never get the Buffer from the SharedFuture. * - * The Buffer will be loaded asynchronously, and the caller gets back a - * shared_future immediately that can be checked later (or waited on) to - * get the actual Buffer when it's ready. The application must take care to - * handle exceptions from the shared_future in case of an unrecoverable - * error during the load. + * The Buffer will be scheduled to load asynchronously, and the caller gets + * back a SharedFuture that can be checked later (or waited on) to get the + * actual Buffer when it's ready. The application must take care to handle + * exceptions from the SharedFuture in case an unrecoverable error ocurred + * during the load. */ - std::shared_future getBufferAsync(const String &name); + SharedFuture getBufferAsync(const String &name); /** * Asynchronously prepares cached Buffers for the given audio file or @@ -740,20 +750,20 @@ public: * may alias an audio file, but it must not currently exist in the buffer * cache. Once called, the buffer must be freed using removeBuffer before * destroying the context, even if you never get the Buffer from the - * shared_future. + * SharedFuture. * - * The Buffer will be loaded asynchronously using the given decoder, and - * the caller gets back a shared_future immediately that can be checked - * later (or waited on) to get the actual Buffer when it's ready. The - * application must take care to handle exceptions from the shared_future - * in case of an unrecoverable error during the load. The decoder must not - * have its read or seek methods called while the buffer is not ready. + * The Buffer will be scheduled to load asynchronously, and the caller gets + * back a SharedFuture that can be checked later (or waited on) to get the + * actual Buffer when it's ready. The application must take care to handle + * exceptions from the SharedFuture in case an unrecoverable error ocurred + * during the load. The decoder must not have its read or seek methods + * called while the buffer is not ready. */ - std::shared_future createBufferAsyncFrom(const String &name, SharedPtr decoder); + SharedFuture createBufferAsyncFrom(const String &name, SharedPtr decoder); /** - * Deletes the cached Buffer object for the given audio file or - * resource name. The buffer must not be in use by a Source. + * Deletes the cached Buffer object for the given audio file or resource + * name. The buffer must not be in use by a Source. */ void removeBuffer(const String &name); /** diff --git a/src/context.cpp b/src/context.cpp index 120f71c..4f3bae1 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -742,7 +742,7 @@ BufferOrExceptT ContextImpl::doCreateBuffer(const String &name, Vectorget()); } -BufferOrExceptT ContextImpl::doCreateBufferAsync(const String &name, Vector>::iterator iter, SharedPtr decoder, std::promise promise) +BufferOrExceptT ContextImpl::doCreateBufferAsync(const String &name, Vector>::iterator iter, SharedPtr decoder, Promise promise) { BufferOrExceptT retval; ALuint srate = decoder->getFrequency(); @@ -803,8 +803,7 @@ Buffer ContextImpl::getBuffer(const String &name) iter = mFutureBuffers.begin(); while(iter != mFutureBuffers.end()) { - std::shared_future future = iter->second; - if(future.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) + if(iter->second.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) iter = mFutureBuffers.erase(iter); else ++iter; @@ -829,9 +828,9 @@ Buffer ContextImpl::getBuffer(const String &name) return *buffer; } -std::shared_future ContextImpl::getBufferAsync(const String &name) +SharedFuture ContextImpl::getBufferAsync(const String &name) { - std::shared_future future; + SharedFuture future; CheckContext(this); if(EXPECT(!mFutureBuffers.empty(), false)) @@ -845,8 +844,7 @@ std::shared_future ContextImpl::getBufferAsync(const String &name) iter = mFutureBuffers.begin(); while(iter != mFutureBuffers.end()) { - std::shared_future future = iter->second; - if(future.wait_for(std::chrono::milliseconds::zero()) == std::future_status::ready) + if(iter->second.wait_for(std::chrono::milliseconds::zero()) == std::future_status::ready) iter = mFutureBuffers.erase(iter); else ++iter; @@ -866,12 +864,12 @@ std::shared_future ContextImpl::getBufferAsync(const String &name) // User asked to create a future buffer that's already loaded. Just // construct a promise, fulfill the promise immediately, then return a // shared future that's already set. - std::promise promise; + Promise promise; promise.set_value(Buffer(iter->get())); return promise.get_future().share(); } - std::promise promise; + Promise promise; future = promise.get_future().share(); BufferOrExceptT ret = doCreateBufferAsync(name, iter, createDecoder(name), std::move(promise)); @@ -896,8 +894,7 @@ void ContextImpl::precacheBuffersAsync(ArrayView names) auto iter = mFutureBuffers.begin(); while(iter != mFutureBuffers.end()) { - std::shared_future future = iter->second; - if(future.wait_for(std::chrono::milliseconds::zero()) == std::future_status::ready) + if(iter->second.wait_for(std::chrono::milliseconds::zero()) == std::future_status::ready) iter = mFutureBuffers.erase(iter); else ++iter; @@ -922,8 +919,8 @@ void ContextImpl::precacheBuffersAsync(ArrayView names) SharedPtr *decoder = std::get_if>(&dec); if(!decoder) continue; - std::promise promise; - std::shared_future future = promise.get_future().share(); + Promise promise; + SharedFuture future = promise.get_future().share(); BufferOrExceptT buf = doCreateBufferAsync(name, iter, std::move(*decoder), std::move(promise)); @@ -954,9 +951,9 @@ Buffer ContextImpl::createBufferFrom(const String &name, SharedPtr deco return *buffer; } -std::shared_future ContextImpl::createBufferAsyncFrom(const String &name, SharedPtr decoder) +SharedFuture ContextImpl::createBufferAsyncFrom(const String &name, SharedPtr decoder) { - std::shared_future future; + SharedFuture future; CheckContext(this); if(EXPECT(!mFutureBuffers.empty(), false)) @@ -965,8 +962,7 @@ std::shared_future ContextImpl::createBufferAsyncFrom(const String &name auto iter = mFutureBuffers.begin(); while(iter != mFutureBuffers.end()) { - std::shared_future future = iter->second; - if(future.wait_for(std::chrono::milliseconds::zero()) == std::future_status::ready) + if(iter->second.wait_for(std::chrono::milliseconds::zero()) == std::future_status::ready) iter = mFutureBuffers.erase(iter); else ++iter; @@ -981,7 +977,7 @@ std::shared_future ContextImpl::createBufferAsyncFrom(const String &name if(iter != mBuffers.end() && (*iter)->getName() == name) throw std::runtime_error("Buffer \""+name+"\" already exists"); - std::promise promise; + Promise promise; future = promise.get_future().share(); BufferOrExceptT ret = doCreateBufferAsync(name, iter, std::move(decoder), std::move(promise)); @@ -1016,8 +1012,7 @@ void ContextImpl::removeBuffer(const String &name) iter = mFutureBuffers.begin(); while(iter != mFutureBuffers.end()) { - std::shared_future future = iter->second; - if(future.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) + if(iter->second.wait_for(std::chrono::milliseconds(0)) == std::future_status::ready) iter = mFutureBuffers.erase(iter); else ++iter; @@ -1310,10 +1305,10 @@ DECL_THUNK2(bool, Context, isSupported, const, ChannelConfig, SampleType) DECL_THUNK0(ArrayView, Context, getAvailableResamplers,) DECL_THUNK0(ALsizei, Context, getDefaultResamplerIndex, const) DECL_THUNK1(Buffer, Context, getBuffer,, const String&) -DECL_THUNK1(std::shared_future, Context, getBufferAsync,, const String&) +DECL_THUNK1(SharedFuture, Context, getBufferAsync,, const String&) DECL_THUNK1(void, Context, precacheBuffersAsync,, ArrayView) DECL_THUNK2(Buffer, Context, createBufferFrom,, const String&, SharedPtr) -DECL_THUNK2(std::shared_future, Context, createBufferAsyncFrom,, const String&, SharedPtr) +DECL_THUNK2(SharedFuture, Context, createBufferAsyncFrom,, const String&, SharedPtr) DECL_THUNK1(void, Context, removeBuffer,, const String&) DECL_THUNK1(void, Context, removeBuffer,, Buffer) DECL_THUNK0(Source, Context, createSource,) diff --git a/src/context.h b/src/context.h index 2ad598d..72550ca 100644 --- a/src/context.h +++ b/src/context.h @@ -130,7 +130,7 @@ private: std::stack mSourceIds; DeviceImpl *const mDevice; - std::unordered_map> mFutureBuffers; + std::unordered_map> mFutureBuffers; Vector> mBuffers; Vector> mSourceGroups; std::deque mAllSources; @@ -153,7 +153,7 @@ private: SharedPtr mDecoder; ALenum mFormat; ALuint mFrames; - std::promise mPromise; + Promise mPromise; ~PendingBuffer() { } }; @@ -174,7 +174,7 @@ private: DecoderOrExceptT findDecoder(const String &name); BufferOrExceptT doCreateBuffer(const String &name, Vector>::iterator iter, SharedPtr decoder); - BufferOrExceptT doCreateBufferAsync(const String &name, Vector>::iterator iter, SharedPtr decoder, std::promise promise); + BufferOrExceptT doCreateBufferAsync(const String &name, Vector>::iterator iter, SharedPtr decoder, Promise promise); bool mIsConnected : 1; bool mIsBatching : 1; @@ -280,10 +280,10 @@ public: ALsizei getDefaultResamplerIndex() const; Buffer getBuffer(const String &name); - std::shared_future getBufferAsync(const String &name); + SharedFuture getBufferAsync(const String &name); void precacheBuffersAsync(ArrayView names); Buffer createBufferFrom(const String &name, SharedPtr decoder); - std::shared_future createBufferAsyncFrom(const String &name, SharedPtr decoder); + SharedFuture createBufferAsyncFrom(const String &name, SharedPtr decoder); void removeBuffer(const String &name); void removeBuffer(Buffer buffer) { removeBuffer(buffer.getName()); } -- 2.11.4.GIT