| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-only |
| 3 | * |
| 4 | * Copyright (C) 2021 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #include <pthread.h> |
| 8 | #include <errno.h> |
| 9 | #include <error.h> |
| 10 | #include <urcu/tls-compat.h> |
| 11 | #include <lttng/ust-cancelstate.h> |
| 12 | |
| 13 | #include "common/logging.h" |
| 14 | |
| 15 | struct ust_cancelstate { |
| 16 | int nesting; |
| 17 | int oldstate; /* oldstate for outermost nesting */ |
| 18 | }; |
| 19 | |
| 20 | static DEFINE_URCU_TLS(struct ust_cancelstate, thread_state); |
| 21 | |
| 22 | int lttng_ust_cancelstate_disable_push(void) |
| 23 | { |
| 24 | struct ust_cancelstate *state = &URCU_TLS(thread_state); |
| 25 | int ret, oldstate; |
| 26 | |
| 27 | if (state->nesting++) |
| 28 | goto end; |
| 29 | ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); |
| 30 | if (ret) { |
| 31 | ERR("pthread_setcancelstate: %s", strerror(ret)); |
| 32 | return -1; |
| 33 | } |
| 34 | state->oldstate = oldstate; |
| 35 | end: |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | int lttng_ust_cancelstate_disable_pop(void) |
| 40 | { |
| 41 | struct ust_cancelstate *state = &URCU_TLS(thread_state); |
| 42 | int ret, oldstate; |
| 43 | |
| 44 | if (!state->nesting) |
| 45 | return -1; |
| 46 | if (--state->nesting) |
| 47 | goto end; |
| 48 | ret = pthread_setcancelstate(state->oldstate, &oldstate); |
| 49 | if (ret) { |
| 50 | ERR("pthread_setcancelstate: %s", strerror(ret)); |
| 51 | return -1; |
| 52 | } |
| 53 | if (oldstate != PTHREAD_CANCEL_DISABLE) { |
| 54 | ERR("pthread_setcancelstate: unexpected oldstate"); |
| 55 | return -1; |
| 56 | } |
| 57 | end: |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | |