Classes | Public Types | Public Member Functions | List of all members
util::IoContextPool Class Reference

A pool of IoContext objects, representing and managing CPU resources of the system. More...

#include <io_context_pool.h>

Public Types

using io_context = ::boost::asio::io_context
 

Public Member Functions

 IoContextPool (const IoContextPool &)=delete
 
void operator= (const IoContextPool &)=delete
 
 IoContextPool (std::size_t pool_size=0, std::vector< size_t > cpus={})
 
void Run ()
 Starts running all IoContext objects in the pool. Does not block.
 
void Stop ()
 Stops all io_context objects in the pool. More...
 
IoContextGetNextContext ()
 Get an io_context to use. Thread-safe.
 
IoContextoperator[] (size_t i)
 
IoContextat (size_t i)
 
size_t size () const
 
template<typename Func , AcceptArgsCheck< Func, IoContext & > = 0>
void AsyncOnAll (Func &&func)
 Runs func in all IO threads asynchronously. More...
 
template<typename Func , AcceptArgsCheck< Func, unsigned, IoContext & > = 0>
void AsyncOnAll (Func &&func)
 Runs func in all IO threads asynchronously. More...
 
template<typename Func , AcceptArgsCheck< Func, IoContext & > = 0>
void AwaitOnAll (Func &&func)
 Runs the funcion in all IO threads asynchronously. Blocks until all the asynchronous calls return. More...
 
template<typename Func , AcceptArgsCheck< Func, unsigned, IoContext & > = 0>
void AwaitOnAll (Func &&func)
 Blocks until all the asynchronous calls to func return. Func receives both the index and IoContext&. func must not block. More...
 
template<typename Func , AcceptArgsCheck< Func, unsigned, IoContext & > = 0>
void AsyncFiberOnAll (Func &&func)
 Runs func in a fiber asynchronously. func must accept IoContext&. func may fiber-block. More...
 
template<typename Func , AcceptArgsCheck< Func, IoContext & > = 0>
void AsyncFiberOnAll (Func &&func)
 Runs func in a fiber asynchronously. func must accept IoContext&. func may fiber-block. More...
 
template<typename Func , AcceptArgsCheck< Func, unsigned, IoContext & > = 0>
void AwaitFiberOnAll (Func &&func)
 Runs func wrapped in fiber on all IO threads in parallel. func must accept IoContext&. func may fiber-block. More...
 
template<typename Func , AcceptArgsCheck< Func, IoContext & > = 0>
void AwaitFiberOnAll (Func &&func)
 Runs func wrapped in fiber on all IO threads in parallel. func must accept IoContext&. func may fiber-block. More...
 
template<typename Func >
void AwaitFiberOnAllSerially (Func &&func)
 Runs func wrapped in fiber on all IO threads, SERIALLY. func must accept IoContext&. func may fiber-block. More...
 
IoContextGetThisContext ()
 

Detailed Description

A pool of IoContext objects, representing and managing CPU resources of the system.

Author
Roman Gershman

The main entry for launching asynchronous processes across all IO threads. For single thread manager see IoContext class.

Definition at line 26 of file io_context_pool.h.

Constructor & Destructor Documentation

◆ IoContextPool()

util::IoContextPool::IoContextPool ( std::size_t  pool_size = 0,
std::vector< size_t >  cpus = {} 
)
explicit

Constructs io_context pool with number of threads equal to 'pool_size'. pool_size = 0 chooses automatically pool size equal to number of cores in the system.

Member Function Documentation

◆ AsyncFiberOnAll() [1/2]

template<typename Func , AcceptArgsCheck< Func, unsigned, IoContext & > = 0>
void util::IoContextPool::AsyncFiberOnAll ( Func &&  func)
inline

Runs func in a fiber asynchronously. func must accept IoContext&. func may fiber-block.

Parameters
func'func' callback runs inside a wrapping fiber.

