2 * Copyright (C) 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 * 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License, version 2.1 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 * This code is originally adapted from userspace-rcu's urcu-wait.h
22 #include <urcu/uatomic.h>
23 #include <urcu/futex.h>
29 * Number of busy-loop attempts before waiting on futex.
31 #define WAIT_ATTEMPTS 1000
34 /* WAITER_WAITING is compared directly (futex compares it). */
36 /* non-zero are used as masks. */
37 WAITER_WOKEN_UP
= (1 << 0),
38 WAITER_RUNNING
= (1 << 1),
39 WAITER_TEARDOWN
= (1 << 2),
43 void lttng_waiter_init(struct lttng_waiter
*waiter
)
45 cds_wfs_node_init(&waiter
->wait_queue_node
);
46 uatomic_set(&waiter
->state
, WAITER_WAITING
);
51 * User must init "waiter" before passing its memory to waker thread.
54 void lttng_waiter_wait(struct lttng_waiter
*waiter
)
58 DBG("Beginning of waiter wait period");
59 /* Load and test condition before read state */
61 for (i
= 0; i
< WAIT_ATTEMPTS
; i
++) {
62 if (uatomic_read(&waiter
->state
) != WAITER_WAITING
) {
67 while (futex_noasync(&waiter
->state
, FUTEX_WAIT
, WAITER_WAITING
,
71 /* Value already changed. */
74 /* Retry if interrupted by signal. */
75 break; /* Get out of switch. */
77 /* Unexpected error. */
78 PERROR("futex_noasync");
84 /* Tell waker thread than we are running. */
85 uatomic_or(&waiter
->state
, WAITER_RUNNING
);
88 * Wait until waker thread lets us know it's ok to tear down
89 * memory allocated for struct lttng_waiter.
91 for (i
= 0; i
< WAIT_ATTEMPTS
; i
++) {
92 if (uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
) {
97 while (!(uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
)) {
100 assert(uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
);
101 DBG("End of waiter wait period");
105 * Note: lttng_waiter_wake needs waiter to stay allocated throughout its
106 * execution. In this scheme, the waiter owns the node memory, and we only allow
107 * it to free this memory when it sees the WAITER_TEARDOWN flag.
110 void lttng_waiter_wake_up(struct lttng_waiter
*waiter
)
113 assert(uatomic_read(&waiter
->state
) == WAITER_WAITING
);
114 uatomic_set(&waiter
->state
, WAITER_WOKEN_UP
);
115 if (!(uatomic_read(&waiter
->state
) & WAITER_RUNNING
)) {
116 if (futex_noasync(&waiter
->state
, FUTEX_WAKE
, 1,
117 NULL
, NULL
, 0) < 0) {
118 PERROR("futex_noasync");
122 /* Allow teardown of struct urcu_wait memory. */
123 uatomic_or(&waiter
->state
, WAITER_TEARDOWN
);
This page took 0.035394 seconds and 4 git commands to generate.