Add ServiceQueue::wrap

This commit is contained in:
Vinnie Falco
2013-10-04 19:02:33 -07:00
parent 6c7f5d093c
commit 2c3ead339e
5 changed files with 471 additions and 3 deletions

View File

@@ -155,6 +155,8 @@
<ClInclude Include="..\..\beast\strings\StringFromNumber.h" />
<ClInclude Include="..\..\beast\Threads.h" />
<ClInclude Include="..\..\beast\Chrono.h" />
<ClInclude Include="..\..\beast\threads\detail\BindHandler.h" />
<ClInclude Include="..\..\beast\threads\detail\DispatchedHandler.h" />
<ClInclude Include="..\..\beast\threads\LockGuard.h" />
<ClInclude Include="..\..\beast\threads\RecursiveMutex.h" />
<ClInclude Include="..\..\beast\threads\ServiceQueue.h" />

View File

@@ -282,9 +282,6 @@
<Filter Include="beast\config\stdlib">
<UniqueIdentifier>{7243e5e5-ad7e-4d81-8444-d545919e850c}</UniqueIdentifier>
</Filter>
<Filter Include="beast\asio">
<UniqueIdentifier>{549430fc-36f6-450e-9d8d-38f4b9e72fb5}</UniqueIdentifier>
</Filter>
<Filter Include="beast\smart_ptr">
<UniqueIdentifier>{4e9c54da-1581-41d7-ac75-48140e4a13d4}</UniqueIdentifier>
</Filter>
@@ -294,6 +291,9 @@
<Filter Include="beast\threads\impl">
<UniqueIdentifier>{386a8cd8-6be3-4cac-9bca-7a01fdb5327a}</UniqueIdentifier>
</Filter>
<Filter Include="beast\threads\detail">
<UniqueIdentifier>{b116764e-1ad5-4854-a549-73c5beb5ae37}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\modules\beast_core\beast_core.h">
@@ -1230,6 +1230,12 @@
<ClInclude Include="..\..\beast\threads\Stoppable.h">
<Filter>beast\threads</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\threads\detail\DispatchedHandler.h">
<Filter>beast\threads\detail</Filter>
</ClInclude>
<ClInclude Include="..\..\beast\threads\detail\BindHandler.h">
<Filter>beast\threads\detail</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\modules\beast_core\containers\AbstractFifo.cpp">

View File

