Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions managing applets |
| 3 | * |
| 4 | * Copyright 2000-2015 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | |
| 16 | #include <common/config.h> |
| 17 | #include <common/mini-clist.h> |
| 18 | #include <proto/applet.h> |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 19 | #include <proto/channel.h> |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 20 | #include <proto/stream.h> |
| 21 | #include <proto/stream_interface.h> |
Willy Tarreau | 81f38d6 | 2015-04-13 17:11:11 +0200 | [diff] [blame] | 22 | |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 23 | unsigned int nb_applets = 0; |
| 24 | unsigned int applets_active_queue = 0; |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 25 | __decl_hathreads(HA_SPINLOCK_T applet_active_lock); /* spin lock related to applet active queue */ |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | 64bca9d | 2015-09-25 17:39:23 +0200 | [diff] [blame] | 27 | struct list applet_active_queue = LIST_HEAD_INIT(applet_active_queue); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 28 | |
| 29 | void applet_run_active() |
| 30 | { |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 31 | struct appctx *curr, *next; |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 32 | struct stream_interface *si; |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 33 | struct list applet_cur_queue = LIST_HEAD_INIT(applet_cur_queue); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 34 | |
Christopher Faulet | 6c57dc9 | 2017-06-27 16:07:01 +0200 | [diff] [blame] | 35 | if (!applets_active_queue) |
| 36 | return; |
| 37 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 38 | HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 39 | |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 40 | curr = LIST_NEXT(&applet_active_queue, typeof(curr), runq); |
| 41 | while (&curr->runq != &applet_active_queue) { |
| 42 | next = LIST_NEXT(&curr->runq, typeof(next), runq); |
Willy Tarreau | f65610a | 2017-10-31 16:06:06 +0100 | [diff] [blame] | 43 | if (curr->thread_mask & tid_bit) { |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 44 | LIST_DEL(&curr->runq); |
| 45 | curr->state = APPLET_RUNNING; |
| 46 | LIST_ADDQ(&applet_cur_queue, &curr->runq); |
| 47 | applets_active_queue--; |
| 48 | } |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 49 | curr = next; |
| 50 | } |
Willy Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 51 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 52 | HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 54 | /* The list is only scanned from the head. This guarantees that if any |
| 55 | * applet removes another one, there is no side effect while walking |
| 56 | * through the list. |
| 57 | */ |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 58 | while (!LIST_ISEMPTY(&applet_cur_queue)) { |
| 59 | curr = LIST_ELEM(applet_cur_queue.n, typeof(curr), runq); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 60 | si = curr->owner; |
| 61 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 62 | /* Now we'll try to allocate the input buffer. We wake up the |
| 63 | * applet in all cases. So this is the applet responsibility to |
| 64 | * check if this buffer was allocated or not. This let a chance |
| 65 | * for applets to do some other processing if needed. */ |
| 66 | if (!channel_alloc_buffer(si_ic(si), &curr->buffer_wait)) |
| 67 | si_applet_cant_put(si); |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | fe12793 | 2015-04-21 19:23:39 +0200 | [diff] [blame] | 69 | /* We always pretend the applet can't get and doesn't want to |
| 70 | * put, it's up to it to change this if needed. This ensures |
| 71 | * that one applet which ignores any event will not spin. |
| 72 | */ |
| 73 | si_applet_cant_get(si); |
| 74 | si_applet_stop_put(si); |
| 75 | |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 76 | curr->applet->fct(curr); |
Willy Tarreau | aa977ba | 2015-09-25 11:45:06 +0200 | [diff] [blame] | 77 | si_applet_wake_cb(si); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 78 | channel_release_buffer(si_ic(si), &curr->buffer_wait); |
Willy Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 79 | |
Christopher Faulet | 1cbe74c | 2016-12-06 09:13:22 +0100 | [diff] [blame] | 80 | if (applet_cur_queue.n == &curr->runq) { |
Willy Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 81 | /* curr was left in the list, move it back to the active list */ |
| 82 | LIST_DEL(&curr->runq); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 83 | LIST_INIT(&curr->runq); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 84 | HA_SPIN_LOCK(APPLETS_LOCK, &applet_active_lock); |
Emeric Brun | c730606 | 2017-06-26 16:36:53 +0200 | [diff] [blame] | 85 | if (curr->state & APPLET_WANT_DIE) { |
| 86 | curr->state = APPLET_SLEEPING; |
| 87 | __appctx_free(curr); |
| 88 | } |
| 89 | else { |
| 90 | if (curr->state & APPLET_WOKEN_UP) { |
| 91 | curr->state = APPLET_SLEEPING; |
| 92 | __appctx_wakeup(curr); |
| 93 | } |
| 94 | else { |
| 95 | curr->state = APPLET_SLEEPING; |
| 96 | } |
| 97 | } |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 98 | HA_SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock); |
Willy Tarreau | 9994238 | 2015-09-25 17:56:16 +0200 | [diff] [blame] | 99 | } |
Willy Tarreau | 3c595ac | 2015-04-19 09:59:31 +0200 | [diff] [blame] | 100 | } |
| 101 | } |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 102 | |
| 103 | __attribute__((constructor)) |
| 104 | static void __applet_init(void) |
| 105 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 106 | HA_SPIN_INIT(&applet_active_lock); |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 107 | } |