blob: 51a7e26f0a780625fafaa8a93f6a3c8fbb7a0305 [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;
Emeric Brun1138fd02017-06-19 12:38:55 +020034#ifdef USE_THREAD
35extern HA_SPINLOCK_T applet_active_lock;
36#endif
Willy Tarreau64bca9d2015-09-25 17:39:23 +020037extern struct list applet_active_queue;
Willy Tarreau81f38d62015-04-13 17:11:11 +020038
Willy Tarreau3c595ac2015-04-19 09:59:31 +020039void applet_run_active();
40
Christopher Fauleta73e59b2016-12-09 17:30:18 +010041
42static int inline appctx_res_wakeup(struct appctx *appctx);
43
44
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020045/* Initializes all required fields for a new appctx. Note that it does the
46 * minimum acceptable initialization for an appctx. This means only the
47 * 3 integer states st0, st1, st2 are zeroed.
48 */
Emeric Brun1138fd02017-06-19 12:38:55 +020049static inline void appctx_init(struct appctx *appctx, unsigned long thread_mask)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020050{
51 appctx->st0 = appctx->st1 = appctx->st2 = 0;
Thierry FOURNIER / OZON.IO6a22dcb2016-11-12 10:51:33 +010052 appctx->io_release = NULL;
Willy Tarreauf65610a2017-10-31 16:06:06 +010053 appctx->thread_mask = thread_mask;
Emeric Brunc7306062017-06-26 16:36:53 +020054 appctx->state = APPLET_SLEEPING;
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020055}
56
57/* Tries to allocate a new appctx and initialize its main fields. The appctx
58 * is returned on success, NULL on failure. The appctx must be released using
59 * pool_free2(connection) or appctx_free(), since it's allocated from the
60 * connection pool. <applet> is assigned as the applet, but it can be NULL.
61 */
Emeric Brun1138fd02017-06-19 12:38:55 +020062static inline struct appctx *appctx_new(struct applet *applet, unsigned long thread_mask)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020063{
64 struct appctx *appctx;
65
66 appctx = pool_alloc2(pool2_connection);
67 if (likely(appctx != NULL)) {
68 appctx->obj_type = OBJ_TYPE_APPCTX;
69 appctx->applet = applet;
Emeric Brun1138fd02017-06-19 12:38:55 +020070 appctx_init(appctx, thread_mask);
Willy Tarreau81f38d62015-04-13 17:11:11 +020071 LIST_INIT(&appctx->runq);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010072 LIST_INIT(&appctx->buffer_wait.list);
73 appctx->buffer_wait.target = appctx;
74 appctx->buffer_wait.wakeup_cb = (int (*)(void *))appctx_res_wakeup;
Emeric Brun1138fd02017-06-19 12:38:55 +020075 HA_ATOMIC_ADD(&nb_applets, 1);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020076 }
77 return appctx;
78}
79
80/* Releases an appctx previously allocated by appctx_new(). Note that
81 * we share the connection pool.
82 */
Emeric Brunc7306062017-06-26 16:36:53 +020083static inline void __appctx_free(struct appctx *appctx)
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020084{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010085 if (!LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau81f38d62015-04-13 17:11:11 +020086 LIST_DEL(&appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010087 applets_active_queue--;
88 }
Emeric Brun1138fd02017-06-19 12:38:55 +020089
Christopher Fauleta73e59b2016-12-09 17:30:18 +010090 if (!LIST_ISEMPTY(&appctx->buffer_wait.list)) {
Emeric Bruna1dd2432017-06-21 15:42:52 +020091 SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010092 LIST_DEL(&appctx->buffer_wait.list);
93 LIST_INIT(&appctx->buffer_wait.list);
Emeric Bruna1dd2432017-06-21 15:42:52 +020094 SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010095 }
Emeric Brun1138fd02017-06-19 12:38:55 +020096
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020097 pool_free2(pool2_connection, appctx);
Emeric Brun1138fd02017-06-19 12:38:55 +020098 HA_ATOMIC_SUB(&nb_applets, 1);
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020099}
Emeric Brunc7306062017-06-26 16:36:53 +0200100static inline void appctx_free(struct appctx *appctx)
101{
Emeric Brun1138fd02017-06-19 12:38:55 +0200102 SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200103 if (appctx->state & APPLET_RUNNING) {
104 appctx->state |= APPLET_WANT_DIE;
Emeric Brun1138fd02017-06-19 12:38:55 +0200105 SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200106 return;
107 }
108 __appctx_free(appctx);
Emeric Brun1138fd02017-06-19 12:38:55 +0200109 SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200110}
Willy Tarreau8a8d83b2015-04-13 13:24:54 +0200111
Willy Tarreau81f38d62015-04-13 17:11:11 +0200112/* wakes up an applet when conditions have changed */
Emeric Brunc7306062017-06-26 16:36:53 +0200113static inline void __appctx_wakeup(struct appctx *appctx)
Willy Tarreau81f38d62015-04-13 17:11:11 +0200114{
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100115 if (LIST_ISEMPTY(&appctx->runq)) {
Willy Tarreau64bca9d2015-09-25 17:39:23 +0200116 LIST_ADDQ(&applet_active_queue, &appctx->runq);
Christopher Faulet1cbe74c2016-12-06 09:13:22 +0100117 applets_active_queue++;
118 }
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{
Emeric Brun1138fd02017-06-19 12:38:55 +0200123 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;
Emeric Brun1138fd02017-06-19 12:38:55 +0200126 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);
Emeric Brun1138fd02017-06-19 12:38:55 +0200130 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{
Emeric Brun1138fd02017-06-19 12:38:55 +0200140 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) {
Emeric Brun1138fd02017-06-19 12:38:55 +0200143 SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +0200144 return 0;
145 }
146 appctx->state |= APPLET_WOKEN_UP;
Emeric Brun1138fd02017-06-19 12:38:55 +0200147 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);
Emeric Brun1138fd02017-06-19 12:38:55 +0200151 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 */