blob: 7cc2c0ad148ef7858a407bf10fd57907acaf28c6 [file] [log] [blame]
Willy Tarreau8a8d83b2015-04-13 13:24:54 +02001/*
2 * include/proto/applet.h
3 * This file contains applet function prototypes
4 *
5 * Copyright (C) 2000-2015 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _PROTO_APPLET_H
23#define _PROTO_APPLET_H
24
25#include <stdlib.h>
26
27#include <common/config.h>
Willy Tarreau81f38d62015-04-13 17:11:11 +020028#include <common/mini-clist.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020029#include <types/applet.h>
30#include <proto/connection.h>
31
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010032extern unsigned int nb_applets;
Christopher Faulet595d7b72017-11-14 11:28:52 +010033extern unsigned long active_applets_mask;
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010034extern unsigned int applets_active_queue;
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010035__decl_hathreads(extern HA_SPINLOCK_T applet_active_lock);
Willy Tarreau64bca9d2015-09-25 17:39:23 +020036extern struct list applet_active_queue;
Willy Tarreau81f38d62015-04-13 17:11:11 +020037
Willy Tarreau3c595ac2015-04-19 09:59:31 +020038void applet_run_active();
39
Christopher Fauleta73e59b2016-12-09 17:30:18 +010040
41static int inline appctx_res_wakeup(struct appctx *appctx);
42
43
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020044/* Initializes all required fields for a new appctx. Note that it does the
45 * minimum acceptable initialization for an appctx. This means only the
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020046 * 3 integer states st0, st1, st2 and the chunk used to gather unfinished
47 * commands are zeroed
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020048 */
Emeric Brun1138fd02017-06-19 12:38:55 +020049static inline void appctx_init(struct appctx *appctx, unsigned long thread_mask)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020050{
51 appctx->st0 = appctx->st1 = appctx->st2 = 0;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020052 appctx->chunk = NULL;
Thierry FOURNIER / OZON.IO6a22dcb2016-11-12 10:51:33 +010053 appctx->io_release = NULL;
Willy Tarreauf65610a2017-10-31 16:06:06 +010054 appctx->thread_mask = thread_mask;
Emeric Brunc7306062017-06-26 16:36:53 +020055 appctx->state = APPLET_SLEEPING;
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020056}
57
58/* Tries to allocate a new appctx and initialize its main fields. The appctx
59 * is returned on success, NULL on failure. The appctx must be released using
Willy Tarreaubafbe012017-11-24 17:34:44 +010060 * pool_free(connection) or appctx_free(), since it's allocated from the
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020061 * connection pool. <applet> is assigned as the applet, but it can be NULL.
62 */
Emeric Brun1138fd02017-06-19 12:38:55 +020063static inline struct appctx *appctx_new(struct applet *applet, unsigned long thread_mask)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020064{
65 struct appctx *appctx;
66
Willy Tarreaubafbe012017-11-24 17:34:44 +010067 appctx = pool_alloc(pool_head_connection);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020068 if (likely(appctx != NULL)) {
69 appctx->obj_type = OBJ_TYPE_APPCTX;
70 appctx->applet = applet;
Emeric Brun1138fd02017-06-19 12:38:55 +020071 appctx_init(appctx, thread_mask);
Willy Tarreau81f38d62015-04-13 17:11:11 +020072 LIST_INIT(&appctx->runq);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010073 LIST_INIT(&appctx->buffer_wait.list);
74 appctx->buffer_wait.target = appctx;
75 appctx->buffer_wait.wakeup_cb = (int (*)(void *))appctx_res_wakeup;
Emeric Brun1138fd02017-06-19 12:38:55 +020076 HA_ATOMIC_ADD(&nb_applets, 1);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020077 }
78 return appctx;
79}
80
81/* Releases an appctx previously allocated by appctx_new(). Note that
82 * we share the connection pool.
83 */
Emeric Brunc7306062017-06-26 16:36:53 +020084static inline void __appctx_free(struct appctx *appctx)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020085{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010086 if (!LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau81f38d62015-04-13 17:11:11 +020087 LIST_DEL(&appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010088 applets_active_queue--;
89 }
Emeric Brun1138fd02017-06-19 12:38:55 +020090
Christopher Fauleta73e59b2016-12-09 17:30:18 +010091 if (!LIST_ISEMPTY(&appctx->buffer_wait.list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +010092 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010093 LIST_DEL(&appctx->buffer_wait.list);
94 LIST_INIT(&appctx->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010095 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010096 }
Emeric Brun1138fd02017-06-19 12:38:55 +020097
Willy Tarreaubafbe012017-11-24 17:34:44 +010098 pool_free(pool_head_connection, appctx);
Emeric Brun1138fd02017-06-19 12:38:55 +020099 HA_ATOMIC_SUB(&nb_applets, 1);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200100}
Emeric Brunc7306062017-06-26 16:36:53 +0200101static inline void appctx_free(struct appctx *appctx)
102{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100103 HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200104 if (appctx->state & APPLET_RUNNING) {
105 appctx->state |= APPLET_WANT_DIE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100106 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200107 return;
108 }
109 __appctx_free(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100110 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200111}
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200112
Willy Tarreau81f38d62015-04-13 17:11:11 +0200113/* wakes up an applet when conditions have changed */
Emeric Brunc7306062017-06-26 16:36:53 +0200114static inline void __appctx_wakeup(struct appctx *appctx)
Willy Tarreau81f38d62015-04-13 17:11:11 +0200115{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100116 if (LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau64bca9d2015-09-25 17:39:23 +0200117 LIST_ADDQ(&applet_active_queue, &appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100118 applets_active_queue++;
Christopher Faulet595d7b72017-11-14 11:28:52 +0100119 active_applets_mask |= appctx->thread_mask;
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100120 }
Willy Tarreau81f38d62015-04-13 17:11:11 +0200121}
122
Emeric Brunc7306062017-06-26 16:36:53 +0200123static inline void appctx_wakeup(struct appctx *appctx)
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200124{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100125 HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200126 if (appctx->state & APPLET_RUNNING) {
127 appctx->state |= APPLET_WOKEN_UP;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100128 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200129 return;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200130 }
Emeric Brunc7306062017-06-26 16:36:53 +0200131 __appctx_wakeup(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100132 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200133}
134
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100135/* Callback used to wake up an applet when a buffer is available. The applet
136 * <appctx> is woken up is if it is not already in the list of "active"
137 * applets. This functions returns 1 is the stream is woken up, otherwise it
Emeric Brunc7306062017-06-26 16:36:53 +0200138 * returns 0. If task is running we request we check if woken was already
139 * requested */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100140static inline int appctx_res_wakeup(struct appctx *appctx)
141{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100142 HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200143 if (appctx->state & APPLET_RUNNING) {
144 if (appctx->state & APPLET_WOKEN_UP) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100145 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200146 return 0;
147 }
148 appctx->state |= APPLET_WOKEN_UP;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100149 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200150 return 1;
151 }
Emeric Brunc7306062017-06-26 16:36:53 +0200152 __appctx_wakeup(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100153 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100154 return 1;
155}
156
157
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200158#endif /* _PROTO_APPLET_H */
159
160/*
161 * Local variables:
162 * c-indent-level: 8
163 * c-basic-offset: 8
164 * End:
165 */