Definition at line 133 of file io_context_pool.h.

◆ AsyncFiberOnAll() [2/2]

template<typename Func , AcceptArgsCheck< Func, IoContext & > = 0>
void util::IoContextPool::AsyncFiberOnAll ( Func &&  func)
inline

Runs func in a fiber asynchronously. func must accept IoContext&. func may fiber-block.

Parameters
func'func' callback runs inside a wrapping fiber.

Definition at line 147 of file io_context_pool.h.

◆ AsyncOnAll() [1/2]

template<typename Func , AcceptArgsCheck< Func, IoContext & > = 0>
void util::IoContextPool::AsyncOnAll ( Func &&  func)
inline

Runs func in all IO threads asynchronously.

The task must be CPU-only non IO-blocking code because it runs directly in IO-fiber. MapTask runs asynchronously and will exit before the task finishes. The 'func' must accept IoContext& as its argument.

Definition at line 67 of file io_context_pool.h.

◆ AsyncOnAll() [2/2]

template<typename Func , AcceptArgsCheck< Func, unsigned, IoContext & > = 0>
void util::IoContextPool::AsyncOnAll ( Func &&  func)
inline

Runs func in all IO threads asynchronously.

The task must be CPU-only non IO-blocking code because it runs directly in IO-fiber. MapTask runs asynchronously and will exit before the task finishes. The 'func' must accept unsigned int (io context index) and IoContext& as its arguments.

Definition at line 84 of file io_context_pool.h.

◆ AwaitFiberOnAll() [1/2]

template<typename Func , AcceptArgsCheck< Func, unsigned, IoContext & > = 0>
void util::IoContextPool::AwaitFiberOnAll ( Func &&  func)
inline

Runs func wrapped in fiber on all IO threads in parallel. func must accept IoContext&. func may fiber-block.

Parameters
funcWaits for all the callbacks to finish.

Definition at line 162 of file io_context_pool.h.

◆ AwaitFiberOnAll() [2/2]

template<typename Func , AcceptArgsCheck< Func, IoContext & > = 0>
void util::IoContextPool::AwaitFiberOnAll ( Func &&  func)
inline

Runs func wrapped in fiber on all IO threads in parallel. func must accept IoContext&. func may fiber-block.

Parameters
funcWaits for all the callbacks to finish.

Definition at line 180 of file io_context_pool.h.

◆ AwaitFiberOnAllSerially()

template<typename Func >
void util::IoContextPool::AwaitFiberOnAllSerially ( Func &&  func)
inline

Runs func wrapped in fiber on all IO threads, SERIALLY. func must accept IoContext&. func may fiber-block.

Note that this function is highly inefficient, it is mostly useful when one wants to reduce several thread-local variables into a single common variable. Instead of creating a mutex for the variable, it is better to just run sequentially.

Definition at line 198 of file io_context_pool.h.

◆ AwaitOnAll() [1/2]

template<typename Func , AcceptArgsCheck< Func, IoContext & > = 0>
void util::IoContextPool::AwaitOnAll ( Func &&  func)
inline

Runs the funcion in all IO threads asynchronously. Blocks until all the asynchronous calls return.

Func must accept "IoContext&" and it should not block.

Definition at line 99 of file io_context_pool.h.

◆ AwaitOnAll() [2/2]

template<typename Func , AcceptArgsCheck< Func, unsigned, IoContext & > = 0>
void util::IoContextPool::AwaitOnAll ( Func &&  func)
inline

Blocks until all the asynchronous calls to func return. Func receives both the index and IoContext&. func must not block.

Definition at line 115 of file io_context_pool.h.

◆ Stop()

void util::IoContextPool::Stop ( )

Stops all io_context objects in the pool.

Waits for all the threads to finish. Requires that Run has been called. Blocks the current thread until all the pool threads exited.

Definition at line 84 of file io_context_pool.cc.


The documentation for this class was generated from the following files: