Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 28 | #include <common/mini-clist.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 29 | #include <types/applet.h> |
| 30 | #include <proto/connection.h> |
| 31 | |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 32 | extern unsigned int nb_applets; |
| 33 | extern unsigned int applets_active_queue; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 34 | #ifdef USE_THREAD |
| 35 | extern HA_SPINLOCK_T applet_active_lock; |
| 36 | #endif |
Willy Tarreau | 64bca9d | 2015-09-25 17:39:23 +0200 | [diff] [blame] | 37 | extern struct list applet_active_queue; |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 39 | void applet_run_active(); |
| 40 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 41 | |
| 42 | static int inline appctx_res_wakeup(struct appctx *appctx); |
| 43 | |
| 44 | |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 45 | /* 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 Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 49 | static inline void appctx_init(struct appctx *appctx, unsigned long thread_mask) |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 50 | { |
| 51 | appctx->st0 = appctx->st1 = appctx->st2 = 0; |
Thierry FOURNIER / OZON.IO | 6a22dcb | 2016-11-12 10:51:33 +0100 | [diff] [blame] | 52 | appctx->io_release = NULL; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 53 | appctx->process_mask = thread_mask; |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 54 | appctx->state = APPLET_SLEEPING; |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 55 | } |
| 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 Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 62 | static inline struct appctx *appctx_new(struct applet *applet, unsigned long thread_mask) |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 63 | { |
| 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 Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 70 | appctx_init(appctx, thread_mask); |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 71 | LIST_INIT(&appctx->runq); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 72 | LIST_INIT(&appctx->buffer_wait.list); |
| 73 | appctx->buffer_wait.target = appctx; |
| 74 | appctx->buffer_wait.wakeup_cb = (int (*)(void *))appctx_res_wakeup; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 75 | HA_ATOMIC_ADD(&nb_applets, 1); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 76 | } |
| 77 | return appctx; |
| 78 | } |
| 79 | |
| 80 | /* Releases an appctx previously allocated by appctx_new(). Note that |
| 81 | * we share the connection pool. |
| 82 | */ |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 83 | static inline void __appctx_free(struct appctx *appctx) |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 84 | { |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 85 | if (!LIST_ISEMPTY(&appctx->runq)) { |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 86 | LIST_DEL(&appctx->runq); |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 87 | applets_active_queue--; |
| 88 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 89 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 90 | if (!LIST_ISEMPTY(&appctx->buffer_wait.list)) { |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 91 | SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 92 | LIST_DEL(&appctx->buffer_wait.list); |
| 93 | LIST_INIT(&appctx->buffer_wait.list); |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 94 | SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 95 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 96 | |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 97 | pool_free2(pool2_connection, appctx); |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 98 | HA_ATOMIC_SUB(&nb_applets, 1); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 99 | } |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 100 | static inline void appctx_free(struct appctx *appctx) |
| 101 | { |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 102 | SPIN_LOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 103 | if (appctx->state & APPLET_RUNNING) { |
| 104 | appctx->state |= APPLET_WANT_DIE; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 105 | SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | __appctx_free(appctx); |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 109 | SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 110 | } |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 111 | |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 112 | /* wakes up an applet when conditions have changed */ |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 113 | static inline void __appctx_wakeup(struct appctx *appctx) |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 114 | { |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 115 | if (LIST_ISEMPTY(&appctx->runq)) { |
Willy Tarreau | 64bca9d | 2015-09-25 17:39:23 +0200 | [diff] [blame] | 116 | LIST_ADDQ(&applet_active_queue, &appctx->runq); |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 117 | applets_active_queue++; |
| 118 | } |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 121 | static inline void appctx_wakeup(struct appctx *appctx) |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 122 | { |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 123 | SPIN_LOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 124 | if (appctx->state & APPLET_RUNNING) { |
| 125 | appctx->state |= APPLET_WOKEN_UP; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 126 | SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 127 | return; |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 128 | } |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 129 | __appctx_wakeup(appctx); |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 130 | SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 133 | /* 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 Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 136 | * returns 0. If task is running we request we check if woken was already |
| 137 | * requested */ |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 138 | static inline int appctx_res_wakeup(struct appctx *appctx) |
| 139 | { |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 140 | SPIN_LOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 141 | if (appctx->state & APPLET_RUNNING) { |
| 142 | if (appctx->state & APPLET_WOKEN_UP) { |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 143 | SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | appctx->state |= APPLET_WOKEN_UP; |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 147 | SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | if (!LIST_ISEMPTY(&appctx->runq)) { |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 152 | SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 153 | return 0; |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 154 | } |
| 155 | __appctx_wakeup(appctx); |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 156 | SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 161 | #endif /* _PROTO_APPLET_H */ |
| 162 | |
| 163 | /* |
| 164 | * Local variables: |
| 165 | * c-indent-level: 8 |
| 166 | * c-basic-offset: 8 |
| 167 | * End: |
| 168 | */ |