blob: 00ad63b6a1259fc89efb6b8cfb55eee17a994e2d [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>
Olivier Houchard673867c2018-05-25 16:58:52 +020031#include <proto/task.h>
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020032
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010033extern unsigned int nb_applets;
Willy Tarreau81f38d62015-04-13 17:11:11 +020034
Olivier Houchard673867c2018-05-25 16:58:52 +020035struct task *task_run_applet(struct task *t, void *context, unsigned short state);
Willy Tarreau3c595ac2015-04-19 09:59:31 +020036
Christopher Fauleta73e59b2016-12-09 17:30:18 +010037
38static int inline appctx_res_wakeup(struct appctx *appctx);
39
40
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020041/* Initializes all required fields for a new appctx. Note that it does the
42 * minimum acceptable initialization for an appctx. This means only the
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020043 * 3 integer states st0, st1, st2 and the chunk used to gather unfinished
44 * commands are zeroed
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020045 */
Emeric Brun1138fd02017-06-19 12:38:55 +020046static inline void appctx_init(struct appctx *appctx, unsigned long thread_mask)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020047{
48 appctx->st0 = appctx->st1 = appctx->st2 = 0;
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020049 appctx->chunk = NULL;
Thierry FOURNIER / OZON.IO6a22dcb2016-11-12 10:51:33 +010050 appctx->io_release = NULL;
Willy Tarreauf65610a2017-10-31 16:06:06 +010051 appctx->thread_mask = thread_mask;
Olivier Houchard673867c2018-05-25 16:58:52 +020052 appctx->state = 0;
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020053}
54
55/* Tries to allocate a new appctx and initialize its main fields. The appctx
56 * is returned on success, NULL on failure. The appctx must be released using
Willy Tarreaubafbe012017-11-24 17:34:44 +010057 * pool_free(connection) or appctx_free(), since it's allocated from the
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020058 * connection pool. <applet> is assigned as the applet, but it can be NULL.
59 */
Emeric Brun1138fd02017-06-19 12:38:55 +020060static inline struct appctx *appctx_new(struct applet *applet, unsigned long thread_mask)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020061{
62 struct appctx *appctx;
63
Willy Tarreaubafbe012017-11-24 17:34:44 +010064 appctx = pool_alloc(pool_head_connection);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020065 if (likely(appctx != NULL)) {
66 appctx->obj_type = OBJ_TYPE_APPCTX;
67 appctx->applet = applet;
Emeric Brun1138fd02017-06-19 12:38:55 +020068 appctx_init(appctx, thread_mask);
Olivier Houchard673867c2018-05-25 16:58:52 +020069 appctx->t = task_new(thread_mask);
70 if (unlikely(appctx->t == NULL)) {
71 pool_free(pool_head_connection, appctx);
72 return NULL;
73 }
74 appctx->t->process = task_run_applet;
75 appctx->t->context = appctx;
Christopher Fauleta73e59b2016-12-09 17:30:18 +010076 LIST_INIT(&appctx->buffer_wait.list);
77 appctx->buffer_wait.target = appctx;
78 appctx->buffer_wait.wakeup_cb = (int (*)(void *))appctx_res_wakeup;
Emeric Brun1138fd02017-06-19 12:38:55 +020079 HA_ATOMIC_ADD(&nb_applets, 1);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020080 }
81 return appctx;
82}
83
84/* Releases an appctx previously allocated by appctx_new(). Note that
85 * we share the connection pool.
86 */
Emeric Brunc7306062017-06-26 16:36:53 +020087static inline void __appctx_free(struct appctx *appctx)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020088{
Olivier Houchard673867c2018-05-25 16:58:52 +020089 task_delete(appctx->t);
90 task_free(appctx->t);
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}
Olivier Houchard673867c2018-05-25 16:58:52 +0200101
Emeric Brunc7306062017-06-26 16:36:53 +0200102static inline void appctx_free(struct appctx *appctx)
103{
Olivier Houchard673867c2018-05-25 16:58:52 +0200104 /* The task is supposed to be run on this thread, so we can just
105 * check if it's running already (or about to run) or not
106 */
107 if (!(appctx->t->state & TASK_RUNNING))
108 __appctx_free(appctx);
109 else {
110 /* if it's running, or about to run, defer the freeing
111 * until the callback is called.
112 */
Emeric Brunc7306062017-06-26 16:36:53 +0200113 appctx->state |= APPLET_WANT_DIE;
Olivier Houchard673867c2018-05-25 16:58:52 +0200114 task_wakeup(appctx->t, TASK_WOKEN_OTHER);
Emeric Brunc7306062017-06-26 16:36:53 +0200115 }
Emeric Brunc7306062017-06-26 16:36:53 +0200116}
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200117
Willy Tarreau81f38d62015-04-13 17:11:11 +0200118/* wakes up an applet when conditions have changed */
Emeric Brunc7306062017-06-26 16:36:53 +0200119static inline void appctx_wakeup(struct appctx *appctx)
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200120{
Olivier Houchard673867c2018-05-25 16:58:52 +0200121 task_wakeup(appctx->t, TASK_WOKEN_OTHER);
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200122}
123
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100124/* Callback used to wake up an applet when a buffer is available. The applet
125 * <appctx> is woken up is if it is not already in the list of "active"
126 * applets. This functions returns 1 is the stream is woken up, otherwise it
Emeric Brunc7306062017-06-26 16:36:53 +0200127 * returns 0. If task is running we request we check if woken was already
128 * requested */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100129static inline int appctx_res_wakeup(struct appctx *appctx)
130{
Olivier Houchard673867c2018-05-25 16:58:52 +0200131 int ret;
132
133 /* To detect if we have already been waken or not, we now that
134 * if the state contains TASK_RUNNING, but not just TASK_RUNNING.
135 * This is racy, but that's OK. At worst we will wake a little more
136 * tasks than necessary when a buffer is available.
137 */
138 ret = ((appctx->state & TASK_RUNNING) != 0) &&
139 ((appctx->state != TASK_RUNNING));
140 task_wakeup(appctx->t, TASK_WOKEN_OTHER);
141 return ret;
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100142}
143
144
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200145#endif /* _PROTO_APPLET_H */
146
147/*
148 * Local variables:
149 * c-indent-level: 8
150 * c-basic-offset: 8
151 * End:
152 */