Fix: hash table cleanup call_rcu deadlock
[lttng-tools.git] / src / bin / lttng-sessiond / health.h
1 /*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #ifndef _HEALTH_H
19 #define _HEALTH_H
20
21 #include <assert.h>
22 #include <time.h>
23 #include <pthread.h>
24 #include <urcu/tls-compat.h>
25 #include <urcu/uatomic.h>
26 #include <urcu/list.h>
27
28 /*
29 * These are the value added to the current state depending of the position in
30 * the thread where is either waiting on a poll() or running in the code.
31 */
32 #define HEALTH_POLL_VALUE (1UL << 0)
33 #define HEALTH_CODE_VALUE (1UL << 1)
34
35 #define HEALTH_IS_IN_POLL(x) ((x) & HEALTH_POLL_VALUE)
36
37 enum health_flags {
38 HEALTH_ERROR = (1U << 0),
39 };
40
41 enum health_type {
42 HEALTH_TYPE_CMD = 0,
43 HEALTH_TYPE_APP_MANAGE = 1,
44 HEALTH_TYPE_APP_REG = 2,
45 HEALTH_TYPE_KERNEL = 3,
46 HEALTH_TYPE_CONSUMER = 4,
47 HEALTH_TYPE_HT_CLEANUP = 5,
48
49 HEALTH_NUM_TYPE,
50 };
51
52 struct health_tls_state_list {
53 struct cds_list_head head;
54 };
55
56 struct health_state {
57 /*
58 * last counter and last_time are only read and updated by the health_check
59 * thread (single updater).
60 */
61 unsigned long last;
62 struct timespec last_time;
63
64 /*
65 * current and flags are updated by multiple threads concurrently.
66 */
67 unsigned long current; /* progress counter, updated atomically */
68 enum health_flags flags; /* other flags, updated atomically */
69 enum health_type type; /* Indicates the nature of the thread. */
70 /* Node of the global TLS state list. */
71 struct cds_list_head node;
72 };
73
74 /* Declare TLS health state. */
75 extern DECLARE_URCU_TLS(struct health_state, health_state);
76
77 /*
78 * Update current counter by 1 to indicate that the thread entered or left a
79 * blocking state caused by a poll(). If the counter's value is not an even
80 * number (meaning a code execution flow), an assert() is raised.
81 */
82 static inline void health_poll_entry(void)
83 {
84 /* Code MUST be in code execution state which is an even number. */
85 assert(!(uatomic_read(&URCU_TLS(health_state).current)
86 & HEALTH_POLL_VALUE));
87
88 uatomic_add(&URCU_TLS(health_state).current, HEALTH_POLL_VALUE);
89 }
90
91 /*
92 * Update current counter by 1 indicating the exit of a poll or blocking call.
93 * If the counter's value is not an odd number (a poll execution), an assert()
94 * is raised.
95 */
96 static inline void health_poll_exit(void)
97 {
98 /* Code MUST be in poll execution state which is an odd number. */
99 assert(uatomic_read(&URCU_TLS(health_state).current)
100 & HEALTH_POLL_VALUE);
101
102 uatomic_add(&URCU_TLS(health_state).current, HEALTH_POLL_VALUE);
103 }
104
105 /*
106 * Update current counter by 2 indicates progress in execution of a
107 * thread.
108 */
109 static inline void health_code_update(void)
110 {
111 uatomic_add(&URCU_TLS(health_state).current, HEALTH_CODE_VALUE);
112 }
113
114 /*
115 * Set health "error" flag.
116 */
117 static inline void health_error(void)
118 {
119 uatomic_or(&URCU_TLS(health_state).flags, HEALTH_ERROR);
120 }
121
122 int health_check_state(enum health_type type);
123 void health_register(enum health_type type);
124 void health_unregister(void);
125
126 #endif /* _HEALTH_H */
This page took 0.031848 seconds and 5 git commands to generate.