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 | |
Willy Tarreau | 64bca9d | 2015-09-25 17:39:23 +0200 | [diff] [blame] | 32 | extern struct list applet_active_queue; |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 33 | |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 34 | void applet_run_active(); |
| 35 | |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 36 | /* Initializes all required fields for a new appctx. Note that it does the |
| 37 | * minimum acceptable initialization for an appctx. This means only the |
| 38 | * 3 integer states st0, st1, st2 are zeroed. |
| 39 | */ |
| 40 | static inline void appctx_init(struct appctx *appctx) |
| 41 | { |
| 42 | appctx->st0 = appctx->st1 = appctx->st2 = 0; |
Thierry FOURNIER / OZON.IO | 6a22dcb | 2016-11-12 10:51:33 +0100 | [diff] [blame] | 43 | appctx->io_release = NULL; |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* Tries to allocate a new appctx and initialize its main fields. The appctx |
| 47 | * is returned on success, NULL on failure. The appctx must be released using |
| 48 | * pool_free2(connection) or appctx_free(), since it's allocated from the |
| 49 | * connection pool. <applet> is assigned as the applet, but it can be NULL. |
| 50 | */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 51 | static inline struct appctx *appctx_new(struct applet *applet) |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 52 | { |
| 53 | struct appctx *appctx; |
| 54 | |
| 55 | appctx = pool_alloc2(pool2_connection); |
| 56 | if (likely(appctx != NULL)) { |
| 57 | appctx->obj_type = OBJ_TYPE_APPCTX; |
| 58 | appctx->applet = applet; |
| 59 | appctx_init(appctx); |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 60 | LIST_INIT(&appctx->runq); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 61 | } |
| 62 | return appctx; |
| 63 | } |
| 64 | |
| 65 | /* Releases an appctx previously allocated by appctx_new(). Note that |
| 66 | * we share the connection pool. |
| 67 | */ |
| 68 | static inline void appctx_free(struct appctx *appctx) |
| 69 | { |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 70 | if (!LIST_ISEMPTY(&appctx->runq)) |
| 71 | LIST_DEL(&appctx->runq); |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 72 | pool_free2(pool2_connection, appctx); |
| 73 | } |
| 74 | |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 75 | /* wakes up an applet when conditions have changed */ |
| 76 | static inline void appctx_wakeup(struct appctx *appctx) |
| 77 | { |
| 78 | if (LIST_ISEMPTY(&appctx->runq)) |
Willy Tarreau | 64bca9d | 2015-09-25 17:39:23 +0200 | [diff] [blame] | 79 | LIST_ADDQ(&applet_active_queue, &appctx->runq); |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 82 | /* removes an applet from the list of active applets */ |
| 83 | static inline void appctx_pause(struct appctx *appctx) |
| 84 | { |
| 85 | if (!LIST_ISEMPTY(&appctx->runq)) { |
| 86 | LIST_DEL(&appctx->runq); |
| 87 | LIST_INIT(&appctx->runq); |
| 88 | } |
| 89 | } |
| 90 | |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 91 | #endif /* _PROTO_APPLET_H */ |
| 92 | |
| 93 | /* |
| 94 | * Local variables: |
| 95 | * c-indent-level: 8 |
| 96 | * c-basic-offset: 8 |
| 97 | * End: |
| 98 | */ |