blob: 653be31e486899aff069c2025a4aa2c20d1336e3 [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;
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020051}
52
53/* Tries to allocate a new appctx and initialize its main fields. The appctx
54 * is returned on success, NULL on failure. The appctx must be released using
55 * pool_free2(connection) or appctx_free(), since it's allocated from the
56 * connection pool. <applet> is assigned as the applet, but it can be NULL.
57 */
Willy Tarreau30576452015-04-13 13:50:30 +020058static inline struct appctx *appctx_new(struct applet *applet)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020059{
60 struct appctx *appctx;
61
62 appctx = pool_alloc2(pool2_connection);
63 if (likely(appctx != NULL)) {
64 appctx->obj_type = OBJ_TYPE_APPCTX;
65 appctx->applet = applet;
66 appctx_init(appctx);
Willy Tarreau81f38d62015-04-13 17:11:11 +020067 LIST_INIT(&appctx->runq);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010068 LIST_INIT(&appctx->buffer_wait.list);
69 appctx->buffer_wait.target = appctx;
70 appctx->buffer_wait.wakeup_cb = (int (*)(void *))appctx_res_wakeup;
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010071 nb_applets++;
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020072 }
73 return appctx;
74}
75
76/* Releases an appctx previously allocated by appctx_new(). Note that
77 * we share the connection pool.
78 */
79static inline void appctx_free(struct appctx *appctx)
80{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010081 if (!LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau81f38d62015-04-13 17:11:11 +020082 LIST_DEL(&appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010083 applets_active_queue--;
84 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +010085 if (!LIST_ISEMPTY(&appctx->buffer_wait.list)) {
86 LIST_DEL(&appctx->buffer_wait.list);
87 LIST_INIT(&appctx->buffer_wait.list);
88 }
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020089 pool_free2(pool2_connection, appctx);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010090 nb_applets--;
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020091}
92
Willy Tarreau81f38d62015-04-13 17:11:11 +020093/* wakes up an applet when conditions have changed */
94static inline void appctx_wakeup(struct appctx *appctx)
95{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010096 if (LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau64bca9d2015-09-25 17:39:23 +020097 LIST_ADDQ(&applet_active_queue, &appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010098 applets_active_queue++;
99 }
Willy Tarreau81f38d62015-04-13 17:11:11 +0200100}
101
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200102/* removes an applet from the list of active applets */
103static inline void appctx_pause(struct appctx *appctx)
104{
105 if (!LIST_ISEMPTY(&appctx->runq)) {
106 LIST_DEL(&appctx->runq);
107 LIST_INIT(&appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100108 applets_active_queue--;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200109 }
110}
111
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100112/* Callback used to wake up an applet when a buffer is available. The applet
113 * <appctx> is woken up is if it is not already in the list of "active"
114 * applets. This functions returns 1 is the stream is woken up, otherwise it
115 * returns 0. */
116static inline int appctx_res_wakeup(struct appctx *appctx)
117{
118 if (!LIST_ISEMPTY(&appctx->runq))
119 return 0;
120 appctx_wakeup(appctx);
121 return 1;
122}
123
124
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200125#endif /* _PROTO_APPLET_H */
126
127/*
128 * Local variables:
129 * c-indent-level: 8
130 * c-basic-offset: 8
131 * End:
132 */