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>
16 * Number of busy-loop attempts before waiting on futex.
18 #define WAIT_ATTEMPTS 1000
21 /* WAITER_WAITING is compared directly (futex compares it). */
23 /* non-zero are used as masks. */
24 WAITER_WOKEN_UP
= (1 << 0),
25 WAITER_RUNNING
= (1 << 1),
26 WAITER_TEARDOWN
= (1 << 2),
29 void lttng_waiter_init(struct lttng_waiter
*waiter
)
31 cds_wfs_node_init(&waiter
->wait_queue_node
);
32 uatomic_set(&waiter
->state
, WAITER_WAITING
);
37 * User must init "waiter" before passing its memory to waker thread.
39 void lttng_waiter_wait(struct lttng_waiter
*waiter
)
43 DBG("Beginning of waiter wait period");
44 /* Load and test condition before read state */
46 for (i
= 0; i
< WAIT_ATTEMPTS
; i
++) {
47 if (uatomic_read(&waiter
->state
) != WAITER_WAITING
) {
52 while (futex_noasync(&waiter
->state
, FUTEX_WAIT
, WAITER_WAITING
,
56 /* Value already changed. */
59 /* Retry if interrupted by signal. */
60 break; /* Get out of switch. */
62 /* Unexpected error. */
63 PERROR("futex_noasync");
69 /* Tell waker thread than we are running. */
70 uatomic_or(&waiter
->state
, WAITER_RUNNING
);
73 * Wait until waker thread lets us know it's ok to tear down
74 * memory allocated for struct lttng_waiter.
76 for (i
= 0; i
< WAIT_ATTEMPTS
; i
++) {
77 if (uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
) {
82 while (!(uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
)) {
85 LTTNG_ASSERT(uatomic_read(&waiter
->state
) & WAITER_TEARDOWN
);
86 DBG("End of waiter wait period");
90 * Note: lttng_waiter_wake needs waiter to stay allocated throughout its
91 * execution. In this scheme, the waiter owns the node memory, and we only allow
92 * it to free this memory when it sees the WAITER_TEARDOWN flag.
94 void lttng_waiter_wake_up(struct lttng_waiter
*waiter
)
97 LTTNG_ASSERT(uatomic_read(&waiter
->state
) == WAITER_WAITING
);
98 uatomic_set(&waiter
->state
, WAITER_WOKEN_UP
);
99 if (!(uatomic_read(&waiter
->state
) & WAITER_RUNNING
)) {
100 if (futex_noasync(&waiter
->state
, FUTEX_WAKE
, 1,
101 NULL
, NULL
, 0) < 0) {
102 PERROR("futex_noasync");
106 /* Allow teardown of struct urcu_wait memory. */
107 uatomic_or(&waiter
->state
, WAITER_TEARDOWN
);
This page took 0.047332 seconds and 4 git commands to generate.