Files
asterisk/include/asterisk/serializer.h
Joshua C. Colp 1dc6fecc21 taskpool: Add taskpool API, switch Stasis to using it.
This change introduces a new API called taskpool. This is a pool
of taskprocessors. It provides the following functionality:

1. Task pushing to a pool of taskprocessors
2. Synchronous tasks
3. Serializers for execution ordering of tasks
4. Growing/shrinking of number of taskprocessors in pool

This functionality already exists through the combination of
threadpool+taskprocessors but through investigating I determined
that this carries substantial overhead for short to medium duration
tasks. The threadpool uses a single queue of work, and for management
of threads it involves additional tasks.

I wrote taskpool to eliminate the extra overhead and management
as much as possible. Instead of a single queue of work each
taskprocessor has its own queue and at push time a selector chooses
the taskprocessor to queue the task to. Each taskprocessor also
has its own thread like normal. This spreads out the tasks immediately
and reduces contention on shared resources.

Using the included efficiency tests the number of tasks that can be
executed per second in a taskpool is 6-12 times more than an equivalent
threadpool+taskprocessor setup.

Stasis has been moved over to using this new API as it is a heavy consumer
of threadpool+taskprocessors and produces a lot of tasks.

UpgradeNote: The threadpool_* options in stasis.conf have now been deprecated
though they continue to be read and used. They have been replaced with taskpool
options that give greater control over the underlying taskpool used for stasis.

DeveloperNote: The taskpool API has been added for common usage of a
pool of taskprocessors. It is suggested to use this API instead of the
threadpool+taskprocessor approach.
2025-09-16 17:21:23 +00:00

110 lines
3.4 KiB
C

/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2019, Sangoma Technologies Corporation
*
* Kevin Harwell <kharwell@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
#ifndef _AST_SERIALIZER_H
#define _AST_SERIALIZER_H
struct ast_threadpool;
struct ast_taskpool;
/*!
* Maintains a named pool of thread pooled taskprocessors. Also if configured
* a shutdown group can be enabled that will ensure all serializers have
* completed any assigned task before destruction.
*/
struct ast_serializer_pool;
/*!
* \brief Destroy the serializer pool.
*
* Attempt to destroy the serializer pool. If a shutdown group has been enabled,
* and times out waiting for threads to complete, then this function will return
* the number of remaining threads, and the pool will not be destroyed.
*
* \param pool The pool to destroy
*/
int ast_serializer_pool_destroy(struct ast_serializer_pool *pool);
/*!
* \brief Create a serializer pool.
*
* Create a serializer pool with an optional shutdown group. If a timeout greater
* than -1 is specified then a shutdown group is enabled on the pool.
*
* \param name The base name for the pool, and used when building taskprocessor(s)
* \param size The size of the pool
* \param threadpool The backing threadpool to use
* \param timeout The timeout used if using a shutdown group (-1 = disabled)
*
* \return A newly allocated serializer pool object
* \retval NULL on error
*/
struct ast_serializer_pool *ast_serializer_pool_create(const char *name,
unsigned int size, struct ast_threadpool *threadpool, int timeout);
/*!
* \brief Create a serializer pool on taskpool.
* \since 23.1.0
* \since 22.7.0
* \since 20.17.0
*
* Create a serializer pool with an optional shutdown group. If a timeout greater
* than -1 is specified then a shutdown group is enabled on the pool.
*
* \param name The base name for the pool, and used when building taskprocessor(s)
* \param size The size of the pool
* \param taskpool The backing taskpool to use
* \param timeout The timeout used if using a shutdown group (-1 = disabled)
*
* \return A newly allocated serializer pool object
* \retval NULL on error
*/
struct ast_serializer_pool *ast_serializer_taskpool_create(const char *name,
unsigned int size, struct ast_taskpool *taskpool, int timeout);
/*!
* \brief Retrieve the base name of the serializer pool.
*
* \param pool The pool object
*
* \return The base name given to the pool
*/
const char *ast_serializer_pool_name(const struct ast_serializer_pool *pool);
/*!
* \brief Retrieve a serializer from the pool.
*
* \param pool The pool object
*
* \return A serializer/taskprocessor
*/
struct ast_taskprocessor *ast_serializer_pool_get(struct ast_serializer_pool *pool);
/*!
* \brief Set taskprocessor alert levels for the serializers in the pool.
*
* \param pool The pool to destroy
* \param high, low
*
* \retval 0 on success.
* \retval -1 on error.
*/
int ast_serializer_pool_set_alerts(struct ast_serializer_pool *pool, long high, long low);
#endif /* _AST_SERIALIZER_H */