2 * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: LGPL-2.1-only
10 #include <urcu/uatomic.h>
11 #include <urcu/futex.h>
17 * Number of busy-loop attempts before waiting on futex.
19 #define WAIT_ATTEMPTS 1000
22 /* WAITER_WAITING is compared directly (futex compares it). */
24 /* non-zero are used as masks. */
25 WAITER_WOKEN_UP
= (1 << 0),
26 WAITER_RUNNING
= (1 << 1),
27 WAITER_TEARDOWN
= (1 << 2),
31 void lttng_waiter_init(struct lttng_waiter
*waiter
)
33 cds_wfs_node_init(&waiter
->wait_queue_node
);
34 uatomic_set(&waiter
->state
, WAITER_WAITING
);
39 * User must init "waiter" before passing its memory to waker thread.
42 void lttng_waiter_wait(struct lttng_waiter
*waiter
)
46 DBG("Beginning of waiter wait period");
47 /* Load and test condition before read state */
49 for (i
= 0; i
< WAIT_ATTEMPTS
; i
++) {
50 if (uatomic_read(&waiter
->state
) != WAITER_WAITING
) {
55 while (futex_noasync(&waiter
->state
, FUTEX_WAIT
, WAITER_WAITING
,
59 /* Value already changed. */
62 /* Retry if interrupted by signal. */
63 break; /* Get out of switch. */
65 /* Unexpected error. */
66 PERROR("futex_noasync");
72 /* Tell waker thread than we are running. */
73 uatomic_or(&waiter
->state
, WAITER_RUNNING
);
76 * Wait until waker thread lets us know it's ok to tear down
77 * memory allocated for struct lttng_waiter.
79 for (i
= 0; i
< WAIT_ATTEMPTS
; i
++) {
80 if (uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
) {
85 while (!(uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
)) {
88 assert(uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
);
89 DBG("End of waiter wait period");
93 * Note: lttng_waiter_wake needs waiter to stay allocated throughout its
94 * execution. In this scheme, the waiter owns the node memory, and we only allow
95 * it to free this memory when it sees the WAITER_TEARDOWN flag.
98 void lttng_waiter_wake_up(struct lttng_waiter
*waiter
)
101 assert(uatomic_read(&waiter
->state
) == WAITER_WAITING
);
102 uatomic_set(&waiter
->state
, WAITER_WOKEN_UP
);
103 if (!(uatomic_read(&waiter
->state
) & WAITER_RUNNING
)) {
104 if (futex_noasync(&waiter
->state
, FUTEX_WAKE
, 1,
105 NULL
, NULL
, 0) < 0) {
106 PERROR("futex_noasync");
110 /* Allow teardown of struct urcu_wait memory. */
111 uatomic_or(&waiter
->state
, WAITER_TEARDOWN
);
This page took 0.032056 seconds and 4 git commands to generate.