blob: cdd4d90f09e6cbf77e45ae62506ad0bf8aaff343 [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
46 * 3 integer states st0, st1, st2 are zeroed.
47 */
Emeric Brun1138fd02017-06-19 12:38:55 +020048static inline void appctx_init(struct appctx *appctx, unsigned long thread_mask)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020049{
50 appctx->st0 = appctx->st1 = appctx->st2 = 0;
Thierry FOURNIER / OZON.IO6a22dcb2016-11-12 10:51:33 +010051 appctx->io_release = NULL;
Willy Tarreauf65610a2017-10-31 16:06:06 +010052 appctx->thread_mask = thread_mask;
Emeric Brunc7306062017-06-26 16:36:53 +020053 appctx->state = APPLET_SLEEPING;
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020054}
55
56/* Tries to allocate a new appctx and initialize its main fields. The appctx
57 * is returned on success, NULL on failure. The appctx must be released using
Willy Tarreaubafbe012017-11-24 17:34:44 +010058 * pool_free(connection) or appctx_free(), since it's allocated from the
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020059 * connection pool. <applet> is assigned as the applet, but it can be NULL.
60 */
Emeric Brun1138fd02017-06-19 12:38:55 +020061static inline struct appctx *appctx_new(struct applet *applet, unsigned long thread_mask)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020062{
63 struct appctx *appctx;
64
Willy Tarreaubafbe012017-11-24 17:34:44 +010065 appctx = pool_alloc(pool_head_connection);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020066 if (likely(appctx != NULL)) {
67 appctx->obj_type = OBJ_TYPE_APPCTX;
68 appctx->applet = applet;
Emeric Brun1138fd02017-06-19 12:38:55 +020069 appctx_init(appctx, thread_mask);
Willy Tarreau81f38d62015-04-13 17:11:11 +020070 LIST_INIT(&appctx->runq);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010071 LIST_INIT(&appctx->buffer_wait.list);
72 appctx->buffer_wait.target = appctx;
73 appctx->buffer_wait.wakeup_cb = (int (*)(void *))appctx_res_wakeup;
Emeric Brun1138fd02017-06-19 12:38:55 +020074 HA_ATOMIC_ADD(&nb_applets, 1);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020075 }
76 return appctx;
77}
78
79/* Releases an appctx previously allocated by appctx_new(). Note that
80 * we share the connection pool.
81 */
Emeric Brunc7306062017-06-26 16:36:53 +020082static inline void __appctx_free(struct appctx *appctx)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020083{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010084 if (!LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau81f38d62015-04-13 17:11:11 +020085 LIST_DEL(&appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010086 applets_active_queue--;
87 }
Emeric Brun1138fd02017-06-19 12:38:55 +020088
Christopher Fauleta73e59b2016-12-09 17:30:18 +010089 if (!LIST_ISEMPTY(&appctx->buffer_wait.list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +010090 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010091 LIST_DEL(&appctx->buffer_wait.list);
92 LIST_INIT(&appctx->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010093 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010094 }
Emeric Brun1138fd02017-06-19 12:38:55 +020095
Willy Tarreaubafbe012017-11-24 17:34:44 +010096 pool_free(pool_head_connection, appctx);
Emeric Brun1138fd02017-06-19 12:38:55 +020097 HA_ATOMIC_SUB(&nb_applets, 1);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020098}
Emeric Brunc7306062017-06-26 16:36:53 +020099static inline void appctx_free(struct appctx *appctx)
100{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100101 HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200102 if (appctx->state & APPLET_RUNNING) {
103 appctx->state |= APPLET_WANT_DIE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100104 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200105 return;
106 }
107 __appctx_free(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100108 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200109}
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200110
Willy Tarreau81f38d62015-04-13 17:11:11 +0200111/* wakes up an applet when conditions have changed */
Emeric Brunc7306062017-06-26 16:36:53 +0200112static inline void __appctx_wakeup(struct appctx *appctx)
Willy Tarreau81f38d62015-04-13 17:11:11 +0200113{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100114 if (LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau64bca9d2015-09-25 17:39:23 +0200115 LIST_ADDQ(&applet_active_queue, &appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100116 applets_active_queue++;
Christopher Faulet595d7b72017-11-14 11:28:52 +0100117 active_applets_mask |= appctx->thread_mask;
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100118 }
Willy Tarreau81f38d62015-04-13 17:11:11 +0200119}
120
Emeric Brunc7306062017-06-26 16:36:53 +0200121static inline void appctx_wakeup(struct appctx *appctx)
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200122{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100123 HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200124 if (appctx->state & APPLET_RUNNING) {
125 appctx->state |= APPLET_WOKEN_UP;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100126 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200127 return;
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200128 }
Emeric Brunc7306062017-06-26 16:36:53 +0200129 __appctx_wakeup(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100130 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200131}
132
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100133/* Callback used to wake up an applet when a buffer is available. The applet
134 * <appctx> is woken up is if it is not already in the list of "active"
135 * applets. This functions returns 1 is the stream is woken up, otherwise it
Emeric Brunc7306062017-06-26 16:36:53 +0200136 * returns 0. If task is running we request we check if woken was already
137 * requested */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100138static inline int appctx_res_wakeup(struct appctx *appctx)
139{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100140 HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200141 if (appctx->state & APPLET_RUNNING) {
142 if (appctx->state & APPLET_WOKEN_UP) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100143 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200144 return 0;
145 }
146 appctx->state |= APPLET_WOKEN_UP;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100147 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200148 return 1;
149 }
Emeric Brunc7306062017-06-26 16:36:53 +0200150 __appctx_wakeup(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100151 HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100152 return 1;
153}
154
155
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200156#endif /* _PROTO_APPLET_H */
157
158/*
159 * Local variables:
160 * c-indent-level: 8
161 * c-basic-offset: 8
162 * End:
163 */