| 1 | /* |
| 2 | * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 3 | * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: LGPL-2.1-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include "waiter.h" |
| 10 | #include <urcu/uatomic.h> |
| 11 | #include <urcu/futex.h> |
| 12 | #include <assert.h> |
| 13 | #include "error.h" |
| 14 | #include <poll.h> |
| 15 | |
| 16 | /* |
| 17 | * Number of busy-loop attempts before waiting on futex. |
| 18 | */ |
| 19 | #define WAIT_ATTEMPTS 1000 |
| 20 | |
| 21 | enum waiter_state { |
| 22 | /* WAITER_WAITING is compared directly (futex compares it). */ |
| 23 | WAITER_WAITING = 0, |
| 24 | /* non-zero are used as masks. */ |
| 25 | WAITER_WOKEN_UP = (1 << 0), |
| 26 | WAITER_RUNNING = (1 << 1), |
| 27 | WAITER_TEARDOWN = (1 << 2), |
| 28 | }; |
| 29 | |
| 30 | LTTNG_HIDDEN |
| 31 | void lttng_waiter_init(struct lttng_waiter *waiter) |
| 32 | { |
| 33 | cds_wfs_node_init(&waiter->wait_queue_node); |
| 34 | uatomic_set(&waiter->state, WAITER_WAITING); |
| 35 | cmm_smp_mb(); |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * User must init "waiter" before passing its memory to waker thread. |
| 40 | */ |
| 41 | LTTNG_HIDDEN |
| 42 | void lttng_waiter_wait(struct lttng_waiter *waiter) |
| 43 | { |
| 44 | unsigned int i; |
| 45 | |
| 46 | DBG("Beginning of waiter wait period"); |
| 47 | /* Load and test condition before read state */ |
| 48 | cmm_smp_rmb(); |
| 49 | for (i = 0; i < WAIT_ATTEMPTS; i++) { |
| 50 | if (uatomic_read(&waiter->state) != WAITER_WAITING) { |
| 51 | goto skip_futex_wait; |
| 52 | } |
| 53 | caa_cpu_relax(); |
| 54 | } |
| 55 | while (futex_noasync(&waiter->state, FUTEX_WAIT, WAITER_WAITING, |
| 56 | NULL, NULL, 0)) { |
| 57 | switch (errno) { |
| 58 | case EWOULDBLOCK: |
| 59 | /* Value already changed. */ |
| 60 | goto skip_futex_wait; |
| 61 | case EINTR: |
| 62 | /* Retry if interrupted by signal. */ |
| 63 | break; /* Get out of switch. */ |
| 64 | default: |
| 65 | /* Unexpected error. */ |
| 66 | PERROR("futex_noasync"); |
| 67 | abort(); |
| 68 | } |
| 69 | } |
| 70 | skip_futex_wait: |
| 71 | |
| 72 | /* Tell waker thread than we are running. */ |
| 73 | uatomic_or(&waiter->state, WAITER_RUNNING); |
| 74 | |
| 75 | /* |
| 76 | * Wait until waker thread lets us know it's ok to tear down |
| 77 | * memory allocated for struct lttng_waiter. |
| 78 | */ |
| 79 | for (i = 0; i < WAIT_ATTEMPTS; i++) { |
| 80 | if (uatomic_read(&waiter->state) & WAITER_TEARDOWN) { |
| 81 | break; |
| 82 | } |
| 83 | caa_cpu_relax(); |
| 84 | } |
| 85 | while (!(uatomic_read(&waiter->state) & WAITER_TEARDOWN)) { |
| 86 | poll(NULL, 0, 10); |
| 87 | } |
| 88 | assert(uatomic_read(&waiter->state) & WAITER_TEARDOWN); |
| 89 | DBG("End of waiter wait period"); |
| 90 | } |
| 91 | |
| 92 | /* |
| 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. |
| 96 | */ |
| 97 | LTTNG_HIDDEN |
| 98 | void lttng_waiter_wake_up(struct lttng_waiter *waiter) |
| 99 | { |
| 100 | cmm_smp_mb(); |
| 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"); |
| 107 | abort(); |
| 108 | } |
| 109 | } |
| 110 | /* Allow teardown of struct urcu_wait memory. */ |
| 111 | uatomic_or(&waiter->state, WAITER_TEARDOWN); |
| 112 | } |