blob: 914b496f52aaabf515340b3350b670f3f00ed8fc [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;
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010034__decl_hathreads(extern HA_SPINLOCK_T applet_active_lock);
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 */
Emeric Brun1138fd02017-06-19 12:38:55 +020047static inline void appctx_init(struct appctx *appctx, unsigned long thread_mask)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020048{
49 appctx->st0 = appctx->st1 = appctx->st2 = 0;
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;
Emeric Brunc7306062017-06-26 16:36:53 +020052 appctx->state = APPLET_SLEEPING;
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
57 * pool_free2(connection) or appctx_free(), since it's allocated from the
58 * 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
64 appctx = pool_alloc2(pool2_connection);
65 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);
Willy Tarreau81f38d62015-04-13 17:11:11 +020069 LIST_INIT(&appctx->runq);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010070 LIST_INIT(&appctx->buffer_wait.list);
71 appctx->buffer_wait.target = appctx;
72 appctx->buffer_wait.wakeup_cb = (int (*)(void *))appctx_res_wakeup;
Emeric Brun1138fd02017-06-19 12:38:55 +020073 HA_ATOMIC_ADD(&nb_applets, 1);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020074 }
75 return appctx;
76}
77
78/* Releases an appctx previously allocated by appctx_new(). Note that
79 * we share the connection pool.
80 */
Emeric Brunc7306062017-06-26 16:36:53 +020081static inline void __appctx_free(struct appctx *appctx)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020082{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010083 if (!LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau81f38d62015-04-13 17:11:11 +020084 LIST_DEL(&appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010085 applets_active_queue--;
86 }
Emeric Brun1138fd02017-06-19 12:38:55 +020087
Christopher Fauleta73e59b2016-12-09 17:30:18 +010088 if (!LIST_ISEMPTY(&appctx->buffer_wait.list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +010089 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010090 LIST_DEL(&appctx->buffer_wait.list);
91 LIST_INIT(&appctx->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010092 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010093 }
Emeric Brun1138fd02017-06-19 12:38:55 +020094
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020095 pool_free2(pool2_connection, appctx);
Emeric Brun1138fd02017-06-19 12:38:55 +020096 HA_ATOMIC_SUB(&nb_applets, 1);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020097}
Emeric Brunc7306062017-06-26 16:36:53 +020098static inline void appctx_free(struct appctx *appctx)
99{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100100 HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200101 if (appctx->state & APPLET_RUNNING) {
102 appctx->state |= APPLET_WANT_DIE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100103 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200104 return;
105 }
106 __appctx_free(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100107 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200108}
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200109
Willy Tarreau81f38d62015-04-13 17:11:11 +0200110/* wakes up an applet when conditions have changed */
Emeric Brunc7306062017-06-26 16:36:53 +0200111static inline void __appctx_wakeup(struct appctx *appctx)
Willy Tarreau81f38d62015-04-13 17:11:11 +0200112{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100113 if (LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau64bca9d2015-09-25 17:39:23 +0200114 LIST_ADDQ(&applet_active_queue, &appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100115 applets_active_queue++;
116 }
Willy Tarreau81f38d62015-04-13 17:11:11 +0200117}
118
Emeric Brunc7306062017-06-26 16:36:53 +0200119static inline void appctx_wakeup(struct appctx *appctx)
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200120{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100121 HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200122 if (appctx->state & APPLET_RUNNING) {
123 appctx->state |= APPLET_WOKEN_UP;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100124 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200125 return;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200126 }
Emeric Brunc7306062017-06-26 16:36:53 +0200127 __appctx_wakeup(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100128 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200129}
130
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100131/* Callback used to wake up an applet when a buffer is available. The applet
132 * <appctx> is woken up is if it is not already in the list of "active"
133 * applets. This functions returns 1 is the stream is woken up, otherwise it
Emeric Brunc7306062017-06-26 16:36:53 +0200134 * returns 0. If task is running we request we check if woken was already
135 * requested */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100136static inline int appctx_res_wakeup(struct appctx *appctx)
137{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100138 HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200139 if (appctx->state & APPLET_RUNNING) {
140 if (appctx->state & APPLET_WOKEN_UP) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100141 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200142 return 0;
143 }
144 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 1;
147 }
Emeric Brunc7306062017-06-26 16:36:53 +0200148 __appctx_wakeup(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100149 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100150 return 1;
151}
152
153
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200154#endif /* _PROTO_APPLET_H */
155
156/*
157 * Local variables:
158 * c-indent-level: 8
159 * c-basic-offset: 8
160 * End:
161 */