blob: 3cf8578c1e40e9614912468c59375c7526ace9ce [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;
33extern unsigned int applets_active_queue;
34
Willy Tarreau64bca9d2015-09-25 17:39:23 +020035extern struct list applet_active_queue;
Willy Tarreau81f38d62015-04-13 17:11:11 +020036
Willy Tarreau3c595ac2015-04-19 09:59:31 +020037void applet_run_active();
38
Christopher Fauleta73e59b2016-12-09 17:30:18 +010039
40static int inline appctx_res_wakeup(struct appctx *appctx);
41
42
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020043/* Initializes all required fields for a new appctx. Note that it does the
44 * minimum acceptable initialization for an appctx. This means only the
45 * 3 integer states st0, st1, st2 are zeroed.
46 */
47static inline void appctx_init(struct appctx *appctx)
48{
49 appctx->st0 = appctx->st1 = appctx->st2 = 0;
Thierry FOURNIER / OZON.IO6a22dcb2016-11-12 10:51:33 +010050 appctx->io_release = NULL;
Emeric Brunc7306062017-06-26 16:36:53 +020051 appctx->state = APPLET_SLEEPING;
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020052}
53
54/* Tries to allocate a new appctx and initialize its main fields. The appctx
55 * is returned on success, NULL on failure. The appctx must be released using
56 * pool_free2(connection) or appctx_free(), since it's allocated from the
57 * connection pool. <applet> is assigned as the applet, but it can be NULL.
58 */
Willy Tarreau30576452015-04-13 13:50:30 +020059static inline struct appctx *appctx_new(struct applet *applet)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020060{
61 struct appctx *appctx;
62
63 appctx = pool_alloc2(pool2_connection);
64 if (likely(appctx != NULL)) {
65 appctx->obj_type = OBJ_TYPE_APPCTX;
66 appctx->applet = applet;
67 appctx_init(appctx);
Willy Tarreau81f38d62015-04-13 17:11:11 +020068 LIST_INIT(&appctx->runq);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010069 LIST_INIT(&appctx->buffer_wait.list);
70 appctx->buffer_wait.target = appctx;
71 appctx->buffer_wait.wakeup_cb = (int (*)(void *))appctx_res_wakeup;
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010072 nb_applets++;
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020073 }
74 return appctx;
75}
76
77/* Releases an appctx previously allocated by appctx_new(). Note that
78 * we share the connection pool.
79 */
Emeric Brunc7306062017-06-26 16:36:53 +020080static inline void __appctx_free(struct appctx *appctx)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020081{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010082 if (!LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau81f38d62015-04-13 17:11:11 +020083 LIST_DEL(&appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010084 applets_active_queue--;
85 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +010086 if (!LIST_ISEMPTY(&appctx->buffer_wait.list)) {
87 LIST_DEL(&appctx->buffer_wait.list);
88 LIST_INIT(&appctx->buffer_wait.list);
89 }
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020090 pool_free2(pool2_connection, appctx);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010091 nb_applets--;
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020092}
Emeric Brunc7306062017-06-26 16:36:53 +020093static inline void appctx_free(struct appctx *appctx)
94{
95 if (appctx->state & APPLET_RUNNING) {
96 appctx->state |= APPLET_WANT_DIE;
97 return;
98 }
99 __appctx_free(appctx);
100}
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200101
Willy Tarreau81f38d62015-04-13 17:11:11 +0200102/* wakes up an applet when conditions have changed */
Emeric Brunc7306062017-06-26 16:36:53 +0200103static inline void __appctx_wakeup(struct appctx *appctx)
Willy Tarreau81f38d62015-04-13 17:11:11 +0200104{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100105 if (LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau64bca9d2015-09-25 17:39:23 +0200106 LIST_ADDQ(&applet_active_queue, &appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100107 applets_active_queue++;
108 }
Willy Tarreau81f38d62015-04-13 17:11:11 +0200109}
110
Emeric Brunc7306062017-06-26 16:36:53 +0200111static inline void appctx_wakeup(struct appctx *appctx)
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200112{
Emeric Brunc7306062017-06-26 16:36:53 +0200113 if (appctx->state & APPLET_RUNNING) {
114 appctx->state |= APPLET_WOKEN_UP;
115 return;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200116 }
Emeric Brunc7306062017-06-26 16:36:53 +0200117 __appctx_wakeup(appctx);
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200118}
119
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100120/* Callback used to wake up an applet when a buffer is available. The applet
121 * <appctx> is woken up is if it is not already in the list of "active"
122 * applets. This functions returns 1 is the stream is woken up, otherwise it
Emeric Brunc7306062017-06-26 16:36:53 +0200123 * returns 0. If task is running we request we check if woken was already
124 * requested */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100125static inline int appctx_res_wakeup(struct appctx *appctx)
126{
Emeric Brunc7306062017-06-26 16:36:53 +0200127 if (appctx->state & APPLET_RUNNING) {
128 if (appctx->state & APPLET_WOKEN_UP) {
129 return 0;
130 }
131 appctx->state |= APPLET_WOKEN_UP;
132 return 1;
133 }
134
135 if (!LIST_ISEMPTY(&appctx->runq)) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100136 return 0;
Emeric Brunc7306062017-06-26 16:36:53 +0200137 }
138 __appctx_wakeup(appctx);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100139 return 1;
140}
141
142
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200143#endif /* _PROTO_APPLET_H */
144
145/*
146 * Local variables:
147 * c-indent-level: 8
148 * c-basic-offset: 8
149 * End:
150 */