@@ -27,6 +27,8 @@
#include "ThreadLocalValue.h"
#include "WaitableEvent.h"
#include "detail/DispatchedHandler.h"
namespace beast {
namespace detail {
@@ -518,6 +520,15 @@ public:
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.

View File

@@ -0,0 +1,276 @@
//------------------------------------------------------------------------------
/*
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_BINDHANDLER_H_INCLUDED
#define BEAST_THREADS_BINDHANDLER_H_INCLUDED
namespace beast {
namespace detail {
/** Overloaded function that re-binds arguments to a handler. */
/** @{ */
template <class Handler, class P1>
class BindHandler1
{
private:
Handler handler;
P1 p1;
public:
BindHandler1 (Handler const& handler_, P1 const& p1_)
: handler (handler_)
, p1 (p1_)
{ }
BindHandler1 (Handler& handler_, P1 const& p1_)
: handler (BEAST_MOVE_CAST(Handler)(handler_))
, p1 (p1_)
{ }
void operator()()
{
handler (
static_cast <P1 const&> (p1)
);
}
void operator()() const
{
handler (p1);
}
};
template <class Handler, class P1>
BindHandler1 <Handler, P1> bindHandler (Handler handler, P1 const& p1)
{
return BindHandler1 <Handler, P1> (handler, p1);
}
//------------------------------------------------------------------------------
template <class Handler, class P1, class P2>
class BindHandler2
{
private:
Handler handler;
P1 p1; P2 p2;
public:
BindHandler2 (Handler const& handler_,
P1 const& p1_, P2 const& p2_)
: handler (handler_)
, p1 (p1_), p2 (p2_)
{ }
BindHandler2 (Handler& handler_,
P1 const& p1_, P2 const& p2_)
: handler (BEAST_MOVE_CAST(Handler)(handler_))
, p1 (p1_), p2 (p2_)
{ }
void operator()()
{
handler (
static_cast <P1 const&> (p1), static_cast <P2 const&> (p2));
}
void operator()() const
{ handler (p1, p2); }
};
template <class Handler, class P1, class P2>
BindHandler2 <Handler, P1, P2> bindHandler (Handler handler,
P1 const& p1, P2 const& p2)
{
return BindHandler2 <Handler, P1, P2> (
handler, p1, p2);
}
//------------------------------------------------------------------------------
template <class Handler, class P1, class P2, class P3>
class BindHandler3
{
private:
Handler handler;
P1 p1; P2 p2; P3 p3;
public:
BindHandler3 (Handler const& handler_,
P1 const& p1_, P2 const& p2_, P3 const& p3_)
: handler (handler_)
, p1 (p1_), p2 (p2_), p3 (p3_)
{ }
BindHandler3 (Handler& handler_,
P1 const& p1_, P2 const& p2_, P3 const& p3_)
: handler (BEAST_MOVE_CAST(Handler)(handler_))
, p1 (p1_), p2 (p2_), p3 (p3_)
{ }
void operator()()
{
handler (
static_cast <P1 const&> (p1), static_cast <P2 const&> (p2), static_cast <P3 const&> (p3));
}
void operator()() const
{ handler (p1, p2, p3); }
};
template <class Handler, class P1, class P2, class P3>
BindHandler3 <Handler, P1, P2, P3> bindHandler (Handler handler,
P1 const& p1, P2 const& p2, P3 const& p3)
{
return BindHandler3 <Handler, P1, P2, P3> (
handler, p1, p2, p3);
}
//------------------------------------------------------------------------------
template <class Handler, class P1, class P2, class P3, class P4>
class BindHandler4
{
private:
Handler handler;
P1 p1; P2 p2; P3 p3; P4 p4;
public:
BindHandler4 (Handler const& handler_,
P1 const& p1_, P2 const& p2_, P3 const& p3_, P4 const& p4_)
: handler (handler_)
, p1 (p1_), p2 (p2_), p3 (p3_), p4 (p4_)
{ }
BindHandler4 (Handler& handler_,
P1 const& p1_, P2 const& p2_, P3 const& p3_, P4 const& p4_)
: handler (BEAST_MOVE_CAST(Handler)(handler_))
, p1 (p1_), p2 (p2_), p3 (p3_), p4 (p4_)
{ }
void operator()()
{
handler (
static_cast <P1 const&> (p1), static_cast <P2 const&> (p2), static_cast <P3 const&> (p3),
static_cast <P4 const&> (p4)
);
}
void operator()() const
{ handler (p1, p2, p3, p4); }
};
template <class Handler, class P1, class P2, class P3, class P4>
BindHandler4 <Handler, P1, P2, P3, P4> bindHandler (Handler handler,
P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4)
{
return BindHandler4 <Handler, P1, P2, P3, P4> (
handler, p1, p2, p3, p4);
}
//------------------------------------------------------------------------------
template <class Handler, class P1, class P2, class P3, class P4, class P5>
class BindHandler5
{
private:
Handler handler;
P1 p1; P2 p2; P3 p3; P4 p4; P5 p5;
public:
BindHandler5 (Handler const& handler_,
P1 const& p1_, P2 const& p2_, P3 const& p3_, P4 const& p4_, P5 const& p5_)
: handler (handler_)
, p1 (p1_), p2 (p2_), p3 (p3_), p4 (p4_), p5 (p5_)
{ }
BindHandler5 (Handler& handler_,
P1 const& p1_, P2 const& p2_, P3 const& p3_, P4 const& p4_, P5 const& p5_)
: handler (BEAST_MOVE_CAST(Handler)(handler_))
, p1 (p1_), p2 (p2_), p3 (p3_), p4 (p4_), p5 (p5_)
{ }
void operator()()
{
handler (
static_cast <P1 const&> (p1), static_cast <P2 const&> (p2), static_cast <P3 const&> (p3),
static_cast <P4 const&> (p4), static_cast <P5 const&> (p5)
);
}
void operator()() const
{ handler (p1, p2, p3, p4, p5); }
};
template <class Handler, class P1, class P2, class P3, class P4, class P5>
BindHandler5 <Handler, P1, P2, P3, P4, P5> bindHandler (Handler handler,
P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4, P5 const& p5)
{
return BindHandler5 <Handler, P1, P2, P3, P4, P5> (
handler, p1, p2, p3, p4, p5);
}
//------------------------------------------------------------------------------
template <class Handler, class P1, class P2, class P3, class P4, class P5, class P6>
class BindHandler6
{
private:
Handler handler;
P1 p1; P2 p2; P3 p3; P4 p4; P5 p5; P6 p6;
public:
BindHandler6 (Handler const& handler_,
P1 const& p1_, P2 const& p2_, P3 const& p3_, P4 const& p4_, P5 const& p5_, P6 const& p6_)
: handler (handler_)
, p1 (p1_), p2 (p2_), p3 (p3_), p4 (p4_), p5 (p5_), p6 (p6_)
{ }
BindHandler6 (Handler& handler_,
P1 const& p1_, P2 const& p2_, P3 const& p3_, P4 const& p4_, P5 const& p5_, P6 const& p6_)
: handler (BEAST_MOVE_CAST(Handler)(handler_))
, p1 (p1_), p2 (p2_), p3 (p3_), p4 (p4_), p5 (p5_), p6 (p6_)
{ }
void operator()()
{
handler (
static_cast <P1 const&> (p1), static_cast <P2 const&> (p2), static_cast <P3 const&> (p3),
static_cast <P4 const&> (p4), static_cast <P5 const&> (p5), static_cast <P6 const&> (p6)
);
}
void operator()() const
{ handler (p1, p2, p3, p4, p5, p6); }
};
template <class Handler, class P1, class P2, class P3, class P4, class P5, class P6>
BindHandler6 <Handler, P1, P2, P3, P4, P5, P6> bindHandler (Handler handler,
P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4, P5 const& p5, P6 const& p6)
{
return BindHandler6 <Handler, P1, P2, P3, P4, P5, P6> (
handler, p1, p2, p3, p4, p5, p6);
}
/** @} */
}
}
#endif

