2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * David M. Lee, II <dlee@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Stasis Message Bus configuration API.
23 * \author David M. Lee, II <dlee@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/config_options.h"
35 #include "asterisk/stasis.h"
36 #include "asterisk/threadpool.h"
41 <configInfo name="stasis" language="en_US">
42 <synopsis>Stasis message bus configuration.</synopsis>
43 <configFile name="stasis.conf">
44 <configObject name="threadpool">
45 <synopsis>Threadpool configuration.</synopsis>
46 <configOption name="initial_size" default="0">
47 <synopsis>Initial number of threads in the message bus threadpool.</synopsis>
49 <configOption name="idle_timeout_sec" default="20">
50 <synopsis>Number of seconds for an idle thread to be disposed of.</synopsis>
52 <configOption name="max_size" default="200">
53 <synopsis>Maximum number of threads in the threadpool.</synopsis>
60 /*! \brief Locking container for safe configuration access. */
61 static AO2_GLOBAL_OBJ_STATIC(confs);
63 struct stasis_threadpool_conf {
70 struct stasis_threadpool_conf *threadpool;
73 /*! \brief Mapping of the stasis conf struct's globals to the
74 * threadpool context in the config file. */
75 static struct aco_type threadpool_option = {
78 .item_offset = offsetof(struct stasis_conf, threadpool),
79 .category = "^threadpool$",
80 .category_match = ACO_WHITELIST,
83 static struct aco_type *threadpool_options[] = ACO_TYPES(&threadpool_option);
85 #define CONF_FILENAME "stasis.conf"
87 /*! \brief The conf file that's processed for the module. */
88 static struct aco_file conf_file = {
89 /*! The config file name. */
90 .filename = CONF_FILENAME,
91 /*! The mapping object types to be processed. */
92 .types = ACO_TYPES(&threadpool_option),
95 static void conf_dtor(void *obj)
97 struct stasis_conf *conf = obj;
99 ao2_cleanup(conf->threadpool);
100 conf->threadpool = NULL;
103 static void *conf_alloc(void)
105 RAII_VAR(struct stasis_conf *, conf, NULL, ao2_cleanup);
107 conf = ao2_alloc_options(sizeof(*conf), conf_dtor,
108 AO2_ALLOC_OPT_LOCK_NOLOCK);
113 conf->threadpool = ao2_alloc_options(sizeof(*conf->threadpool), NULL,
114 AO2_ALLOC_OPT_LOCK_NOLOCK);
115 if (!conf->threadpool) {
119 aco_set_defaults(&threadpool_option, "threadpool", conf->threadpool);
125 CONFIG_INFO_CORE("stasis", cfg_info, confs, conf_alloc,
126 .files = ACO_FILES(&conf_file));
128 void stasis_config_get_threadpool_options(
129 struct ast_threadpool_options *threadpool_options)
131 RAII_VAR(struct stasis_conf *, conf, NULL, ao2_cleanup);
133 conf = ao2_global_obj_ref(confs);
135 ast_assert(conf && conf->threadpool);
138 struct ast_threadpool_options newopts = {
139 .version = AST_THREADPOOL_OPTIONS_VERSION,
140 .initial_size = conf->threadpool->initial_size,
142 .idle_timeout = conf->threadpool->idle_timeout_sec,
143 .max_size = conf->threadpool->max_size,
146 *threadpool_options = newopts;
150 /*! \brief Load (or reload) configuration. */
151 static int process_config(int reload)
153 RAII_VAR(struct stasis_conf *, conf, conf_alloc(), ao2_cleanup);
155 switch (aco_process_config(&cfg_info, reload)) {
156 case ACO_PROCESS_ERROR:
158 && !aco_set_defaults(&threadpool_option, "threadpool", conf->threadpool)) {
159 ast_log(AST_LOG_NOTICE, "Failed to process Stasis configuration; using defaults\n");
160 ao2_global_obj_replace(confs, conf);
165 case ACO_PROCESS_UNCHANGED:
172 static void config_exit(void)
174 aco_info_destroy(&cfg_info);
175 ao2_global_obj_release(confs);
178 int stasis_config_init(void)
180 if (aco_info_init(&cfg_info)) {
181 aco_info_destroy(&cfg_info);
185 ast_register_atexit(config_exit);
187 /* threadpool section */
188 aco_option_register(&cfg_info, "initial_size", ACO_EXACT,
189 threadpool_options, "0", OPT_INT_T, PARSE_IN_RANGE,
190 FLDSET(struct stasis_threadpool_conf, initial_size), 0,
192 aco_option_register(&cfg_info, "idle_timeout_sec", ACO_EXACT,
193 threadpool_options, "20", OPT_INT_T, PARSE_IN_RANGE,
194 FLDSET(struct stasis_threadpool_conf, idle_timeout_sec), 0,
196 aco_option_register(&cfg_info, "max_size", ACO_EXACT,
197 threadpool_options, "200", OPT_INT_T, PARSE_IN_RANGE,
198 FLDSET(struct stasis_threadpool_conf, max_size), 0, INT_MAX);
200 return process_config(0);