Skip to content

Commit

Permalink
Rename scheduler_allocator to recycling_allocator.
Browse files Browse the repository at this point in the history
Changed to work across different scheduler types, where those scheduler
types all inherit from thread_context.
  • Loading branch information
chriskohlhoff committed Sep 20, 2014
1 parent 7fc4d71 commit 4e4aca0
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 77 deletions.
3 changes: 2 additions & 1 deletion asio/include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ nobase_include_HEADERS = \
asio/detail/reactor.hpp \
asio/detail/reactor_op.hpp \
asio/detail/reactor_op_queue.hpp \
asio/detail/recycling_allocator.hpp \
asio/detail/regex_fwd.hpp \
asio/detail/resolve_endpoint_op.hpp \
asio/detail/resolve_op.hpp \
asio/detail/resolver_service_base.hpp \
asio/detail/resolver_service.hpp \
asio/detail/scheduler_allocator.hpp \
asio/detail/scheduler.hpp \
asio/detail/scheduler_operation.hpp \
asio/detail/scheduler_thread_info.hpp \
Expand Down Expand Up @@ -207,6 +207,7 @@ nobase_include_HEADERS = \
asio/detail/std_thread.hpp \
asio/detail/strand_executor_service.hpp \
asio/detail/strand_service.hpp \
asio/detail/thread_context.hpp \
asio/detail/thread_group.hpp \
asio/detail/thread.hpp \
asio/detail/thread_info_base.hpp \
Expand Down
22 changes: 11 additions & 11 deletions asio/include/asio/detail/impl/scheduler.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ std::size_t scheduler::poll(asio::error_code& ec)
// that are already on a thread-private queue need to be put on to the main
// queue now.
if (one_thread_)
if (thread_info* outer_thread_info = ctx.next_by_key())
op_queue_.push(outer_thread_info->private_op_queue);
if (thread_info* outer_info = static_cast<thread_info*>(ctx.next_by_key()))
op_queue_.push(outer_info->private_op_queue);
#endif // defined(ASIO_HAS_THREADS)

std::size_t n = 0;
Expand Down Expand Up @@ -221,8 +221,8 @@ std::size_t scheduler::poll_one(asio::error_code& ec)
// that are already on a thread-private queue need to be put on to the main
// queue now.
if (one_thread_)
if (thread_info* outer_thread_info = ctx.next_by_key())
op_queue_.push(outer_thread_info->private_op_queue);
if (thread_info* outer_info = static_cast<thread_info*>(ctx.next_by_key()))
op_queue_.push(outer_info->private_op_queue);
#endif // defined(ASIO_HAS_THREADS)