View File

@@ -0,0 +1,173 @@
//------------------------------------------------------------------------------
/*
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_DISPATCHEDHANDLER_H_INCLUDED
#define BEAST_THREADS_DISPATCHEDHANDLER_H_INCLUDED
#include "BindHandler.h"
namespace beast {
namespace detail {
/** A wrapper that packages function call arguments into a dispatch. */
template <typename Dispatcher, typename Handler>
class DispatchedHandler
{
private:
Dispatcher m_dispatcher;
Handler m_handler;
public:
typedef void result_type;
DispatchedHandler (Dispatcher dispatcher, Handler& handler)
: m_dispatcher (dispatcher)
, m_handler (BEAST_MOVE_CAST(Handler)(handler))
{
}
#if BEAST_COMPILER_SUPPORTS_MOVE_SEMANTICS
DispatchedHandler (DispatchedHandler const& other)
: m_dispatcher (other.m_dispatcher)
, m_handler (other.m_handler)
{
}
DispatchedHandler (DispatchedHandler&& other)
: m_dispatcher (other.m_dispatcher)
, m_handler (BEAST_MOVE_CAST(Handler)(other.m_handler))
{
}
#endif
void operator()()
{
m_dispatcher.dispatch (m_handler);
}
void operator()() const
{
m_dispatcher.dispatch (m_handler);
}
template <class P1>
void operator() (P1 const& p1)
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1));
}
template <class P1>
void operator() (P1 const& p1) const
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1));
}
template <class P1, class P2>
void operator() (P1 const& p1, P2 const& p2)
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1, p2));
}
template <class P1, class P2>
void operator() (P1 const& p1, P2 const& p2) const
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1, p2));
}
template <class P1, class P2, class P3>
void operator() (P1 const& p1, P2 const& p2, P3 const& p3)
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1, p2, p3));
}
template <class P1, class P2, class P3>
void operator() (P1 const& p1, P2 const& p2, P3 const& p3) const
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1, p2, p3));
}
template <class P1, class P2, class P3, class P4>
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4)
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1, p2, p3, p4));
}
template <class P1, class P2, class P3, class P4>
void operator() (P1 const& p1, P2 const& p2, P3 const& p3, P4 const& p4) const
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1, p2, p3, p4));
}
template <class P1, class P2, class P3, class P4, class P5>
void operator() (P1 const& p1, P2 const& p2, P3 const& p3,
P4 const& p4, P5 const& p5)
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1, p2, p3, p4, p5));
}
template <class P1, class P2, class P3, class P4, class P5>
void operator() (P1 const& p1, P2 const& p2, P3 const& p3,
P4 const& p4, P5 const& p5) const
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1, p2, p3, p4, p5));
}
template <class P1, class P2, class P3, class P4, class P5, class P6>
void operator() (P1 const& p1, P2 const& p2, P3 const& p3,
P4 const& p4, P5 const& p5, P6 const& p6)
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1, p2, p3, p4, p5, p6));
}
template <class P1, class P2, class P3, class P4, class P5, class P6>
void operator() (P1 const& p1, P2 const& p2, P3 const& p3,
P4 const& p4, P5 const& p5, P6 const& p6) const
{
m_dispatcher.dispatch (
detail::bindHandler (m_handler,
p1, p2, p3, p4, p5, p6));
}
};
}
}
#endif