Wait for the destruction of sessions before tearing down main thread
[lttng-tools.git] / src / bin / lttng-sessiond / thread-utils.c
CommitLineData
a7333da7
JG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "lttng-sessiond.h"
21#include "utils.h"
22#include <common/utils.h>
99d688f2
JG
23#include <pthread.h>
24
25#define USEC_PER_SEC 1000000
a7333da7
JG
26
27/*
28 * Quit pipe for all threads. This permits a single cancellation point
29 * for all threads when receiving an event on the pipe.
30 */
31static int thread_quit_pipe[2] = { -1, -1 };
32
99d688f2
JG
33/*
34 * Allows threads to query the state of the client thread.
35 */
36static struct client_thread_state {
37 pthread_cond_t cond;
38 pthread_mutex_t lock;
39 bool is_running;
40} client_thread_state = {
41 .cond = PTHREAD_COND_INITIALIZER,
42 .lock = PTHREAD_MUTEX_INITIALIZER,
43 .is_running = false
44};
45
a7333da7
JG
46/*
47 * Init thread quit pipe.
48 *
49 * Return -1 on error or 0 if all pipes are created.
50 */
51static int __init_thread_quit_pipe(int *a_pipe)
52{
53 int ret, i;
54
55 ret = pipe(a_pipe);
56 if (ret < 0) {
57 PERROR("thread quit pipe");
58 goto error;
59 }
60
61 for (i = 0; i < 2; i++) {
62 ret = fcntl(a_pipe[i], F_SETFD, FD_CLOEXEC);
63 if (ret < 0) {
64 PERROR("fcntl");
65 goto error;
66 }
67 }
68
69error:
70 return ret;
71}
72
73int sessiond_init_thread_quit_pipe(void)
74{
75 return __init_thread_quit_pipe(thread_quit_pipe);
76}
77
78int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
79{
80 return (fd == thread_quit_pipe[0] && (events & LPOLLIN));
81}
82
83/*
84 * Wait for a notification on the quit pipe (with a timeout).
85 *
99d688f2
JG
86 * A timeout value of -1U means no timeout.
87 *
a7333da7
JG
88 * Returns 1 if the caller should quit, 0 if the timeout was reached, and
89 * -1 if an error was encountered.
90 */
91int sessiond_wait_for_quit_pipe(unsigned int timeout_us)
92{
93 int ret;
94 fd_set read_fds;
95 struct timeval timeout;
96
97 FD_ZERO(&read_fds);
98 FD_SET(thread_quit_pipe[0], &read_fds);
99 memset(&timeout, 0, sizeof(timeout));
99d688f2
JG
100 timeout.tv_sec = timeout_us / USEC_PER_SEC;
101 timeout.tv_usec = timeout_us % USEC_PER_SEC;
a7333da7
JG
102
103 while (true) {
104 ret = select(thread_quit_pipe[0] + 1, &read_fds, NULL, NULL,
99d688f2 105 timeout_us != -1U ? &timeout : NULL);
a7333da7
JG
106 if (ret < 0 && errno == EINTR) {
107 /* Retry on interrupt. */
108 continue;
109 } else {
110 break;
111 }
112 }
113
114 if (ret > 0) {
115 /* Should quit. */
116 ret = 1;
117 } else if (ret < 0 && errno != EINTR) {
118 /* Unknown error. */
119 PERROR("Failed to select() thread quit pipe");
120 ret = -1;
121 } else {
122 /* Timeout reached. */
123 ret = 0;
124 }
125
126 return ret;
127}
128
129int sessiond_notify_quit_pipe(void)
130{
131 return notify_thread_pipe(thread_quit_pipe[1]);
132}
133
134void sessiond_close_quit_pipe(void)
135{
136 utils_close_pipe(thread_quit_pipe);
137}
138
99d688f2
JG
139void sessiond_set_client_thread_state(bool running)
140{
141 pthread_mutex_lock(&client_thread_state.lock);
142 client_thread_state.is_running = running;
143 pthread_cond_broadcast(&client_thread_state.cond);
144 pthread_mutex_unlock(&client_thread_state.lock);
145}
146
147void sessiond_wait_client_thread_stopped(void)
148{
149 pthread_mutex_lock(&client_thread_state.lock);
150 while (client_thread_state.is_running) {
151 pthread_cond_wait(&client_thread_state.cond,
152 &client_thread_state.lock);
153 }
154 pthread_mutex_unlock(&client_thread_state.lock);
155}
156
a7333da7
JG
157static
158int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size,
159 int *a_pipe)
160{
161 int ret;
162
163 assert(events);
164
165 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
166 if (ret < 0) {
167 goto error;
168 }
169
170 /* Add quit pipe */
171 ret = lttng_poll_add(events, a_pipe[0], LPOLLIN | LPOLLERR);
172 if (ret < 0) {
173 goto error;
174 }
175
176 return 0;
177
178error:
179 return ret;
180}
181
182/*
183 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
184 */
185int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
186{
187 return __sessiond_set_thread_pollset(events, size, thread_quit_pipe);
188}
This page took 0.031688 seconds and 5 git commands to generate.