Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / bin / lttng-sessiond / thread-utils.c
index 3a8ddb3303639f75d86be3e1b7da7a232d2771ec..5e42d110fbc27d244acf9538f7c1da297b2c5b6b 100644 (file)
@@ -1,20 +1,10 @@
 /*
- * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
- *                      Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
- *               2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
+ * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include "lttng-sessiond.h"
 #include <common/utils.h>
 #include <pthread.h>
 
-#define USEC_PER_SEC 1000000
-
 /*
  * Quit pipe for all threads. This permits a single cancellation point
  * for all threads when receiving an event on the pipe.
  */
 static int thread_quit_pipe[2] = { -1, -1 };
 
-/*
- * Allows threads to query the state of the client thread.
- */
-static struct client_thread_state {
-       pthread_cond_t cond;
-       pthread_mutex_t lock;
-       bool is_running;
-} client_thread_state = {
-       .cond = PTHREAD_COND_INITIALIZER,
-       .lock = PTHREAD_MUTEX_INITIALIZER,
-       .is_running = false
-};
-
 /*
  * Init thread quit pipe.
  *
@@ -88,41 +63,38 @@ int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
  * Returns 1 if the caller should quit, 0 if the timeout was reached, and
  * -1 if an error was encountered.
  */
-int sessiond_wait_for_quit_pipe(unsigned int timeout_us)
+int sessiond_wait_for_quit_pipe(int timeout_ms)
 {
        int ret;
-       fd_set read_fds;
-       struct timeval timeout;
-
-       FD_ZERO(&read_fds);
-       FD_SET(thread_quit_pipe[0], &read_fds);
-       memset(&timeout, 0, sizeof(timeout));
-       timeout.tv_sec = timeout_us / USEC_PER_SEC;
-       timeout.tv_usec = timeout_us % USEC_PER_SEC;
-
-       while (true) {
-               ret = select(thread_quit_pipe[0] + 1, &read_fds, NULL, NULL,
-                               timeout_us != -1U ? &timeout : NULL);
-               if (ret < 0 && errno == EINTR) {
-                       /* Retry on interrupt. */
-                       continue;
-               } else {
-                       break;
-               }
-       }
+       struct lttng_poll_event events;
 
+       ret = lttng_poll_create(&events, 1, LTTNG_CLOEXEC);
+       if (ret < 0) {
+               PERROR("Failed to initialize poll/epoll set");
+               ret = -1;
+               goto end;
+       }
+       ret = lttng_poll_add(&events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
+       if (ret < 0) {
+               PERROR("Failed to add file descriptor to poll/epoll set");
+               ret = -1;
+               goto end_clean_poll;
+       }
+       ret = lttng_poll_wait(&events, timeout_ms);
        if (ret > 0) {
                /* Should quit. */
                ret = 1;
        } else if (ret < 0 && errno != EINTR) {
                /* Unknown error. */
-               PERROR("Failed to select() thread quit pipe");
+               PERROR("Failed to epoll()/poll() thread quit pipe");
                ret = -1;
        } else {
                /* Timeout reached. */
                ret = 0;
        }
-
+end_clean_poll:
+       lttng_poll_clean(&events);
+end:
        return ret;
 }
 
@@ -136,24 +108,6 @@ void sessiond_close_quit_pipe(void)
        utils_close_pipe(thread_quit_pipe);
 }
 
-void sessiond_set_client_thread_state(bool running)
-{
-       pthread_mutex_lock(&client_thread_state.lock);
-       client_thread_state.is_running = running;
-       pthread_cond_broadcast(&client_thread_state.cond);
-       pthread_mutex_unlock(&client_thread_state.lock);
-}
-
-void sessiond_wait_client_thread_stopped(void)
-{
-       pthread_mutex_lock(&client_thread_state.lock);
-       while (client_thread_state.is_running) {
-               pthread_cond_wait(&client_thread_state.cond,
-                               &client_thread_state.lock);
-       }
-       pthread_mutex_unlock(&client_thread_state.lock);
-}
-
 static
 int __sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size,
                int *a_pipe)
This page took 0.02509 seconds and 4 git commands to generate.