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>
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.
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.
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.
20 #include "lttng-sessiond.h"
22 #include <common/utils.h>
25 #define USEC_PER_SEC 1000000
28 * Quit pipe for all threads. This permits a single cancellation point
29 * for all threads when receiving an event on the pipe.
31 static int thread_quit_pipe
[2] = { -1, -1 };
34 * Allows threads to query the state of the client thread.
36 static struct client_thread_state
{
40 } client_thread_state
= {
41 .cond
= PTHREAD_COND_INITIALIZER
,
42 .lock
= PTHREAD_MUTEX_INITIALIZER
,
47 * Init thread quit pipe.
49 * Return -1 on error or 0 if all pipes are created.
51 static int __init_thread_quit_pipe(int *a_pipe
)
57 PERROR("thread quit pipe");
61 for (i
= 0; i
< 2; i
++) {
62 ret
= fcntl(a_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
73 int sessiond_init_thread_quit_pipe(void)
75 return __init_thread_quit_pipe(thread_quit_pipe
);
78 int sessiond_check_thread_quit_pipe(int fd
, uint32_t events
)
80 return (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
));
84 * Wait for a notification on the quit pipe (with a timeout).
86 * A timeout value of -1U means no timeout.
88 * Returns 1 if the caller should quit, 0 if the timeout was reached, and
89 * -1 if an error was encountered.
91 int sessiond_wait_for_quit_pipe(unsigned int timeout_us
)
95 struct timeval timeout
;
98 FD_SET(thread_quit_pipe
[0], &read_fds
);
99 memset(&timeout
, 0, sizeof(timeout
));
100 timeout
.tv_sec
= timeout_us
/ USEC_PER_SEC
;
101 timeout
.tv_usec
= timeout_us
% USEC_PER_SEC
;
104 ret
= select(thread_quit_pipe
[0] + 1, &read_fds
, NULL
, NULL
,
105 timeout_us
!= -1U ? &timeout
: NULL
);
106 if (ret
< 0 && errno
== EINTR
) {
107 /* Retry on interrupt. */
117 } else if (ret
< 0 && errno
!= EINTR
) {
119 PERROR("Failed to select() thread quit pipe");
122 /* Timeout reached. */
129 int sessiond_notify_quit_pipe(void)
131 return notify_thread_pipe(thread_quit_pipe
[1]);
134 void sessiond_close_quit_pipe(void)
136 utils_close_pipe(thread_quit_pipe
);
139 void sessiond_set_client_thread_state(bool running
)
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
);
147 void sessiond_wait_client_thread_stopped(void)
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
);
154 pthread_mutex_unlock(&client_thread_state
.lock
);
158 int __sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
,
165 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
171 ret
= lttng_poll_add(events
, a_pipe
[0], LPOLLIN
| LPOLLERR
);
183 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
185 int sessiond_set_thread_pollset(struct lttng_poll_event
*events
, size_t size
)
187 return __sessiond_set_thread_pollset(events
, size
, thread_quit_pipe
);