mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
0e7bac9Fix include pathe5bb90fFix constness of Proxyac0142aUse template castef6e381Add missing Url.h include206e65cFix constness of operator[]695cc38Use template instantiation for friend declaration7b1e03aAdd BaseFromMember49bc04fMake List<>::Node not uncopyabled5954ffAdd Journal to UnitTest58da106Temporarily disable ServiceQueue dtor precondition assertsfe58c1aAdd missing #include2c02580Add PropertyStream for server state introspection24c2315Add ScopedWrapperContexta3845f5Add RelativeTime::value_type typedef7442932Fix missing PropertyStream membersed5a98fMore PropertyStream output for PeerFinderfcfa10dAdd PropertyStream3cf0729Tidy up AbstractHandler usage in HTTPClient55171f4Remove obsolete source files1311ca3Increase arity of SharedFunction67d807dAdd IPEndpoint::key_equalebf395eAdd ErrorCode and boost library2c3ead3Add ServiceQueue::wrap6c7f5d0Move many Thread related classes93e9d86Measure CPU utilization in ServiceQueueca47d72Move ServiceQueue, ThreadLocalValue, SpinLockc864e4dMove WaitableEventff305e6Add CPUMeter and ScopedTimeInterval01fd05cAdd RecursiveMutex, UnlockGuard, TryLockGuard5831a53Remove Journal from most Stoppable overridesb60a7f3Add Request and Response HTTP parsers44445ffRefactor net buffers classesac37c38Beast class refactor8b7056bFix eof on HTTP client get228b664Remove obsolete beast container classes1dfd655Use RelativeTime from startup in DeadlineTimerae22d5dAdd more methods to RelativeTimec67929eRemove unhandled exception catcher2472a90Add 64 bit output for MurmurHashf3d97c7Add RelativeTime::fromStartupb0b8660IPEndpoint better parsingae551cdAdd alternate form string parsing to IPEndpointd0a0dbfDon't break on Throw0e46762Add hasher functors for IPEndpointa1ec423Add Thread::stopThreadAsync4f7dca3Add compiler, stdlib, and platform skeleton to beast/config4394594Tidy up some use of Error for throwe5e0f52Journal console output improvementsf07515eAdd Stoppable prepare and start interfacesd37dd46Move RelativeTime to chrono, add ostream support3f6e7aaAdd console feature to Journalad0064aJournal option to write to Output window (MSVC)0b7574bAdd compilation test scriptcc05ce1Add ServiceQueuee132aabUse boost for functional when the config is set026b926Fix is_continuation for boost versionc807a4eFix invoked_type type reference2ff781bRemove LockFreeStack::size3acb474Add SharedData::ConstAccess7e4c834Add LockFreeStack::empty9c61a6dAdded AbstractHandler, WrapHandler. HTTPClient Fixes.94e40dcFix unittest, by removing recursive call.38bf408Fix nonstandard C++ extension in getNullSink1ef044dBuild fixesd5d3746Fix missing <cmath> include for Gentoo5f231d3Update copyright notice and licenses7b89bf6Add FixedArray, IntrusiveArray, Crypto5c5de57Reorganize beast modules and files9e18bb3Merge commit '43deaaa5cf0d0178a4a6c3cb69c02a2a9a43ec7d' as 'src/beast/beast/http/impl/http-parser'57703acFix BeforeBoost.h includefbc247bAdd Stoppable to beast56496d8IPEndpoint comparisons9d9c822Migrate some headers and general tidying1a3cddcAdd SharedArg and AsyncObject373ca9cAdd HTTPRequest and improvements to HTTPMessage parsing9534516Add some thread classes and fix SharedData with a simple mutex adapter755ab36Make CallQueue unit test runManualc0ca037Remove Beast version printing on startup7efb6a3Reorganize some MPL and Utility classes and files69c26a1Fix missing BeastConfig.h include in Net.cpp40aa552Disable Beast version printing in Ripple BeastConfig.h7b1352dAdd InterruptibleThread unit test68cf759ThreadWithCallQueue unit test adjustment6501deaIPEndpoint parsing and tidying72fc42bMove and add some template metaprogramming classes2a164f0Change filname capitalization (end)6a14f25Change filename capitalization92fd417Move integer types to beast/CStdInt.hebbd9ffMove TargetPlatform.h to beast/Config.h874b524Add IPEndpoint14b34fcTidy up some zlib macro undefines34fffcaRename beast sources for consistency4e59ab2Add CallQueue unit test327d7a6Fixes for consolidated beast unity includesd5ece4eRemove unused and broken classes39f13beRemove unused ConcurrentObject37624a7Add ThreadWithCallQueue unit teste82ec68Remove obsolete beast_Function90551a6Temporarily leave sqlite3 in whatever threading mode it was already in.43ebbb1Fix SharedSingleton to use memoryBarrierf343941Tidy up SharedSingleton doc comments001997eFix leak on exit from Singleton dependency cycle83b9d22Rename to DeadlineTimer::cancel()77874eeUse new instead of ::new for placement2a04dccJournal improvements50965caSharedFunction improvements277e32bAdd LockFreeStack iteratorsd94e4c2Fix undefined behavior in UnsignedIntegerCalc (again)2dc25ceFix DeadlineTimer, callback while holding lock207ffdeFix undefined behavior in UnsignedIntegerCalc1ad8ff9Fix UnsignedInteger::isZero1dd2836Add support for multiprecision integer arithmetic and binary data encodinga45fc47Update .gitignore962a95dTidy up UnsignedIntegerca695faAdd Time::isNull()e96ce99Better random number facilities in UnitTest550b8e5Fine tune UnsignedInteger declaration8e7e3b7Allow negative relative expirations in DeadlineTimerf3dc7ceAdd generic Journal class for loggingbfdda32Make ChildProcess UnitTest manual since it malfunctions02acf7dGeneral refactoring of beast framework classes84ef06eFix ExitHook to derive from AtExitHookf0acc9cReduce the max threads in the Workers unit test55447b0New SharedSingleton, resolves destruction of objects with static storage duration.41eb8a1Remove deprecated SharedPtr::getObject9eda4bcMake SharedObject members const, the counter mutable6eda777Remove deprecated createOnDemandOnce SingletonLifetime option8c522aaFix off by one in pending i/o count on HTTPClient057344eAdd HTTPMessage::toString and familyee728e3Add UniformResourceLocator::emptyae324fbMove ./modules to ./src git-subtree-dir: src/beast git-subtree-split:0e7bac945f
632 lines
17 KiB
C++
632 lines
17 KiB
C++
//------------------------------------------------------------------------------
|
|
/*
|
|
This file is part of Beast: https://github.com/vinniefalco/Beast
|
|
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#ifndef BEAST_THREADS_SERVICEQUEUE_H_INCLUDED
|
|
#define BEAST_THREADS_SERVICEQUEUE_H_INCLUDED
|
|
|
|
#include "../chrono/CPUMeter.h"
|
|
#include "../intrusive/List.h"
|
|
#include "../intrusive/LockFreeStack.h"
|
|
#include "SharedData.h"
|
|
#include "ThreadLocalValue.h"
|
|
#include "WaitableEvent.h"
|
|
|
|
#include "detail/DispatchedHandler.h"
|
|
|
|
namespace beast {
|
|
|
|
namespace detail {
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// VFALCO NOTE This allocator is a work in progress
|
|
|
|
#if 0
|
|
|
|
class ServiceQueueAllocatorArena : public SharedObject
|
|
{
|
|
public:
|
|
typedef std::size_t size_type;
|
|
|
|
class Page : public LockFreeStack <Page>::Node
|
|
{
|
|
public:
|
|
size_type const m_pageBytes;
|
|
Atomic <int> m_refs;
|
|
char* m_pos;
|
|
bool m_full;
|
|
size_type m_count;
|
|
|
|
static std::size_t overhead()
|
|
{ return sizeof (Page); }
|
|
|
|
static std::size_t pointer_overhead()
|
|
{ return sizeof (Page*); }
|
|
|
|
// pageBytes doesn't include the Page structure
|
|
explicit Page (size_type pageBytes)
|
|
: m_pageBytes (pageBytes)
|
|
, m_pos (begin())
|
|
, m_full (false)
|
|
, m_count (0)
|
|
{
|
|
}
|
|
|
|
~Page ()
|
|
{
|
|
// This means someone forgot to deallocate something
|
|
bassert (!m_full && m_refs.get() == 0);
|
|
}
|
|
|
|
static Page* create (size_type pageBytes)
|
|
{
|
|
return new (new uint8[pageBytes + overhead()]) Page (pageBytes);
|
|
}
|
|
|
|
static void destroy (Page* page)
|
|
{
|
|
page->~Page();
|
|
delete[] ((uint8*)page);
|
|
}
|
|
|
|
void reset ()
|
|
{
|
|
m_refs.set (0);
|
|
m_pos = begin();
|
|
m_full = false;
|
|
m_count = 0;
|
|
}
|
|
|
|
bool full() const
|
|
{
|
|
return m_full;
|
|
}
|
|
|
|
void* allocate (size_type n)
|
|
{
|
|
size_type const needed (n + pointer_overhead());
|
|
char* pos = m_pos + needed;
|
|
if (pos > end())
|
|
{
|
|
m_full = true;
|
|
return nullptr;
|
|
}
|
|
++m_refs;
|
|
void* p (m_pos + pointer_overhead());
|
|
get_page(p) = this;
|
|
m_pos = pos;
|
|
++m_count;
|
|
return p;
|
|
}
|
|
|
|
char* begin() const
|
|
{
|
|
return const_cast <char*> (
|
|
reinterpret_cast <char const*> (this) + overhead());
|
|
}
|
|
|
|
char const* end() const
|
|
{
|
|
return begin() + m_pageBytes;
|
|
}
|
|
|
|
// Returns true if the page can be recycled
|
|
bool deallocate (void* p, size_type)
|
|
{
|
|
bool const unused ((--m_refs) == 0);
|
|
return unused && m_full;
|
|
}
|
|
|
|
// Returns a reference to the per-allocation overhead area
|
|
static Page*& get_page (void* p)
|
|
{
|
|
return *reinterpret_cast <Page**>(
|
|
static_cast <char*>(p) - pointer_overhead());
|
|
}
|
|
};
|
|
|
|
struct State
|
|
{
|
|
State()
|
|
{
|
|
}
|
|
|
|
~State()
|
|
{
|
|
// If this goes off, someone forgot to call deallocate!
|
|
bassert (full.get() == 0);
|
|
|
|
destroy (active);
|
|
destroy (recycle);
|
|
}
|
|
|
|
void destroy (LockFreeStack <Page>& stack)
|
|
{
|
|
for(;;)
|
|
{
|
|
Page* const page (stack.pop_front());
|
|
if (page == nullptr)
|
|
break;
|
|
Page::destroy (page);
|
|
}
|
|
}
|
|
|
|
Atomic <int> full;
|
|
Atomic <int> page_count;
|
|
LockFreeStack <Page> active;
|
|
LockFreeStack <Page> recycle;
|
|
};
|
|
|
|
typedef SharedData <State> SharedState;
|
|
|
|
size_type const m_maxBytes;
|
|
SharedState m_state;
|
|
|
|
explicit ServiceQueueAllocatorArena (size_type maxBytes = 16 * 1024)
|
|
: m_maxBytes (maxBytes)
|
|
{
|
|
}
|
|
|
|
~ServiceQueueAllocatorArena()
|
|
{
|
|
}
|
|
|
|
void* allocate (size_type n)
|
|
{
|
|
SharedState::UnlockedAccess state (m_state);
|
|
|
|
// Loop until we satisfy the allocation from an
|
|
// active page, or we run out of active pages.
|
|
//
|
|
for (;;)
|
|
{
|
|
// Acquire ownership of an active page
|
|
// This prevents other threads from seeing it.
|
|
Page* page (state->active.pop_front());
|
|
if (page == nullptr)
|
|
break;
|
|
|
|
void* p = page->allocate (n);
|
|
if (p != nullptr)
|
|
{
|
|
// Put the page back so other threads can use it
|
|
state->active.push_front (page);
|
|
return p;
|
|
}
|
|
|
|
// Page is full, count it for diagnostics
|
|
++state->full;
|
|
}
|
|
|
|
// No active page, get a recycled page or create a new page.
|
|
//
|
|
Page* page (state->recycle.pop_front());
|
|
if (page == nullptr)
|
|
{
|
|
page = Page::create (std::max (m_maxBytes, n));
|
|
++state->page_count;
|
|
}
|
|
|
|
void* p = page->allocate (n);
|
|
bassert (p != nullptr);
|
|
// Throw page into the active list so other threads can use it
|
|
state->active.push_front (page);
|
|
return p;
|
|
}
|
|
|
|
void deallocate (void* p, size_type n)
|
|
{
|
|
SharedState::UnlockedAccess state (m_state);
|
|
Page* const page (Page::get_page(p));
|
|
if (page->deallocate (p, n))
|
|
{
|
|
--state->full;
|
|
page->reset();
|
|
Page::destroy (page);
|
|
//state->recycle.push_front (page);
|
|
}
|
|
}
|
|
};
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
struct ServiceQueueAllocator
|
|
{
|
|
typedef T value_type;
|
|
typedef T* pointer;
|
|
typedef T& reference;
|
|
typedef T const* const_pointer;
|
|
typedef T const& const_reference;
|
|
typedef std::size_t size_type;
|
|
typedef std::ptrdiff_t difference_type;
|
|
|
|
ServiceQueueAllocator ()
|
|
: m_arena (new ServiceQueueAllocatorArena)
|
|
{
|
|
}
|
|
|
|
ServiceQueueAllocator (ServiceQueueAllocator const& other)
|
|
: m_arena (other.m_arena)
|
|
{
|
|
}
|
|
|
|
template <typename U>
|
|
ServiceQueueAllocator (ServiceQueueAllocator <U> const& other)
|
|
: m_arena (other.m_arena)
|
|
{
|
|
}
|
|
|
|
template <typename U>
|
|
struct rebind
|
|
{
|
|
typedef ServiceQueueAllocator <U> other;
|
|
};
|
|
|
|
pointer address (reference x) const
|
|
{
|
|
return &x;
|
|
}
|
|
|
|
const_pointer address (const_reference x) const
|
|
{
|
|
return &x;
|
|
}
|
|
|
|
pointer allocate (size_type n,
|
|
std::allocator<void>::const_pointer = nullptr) const
|
|
{
|
|
size_type const bytes (n * sizeof (value_type));
|
|
pointer const p (static_cast <pointer> (
|
|
m_arena->allocate (bytes)));
|
|
return p;
|
|
}
|
|
|
|
void deallocate (pointer p, size_type n) const
|
|
{
|
|
size_type const bytes = (n * sizeof (value_type));
|
|
m_arena->deallocate (p, bytes);
|
|
}
|
|
|
|
size_type max_size () const
|
|
{
|
|
return std::numeric_limits <size_type>::max () / sizeof (value_type);
|
|
}
|
|
|
|
void construct (pointer p, const_reference val) const
|
|
{
|
|
new ((void *)p) value_type (val);
|
|
}
|
|
|
|
void destroy (pointer p) const
|
|
{
|
|
p->~value_type ();
|
|
}
|
|
|
|
private:
|
|
template <typename>
|
|
friend struct ServiceQueueAllocator;
|
|
|
|
SharedPtr <ServiceQueueAllocatorArena> m_arena;
|
|
};
|
|
|
|
#endif
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
class ServiceQueueBase
|
|
{
|
|
public:
|
|
ServiceQueueBase();
|
|
~ServiceQueueBase();
|
|
|
|
std::size_t poll();
|
|
std::size_t poll_one();
|
|
std::size_t run();
|
|
std::size_t run_one();
|
|
void stop();
|
|
bool stopped() const
|
|
{ return m_stopped.get() != 0; }
|
|
void reset();
|
|
|
|
protected:
|
|
class Item;
|
|
class Waiter;
|
|
class ScopedServiceThread;
|
|
|
|
void wait();
|
|
void enqueue (Item* item);
|
|
|
|
virtual std::size_t dequeue() = 0;
|
|
virtual Waiter* new_waiter() = 0;
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
class Item : public List <Item>::Node
|
|
{
|
|
public:
|
|
virtual ~Item() { }
|
|
virtual void operator()() = 0;
|
|
virtual std::size_t size() const = 0;
|
|
};
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
class Waiter : public LockFreeStack <Waiter>::Node
|
|
{
|
|
public:
|
|
Waiter()
|
|
{ }
|
|
void wait()
|
|
{ m_event.wait(); }
|
|
void signal()
|
|
{ m_event.signal(); }
|
|
private:
|
|
WaitableEvent m_event;
|
|
};
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
struct State
|
|
{
|
|
// handlers
|
|
List <Item> handlers;
|
|
LockFreeStack <Waiter> waiting;
|
|
LockFreeStack <Waiter> unused;
|
|
};
|
|
|
|
typedef SharedData <State> SharedState;
|
|
SharedState m_state;
|
|
CPUMeter m_cpuMeter;
|
|
Atomic <int> m_stopped;
|
|
|
|
static ThreadLocalValue <ServiceQueueBase*> s_service;
|
|
};
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
/** A queue for disatching function calls on other threads.
|
|
Handlers are guaranteed to be called only from threads that are currently
|
|
calling run, run_one, poll, or poll_one.
|
|
*/
|
|
template <class Allocator = std::allocator <char> >
|
|
class ServiceQueueType : public detail::ServiceQueueBase
|
|
{
|
|
private:
|
|
using ServiceQueueBase::Item;
|
|
using ServiceQueueBase::Waiter;
|
|
|
|
template <typename Handler>
|
|
class ItemType : public Item
|
|
{
|
|
public:
|
|
explicit ItemType (BEAST_MOVE_ARG(Handler) handler)
|
|
: m_handler (BEAST_MOVE_CAST(Handler)(handler))
|
|
{ }
|
|
void operator() ()
|
|
{ m_handler(); }
|
|
std::size_t size() const
|
|
{ return sizeof (*this); }
|
|
private:
|
|
Handler m_handler;
|
|
};
|
|
|
|
public:
|
|
typedef Allocator allocator_type; // for std::uses_allocator<>
|
|
|
|
explicit ServiceQueueType (int expectedConcurrency = 1,
|
|
Allocator alloc = Allocator())
|
|
: m_alloc (alloc)
|
|
{
|
|
typename Allocator::template rebind <Waiter>::other a (m_alloc);
|
|
SharedState::Access state (m_state);
|
|
while (expectedConcurrency--)
|
|
{
|
|
state->unused.push_front (new (a.allocate (1)) Waiter);
|
|
}
|
|
}
|
|
|
|
~ServiceQueueType()
|
|
{
|
|
SharedState::Access state (m_state);
|
|
|
|
// Must be empty
|
|
//bassert (state->handlers.empty());
|
|
|
|
// Cannot destroy while threads are waiting
|
|
bassert (state->waiting.empty());
|
|
|
|
typename Allocator::template rebind <Waiter>::other a (m_alloc);
|
|
for(;;)
|
|
{
|
|
Waiter* const waiter (state->unused.pop_front());
|
|
if (waiter == nullptr)
|
|
break;
|
|
a.destroy (waiter);
|
|
a.deallocate (waiter, 1);
|
|
}
|
|
}
|
|
|
|
/** Returns the percentage of time the queue is using the CPU. */
|
|
double getUtilizaton () const
|
|
{ return m_cpuMeter.getUtilizaton(); }
|
|
|
|
/** Returns the allocator associated with the container. */
|
|
allocator_type get_allocator() const
|
|
{
|
|
return m_alloc;
|
|
}
|
|
|
|
/** Returns `true` if the current thread is processing events.
|
|
If the current thread of execution is inside a call to run,
|
|
run_one, poll, or poll_one, this function returns `true`.
|
|
*/
|
|
bool is_service_thread() const
|
|
{ return s_service.get() == this; }
|
|
|
|
/** Run the handler on a service thread.
|
|
If the current thread of execution is a service thread then this
|
|
function wil dispatch the handler on the caller's thread before
|
|
returning.
|
|
The function signature of the handler must be:
|
|
@code
|
|
void handler();
|
|
@endcode
|
|
*/
|
|
template <typename Handler>
|
|
void dispatch (BEAST_MOVE_ARG(Handler) handler)
|
|
{
|
|
if (is_service_thread())
|
|
{
|
|
handler();
|
|
}
|
|
else
|
|
{
|
|
typename Allocator::template rebind <ItemType <Handler> >::other a (m_alloc);
|
|
enqueue (new (a.allocate (1))
|
|
ItemType <Handler> (BEAST_MOVE_CAST(Handler)(handler)));
|
|
}
|
|
}
|
|
|
|
/** Request the handler to run on a service thread.
|
|
This returns immediately, even if the current thread of execution is
|
|
a service thread.
|
|
The function signature of the handler must be:
|
|
@code
|
|
void handler();
|
|
@endcode
|
|
*/
|
|
template <typename Handler>
|
|
void post (BEAST_MOVE_ARG(Handler) handler)
|
|
{
|
|
typename Allocator::template rebind <ItemType <Handler> >::other a (m_alloc);
|
|
enqueue (new (a.allocate (1))
|
|
ItemType <Handler> (BEAST_MOVE_CAST(Handler)(handler)));
|
|
}
|
|
|
|
/** Return a new handler that dispatches the wrapped handler on the queue. */
|
|
template <typename Handler>
|
|
detail::DispatchedHandler <ServiceQueueType&, Handler> wrap (
|
|
BEAST_MOVE_ARG(Handler) handler)
|
|
{
|
|
return detail::DispatchedHandler <ServiceQueueType&, Handler> (
|
|
*this, BEAST_MOVE_CAST(Handler)(handler));
|
|
}
|
|
|
|
/** Run the event loop to execute ready handlers.
|
|
This runs handlers that are ready to run, without blocking, until
|
|
there are no more handlers ready or the service queue has been stopped.
|
|
@return The number of handlers that were executed.
|
|
*/
|
|
std::size_t poll ()
|
|
{ return ServiceQueueBase::poll(); }
|
|
|
|
/** Run the event loop to execute at most one ready handler.
|
|
This will run zero or one handlers, without blocking, depending on
|
|
whether or not there is handler immediately ready to run.
|
|
@return The number of handlers that were executed.
|
|
*/
|
|
std::size_t poll_one ()
|
|
{ return ServiceQueueBase::poll_one(); }
|
|
|
|
/** Runs the queue's processing loop.
|
|
The current thread of execution becomes a service thread. This call
|
|
blocks until there is no more work remaining.
|
|
@return The number of handlers that were executed.
|
|
*/
|
|
std::size_t run ()
|
|
{ return ServiceQueueBase::run(); }
|
|
|
|
/** Runs the queue's processing loop to execute at most one handler.
|
|
@return The number of handlers that were executed.
|
|
*/
|
|
std::size_t run_one ()
|
|
{ return ServiceQueueBase::run_one(); }
|
|
|
|
/** Stop the queue's processing loop.
|
|
All threads executing run or run_one will return as soon as possible.
|
|
Future calls to run, run_one, poll, or poll_one will return immediately
|
|
until reset is called.
|
|
@see reset
|
|
*/
|
|
void stop()
|
|
{ return ServiceQueueBase::stop(); }
|
|
|
|
/** Returns `true` if the queue has been stopped.
|
|
When a queue is stopped, calls to run, run_one, poll, or poll_one will
|
|
return immediately without invoking any handlers.
|
|
*/
|
|
bool stopped() const
|
|
{ return ServiceQueueBase::stopped(); }
|
|
|
|
/** Reset the queue after a stop.
|
|
This allows the event loop to be restarted. This may not be called while
|
|
there are any threads currently executing the run, run_one, poll, or
|
|
poll_one functions, or undefined behavior will result.
|
|
*/
|
|
void reset()
|
|
{ return ServiceQueueBase::reset(); }
|
|
|
|
private:
|
|
// Dispatch a single queued handler if possible.
|
|
// Returns the number of handlers dispatched (0 or 1)
|
|
//
|
|
std::size_t dequeue ()
|
|
{
|
|
if (stopped())
|
|
return 0;
|
|
|
|
Item* item (nullptr);
|
|
|
|
{
|
|
SharedState::Access state (m_state);
|
|
if (state->handlers.empty())
|
|
return 0;
|
|
item = &state->handlers.front();
|
|
state->handlers.erase (
|
|
state->handlers.iterator_to (*item));
|
|
}
|
|
|
|
(*item)();
|
|
|
|
typename Allocator::template rebind <uint8>::other a (m_alloc);
|
|
std::size_t const size (item->size());
|
|
item->~Item();
|
|
a.deallocate (reinterpret_cast<uint8*>(item), size);
|
|
return 1;
|
|
}
|
|
|
|
// Create a new Waiter
|
|
Waiter* new_waiter()
|
|
{
|
|
typename Allocator::template rebind <Waiter>::other a (m_alloc);
|
|
return new (a.allocate (1)) Waiter;
|
|
}
|
|
|
|
Allocator m_alloc;
|
|
};
|
|
|
|
typedef ServiceQueueType <std::allocator <char> > ServiceQueue;
|
|
|
|
}
|
|
|
|
#endif
|