return do_poll_one(lock, this_thread, ec);
Expand Down Expand Up @@ -252,10 +252,10 @@ void scheduler::post_immediate_completion(
#if defined(ASIO_HAS_THREADS)
if (one_thread_ || is_continuation)
{
if (thread_info* this_thread = thread_call_stack::contains(this))
if (thread_info_base* this_thread = thread_call_stack::contains(this))
{
++this_thread->private_outstanding_work;
this_thread->private_op_queue.push(op);
++static_cast<thread_info*>(this_thread)->private_outstanding_work;
static_cast<thread_info*>(this_thread)->private_op_queue.push(op);
return;
}
}
Expand All @@ -274,9 +274,9 @@ void scheduler::post_deferred_completion(scheduler::operation* op)
#if defined(ASIO_HAS_THREADS)
if (one_thread_)
{
if (thread_info* this_thread = thread_call_stack::contains(this))
if (thread_info_base* this_thread = thread_call_stack::contains(this))
{
this_thread->private_op_queue.push(op);
static_cast<thread_info*>(this_thread)->private_op_queue.push(op);
return;
}
}
Expand All @@ -295,9 +295,9 @@ void scheduler::post_deferred_completions(
#if defined(ASIO_HAS_THREADS)
if (one_thread_)
{
if (thread_info* this_thread = thread_call_stack::contains(this))
if (thread_info_base* this_thread = thread_call_stack::contains(this))
{
this_thread->private_op_queue.push(ops);
static_cast<thread_info*>(this_thread)->private_op_queue.push(ops);
return;
}
}
Expand Down
15 changes: 8 additions & 7 deletions asio/include/asio/detail/impl/strand_executor_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "asio/detail/call_stack.hpp"
#include "asio/detail/fenced_block.hpp"
#include "asio/detail/recycling_allocator.hpp"
#include "asio/executor_work.hpp"

#include "asio/detail/push_options.hpp"
Expand Down Expand Up @@ -63,7 +64,7 @@ class strand_executor_service::invoker
if (more_handlers)
{
Executor ex(this_->work_.get_executor());
scheduler_allocator<void> allocator;
recycling_allocator<void> allocator;
ex.post(ASIO_MOVE_CAST(invoker)(*this_), allocator);
}
}
Expand Down Expand Up @@ -110,8 +111,8 @@ void strand_executor_service::dispatch(const implementation_type& impl,
}

// Construct an allocator to be used for the operation.
typedef typename detail::get_scheduler_allocator<Allocator>::type alloc_type;
alloc_type allocator(detail::get_scheduler_allocator<Allocator>::get(a));
typedef typename detail::get_recycling_allocator<Allocator>::type alloc_type;
alloc_type allocator(detail::get_recycling_allocator<Allocator>::get(a));

// Allocate and construct an operation to wrap the function.
typedef executor_op<function_type, alloc_type> op;
Expand All @@ -138,8 +139,8 @@ void strand_executor_service::post(const implementation_type& impl,
function_type tmp(ASIO_MOVE_CAST(Function)(function));

// Construct an allocator to be used for the operation.
typedef typename detail::get_scheduler_allocator<Allocator>::type alloc_type;
alloc_type allocator(detail::get_scheduler_allocator<Allocator>::get(a));
typedef typename detail::get_recycling_allocator<Allocator>::type alloc_type;
alloc_type allocator(detail::get_recycling_allocator<Allocator>::get(a));

// Allocate and construct an operation to wrap the function.
typedef executor_op<function_type, alloc_type> op;
Expand All @@ -166,8 +167,8 @@ void strand_executor_service::defer(const implementation_type& impl,
function_type tmp(ASIO_MOVE_CAST(Function)(function));

// Construct an allocator to be used for the operation.
typedef typename detail::get_scheduler_allocator<Allocator>::type alloc_type;
alloc_type allocator(detail::get_scheduler_allocator<Allocator>::get(a));
typedef typename detail::get_recycling_allocator<Allocator>::type alloc_type;
alloc_type allocator(detail::get_recycling_allocator<Allocator>::get(a));

// Allocate and construct an operation to wrap the function.
typedef executor_op<function_type, alloc_type> op;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// detail/scheduler_allocator.hpp
// detail/recycling_allocator.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
Expand All @@ -8,89 +8,87 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef ASIO_DETAIL_SCHEDULER_ALLOCATOR_HPP
#define ASIO_DETAIL_SCHEDULER_ALLOCATOR_HPP
#ifndef ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP
#define ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

#include "asio/detail/config.hpp"
#include "asio/detail/call_stack.hpp"
#include "asio/detail/memory.hpp"
#include "asio/detail/scheduler_thread_info.hpp"
#include "asio/detail/thread_context.hpp"
#include "asio/detail/thread_info_base.hpp"

#include "asio/detail/push_options.hpp"

namespace asio {
namespace detail {

template <typename T = void>
class scheduler_allocator
template <typename T>
class recycling_allocator
{
public:
template <typename U>
struct rebind
{
typedef scheduler_allocator<U> other;
typedef recycling_allocator<T> other;
};

scheduler_allocator()
recycling_allocator()
{
}

template <typename U>
scheduler_allocator(const scheduler_allocator<U>&)
recycling_allocator(const recycling_allocator<U>&)
{
}

T* allocate(std::size_t n)
{
typedef scheduler_thread_info thread_info;
typedef call_stack<scheduler, thread_info> call_stack;
void* p = thread_info::allocate(call_stack::top(), sizeof(T) * n);
typedef thread_context::thread_call_stack call_stack;
void* p = thread_info_base::allocate(call_stack::top(), sizeof(T) * n);
return static_cast<T*>(p);
}

void deallocate(T* p, std::size_t n)
{
typedef scheduler_thread_info thread_info;
typedef call_stack<scheduler, thread_info> call_stack;
thread_info::deallocate(call_stack::top(), p, sizeof(T) * n);
typedef thread_context::thread_call_stack call_stack;
thread_info_base::deallocate(call_stack::top(), p, sizeof(T) * n);
}
};

template <>
class scheduler_allocator<void>
class recycling_allocator<void>
{
public:
template <typename U>
struct rebind
{
typedef scheduler_allocator<U> other;
typedef recycling_allocator<U> other;
};

scheduler_allocator()
recycling_allocator()
{
}

template <typename U>
scheduler_allocator(const scheduler_allocator<U>&)
recycling_allocator(const recycling_allocator<U>&)
{
}
};

template <typename Allocator>
struct get_scheduler_allocator
struct get_recycling_allocator
{
typedef Allocator type;
static type get(const Allocator& a) { return a; }
};

template <typename T>
struct get_scheduler_allocator<std::allocator<T> >
struct get_recycling_allocator<std::allocator<T> >
{
typedef scheduler_allocator<T> type;
typedef recycling_allocator<T> type;
static type get(const std::allocator<T>&) { return type(); }
};

Expand All @@ -99,4 +97,4 @@ struct get_scheduler_allocator<std::allocator<T> >

#include "asio/detail/pop_options.hpp"

#endif // ASIO_DETAIL_SCHEDULER_ALLOCATOR_HPP
#endif // ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP
8 changes: 3 additions & 5 deletions asio/include/asio/detail/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
#include "asio/error_code.hpp"
#include "asio/execution_context.hpp"
#include "asio/detail/atomic_count.hpp"
#include "asio/detail/call_stack.hpp"
#include "asio/detail/event.hpp"
#include "asio/detail/mutex.hpp"
#include "asio/detail/op_queue.hpp"
#include "asio/detail/reactor_fwd.hpp"
#include "asio/detail/scheduler_operation.hpp"
#include "asio/detail/thread_context.hpp"

#include "asio/detail/push_options.hpp"

Expand All @@ -37,7 +37,8 @@ namespace detail {
struct scheduler_thread_info;

class scheduler
: public asio::detail::execution_context_service_base<scheduler>
: public asio::detail::execution_context_service_base<scheduler>,
public thread_context
{
public:
typedef scheduler_operation operation;
Expand Down Expand Up @@ -181,9 +182,6 @@ class scheduler

// Flag to indicate that the dispatcher has been shut down.
bool shutdown_;

// Per-thread call stack to track the state of each thread in the io_service.
typedef call_stack<scheduler, thread_info> thread_call_stack;
};

} // namespace detail
Expand Down
42 changes: 42 additions & 0 deletions asio/include/asio/detail/thread_context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// detail/thread_context.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2014 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef ASIO_DETAIL_THREAD_CONTEXT_HPP
#define ASIO_DETAIL_THREAD_CONTEXT_HPP

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

#include <climits>
#include <cstddef>
#include "asio/detail/call_stack.hpp"

#include "asio/detail/push_options.hpp"

namespace asio {
namespace detail {

class thread_info_base;

// Base class for things that manage threads (scheduler, win_iocp_io_service).
class thread_context
{
public:
// Per-thread call stack to track the state of each thread in the context.
typedef call_stack<thread_context, thread_info_base> thread_call_stack;
};

} // namespace detail
} // namespace asio

#include "asio/detail/pop_options.hpp"

#endif // ASIO_DETAIL_THREAD_CONTEXT_HPP
10 changes: 3 additions & 7 deletions asio/include/asio/detail/win_iocp_io_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
#if defined(ASIO_HAS_IOCP)

#include "asio/io_service.hpp"
#include "asio/detail/call_stack.hpp"
#include "asio/detail/limits.hpp"
#include "asio/detail/mutex.hpp"
#include "asio/detail/op_queue.hpp"
#include "asio/detail/scoped_ptr.hpp"
#include "asio/detail/socket_types.hpp"
#include "asio/detail/thread.hpp"
#include "asio/detail/thread_context.hpp"
#include "asio/detail/timer_queue_base.hpp"
#include "asio/detail/timer_queue_set.hpp"
#include "asio/detail/wait_op.hpp"
Expand All @@ -41,10 +41,10 @@ namespace detail {
class wait_op;

class win_iocp_io_service
: public asio::detail::service_base<win_iocp_io_service>
: public asio::detail::service_base<win_iocp_io_service>,
public thread_context
{
public:

// Constructor. Specifies a concurrency hint that is passed through to the
// underlying I/O completion port.
ASIO_DECL win_iocp_io_service(asio::io_service& io_service,
Expand Down Expand Up @@ -294,10 +294,6 @@ class win_iocp_io_service

// The operations that are ready to dispatch.
op_queue<win_iocp_operation> completed_ops_;

// Per-thread call stack to track the state of each thread in the io_service.
typedef call_stack<win_iocp_io_service,
win_iocp_thread_info> thread_call_stack;
};

} // namespace detail
Expand Down
Loading

0 comments on commit 4e4aca0

Please sign in to comment.