blob: b0783e6a639b2f3419eaa3224d99d62ffaa80a47 [file] [log] [blame]
Willy Tarreau81f38d62015-04-13 17:11:11 +02001/*
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 Fauleta73e59b2016-12-09 17:30:18 +010019#include <proto/channel.h>
Willy Tarreau3c595ac2015-04-19 09:59:31 +020020#include <proto/stream.h>
21#include <proto/stream_interface.h>
Willy Tarreau81f38d62015-04-13 17:11:11 +020022
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010023unsigned int nb_applets = 0;
24unsigned int applets_active_queue = 0;
25
Emeric Brun1138fd02017-06-19 12:38:55 +020026#ifdef USE_THREAD
27HA_SPINLOCK_T applet_active_lock; /* spin lock related to applet active queue */
28#endif
29
Willy Tarreau64bca9d2015-09-25 17:39:23 +020030struct list applet_active_queue = LIST_HEAD_INIT(applet_active_queue);
Willy Tarreau3c595ac2015-04-19 09:59:31 +020031
32void applet_run_active()
33{
Emeric Brunc7306062017-06-26 16:36:53 +020034 struct appctx *curr, *next;
Willy Tarreau3c595ac2015-04-19 09:59:31 +020035 struct stream_interface *si;
Emeric Brunc7306062017-06-26 16:36:53 +020036 struct list applet_cur_queue = LIST_HEAD_INIT(applet_cur_queue);
Willy Tarreau3c595ac2015-04-19 09:59:31 +020037
Christopher Faulet6c57dc92017-06-27 16:07:01 +020038 if (!applets_active_queue)
39 return;
40
Emeric Brun1138fd02017-06-19 12:38:55 +020041 SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
42
Emeric Brunc7306062017-06-26 16:36:53 +020043 curr = LIST_NEXT(&applet_active_queue, typeof(curr), runq);
44 while (&curr->runq != &applet_active_queue) {
45 next = LIST_NEXT(&curr->runq, typeof(next), runq);
Emeric Brun1138fd02017-06-19 12:38:55 +020046 if (curr->process_mask & (1UL << tid)) {
47 LIST_DEL(&curr->runq);
48 curr->state = APPLET_RUNNING;
49 LIST_ADDQ(&applet_cur_queue, &curr->runq);
50 applets_active_queue--;
51 }
Emeric Brunc7306062017-06-26 16:36:53 +020052 curr = next;
53 }
Willy Tarreau99942382015-09-25 17:56:16 +020054
Emeric Brun1138fd02017-06-19 12:38:55 +020055 SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
56
Willy Tarreau99942382015-09-25 17:56:16 +020057 /* The list is only scanned from the head. This guarantees that if any
58 * applet removes another one, there is no side effect while walking
59 * through the list.
60 */
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010061 while (!LIST_ISEMPTY(&applet_cur_queue)) {
62 curr = LIST_ELEM(applet_cur_queue.n, typeof(curr), runq);
Willy Tarreau3c595ac2015-04-19 09:59:31 +020063 si = curr->owner;
64
Christopher Fauleta73e59b2016-12-09 17:30:18 +010065 /* Now we'll try to allocate the input buffer. We wake up the
66 * applet in all cases. So this is the applet responsibility to
67 * check if this buffer was allocated or not. This let a chance
68 * for applets to do some other processing if needed. */
69 if (!channel_alloc_buffer(si_ic(si), &curr->buffer_wait))
70 si_applet_cant_put(si);
Willy Tarreau3c595ac2015-04-19 09:59:31 +020071
Willy Tarreaufe127932015-04-21 19:23:39 +020072 /* We always pretend the applet can't get and doesn't want to
73 * put, it's up to it to change this if needed. This ensures
74 * that one applet which ignores any event will not spin.
75 */
76 si_applet_cant_get(si);
77 si_applet_stop_put(si);
78
Willy Tarreau3c595ac2015-04-19 09:59:31 +020079 curr->applet->fct(curr);
Willy Tarreauaa977ba2015-09-25 11:45:06 +020080 si_applet_wake_cb(si);
Christopher Fauleta73e59b2016-12-09 17:30:18 +010081 channel_release_buffer(si_ic(si), &curr->buffer_wait);
Willy Tarreau99942382015-09-25 17:56:16 +020082
Christopher Faulet1cbe74c2016-12-06 09:13:22 +010083 if (applet_cur_queue.n == &curr->runq) {
Willy Tarreau99942382015-09-25 17:56:16 +020084 /* curr was left in the list, move it back to the active list */
85 LIST_DEL(&curr->runq);
Emeric Brunc7306062017-06-26 16:36:53 +020086 LIST_INIT(&curr->runq);
Emeric Brun1138fd02017-06-19 12:38:55 +020087 SPIN_LOCK(APPLETS_LOCK, &applet_active_lock);
Emeric Brunc7306062017-06-26 16:36:53 +020088 if (curr->state & APPLET_WANT_DIE) {
89 curr->state = APPLET_SLEEPING;
90 __appctx_free(curr);
91 }
92 else {
93 if (curr->state & APPLET_WOKEN_UP) {
94 curr->state = APPLET_SLEEPING;
95 __appctx_wakeup(curr);
96 }
97 else {
98 curr->state = APPLET_SLEEPING;
99 }
100 }
Emeric Brun1138fd02017-06-19 12:38:55 +0200101 SPIN_UNLOCK(APPLETS_LOCK, &applet_active_lock);
Willy Tarreau99942382015-09-25 17:56:16 +0200102 }
Willy Tarreau3c595ac2015-04-19 09:59:31 +0200103 }
104}
Emeric Brun1138fd02017-06-19 12:38:55 +0200105
106__attribute__((constructor))
107static void __applet_init(void)
108{
109 SPIN_INIT(&applet_active_lock);
110}