| 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 | #include <usterr-signal-safe.h> |
| 13 | |
| 14 | struct ust_cancelstate { |
| 15 | int nesting; |
| 16 | int oldstate; /* oldstate for outermost nesting */ |
| 17 | }; |
| 18 | |
| 19 | static DEFINE_URCU_TLS(struct ust_cancelstate, thread_state); |
| 20 | |
| 21 | int lttng_ust_cancelstate_disable_push(void) |
| 22 | { |
| 23 | struct ust_cancelstate *state = &URCU_TLS(thread_state); |
| 24 | int ret, oldstate; |
| 25 | |
| 26 | if (state->nesting++) |
| 27 | goto end; |
| 28 | ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate); |
| 29 | if (ret) { |
| 30 | ERR("pthread_setcancelstate: %s", strerror(ret)); |
| 31 | return -1; |
| 32 | } |
| 33 | state->oldstate = oldstate; |
| 34 | end: |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | int lttng_ust_cancelstate_disable_pop(void) |
| 39 | { |
| 40 | struct ust_cancelstate *state = &URCU_TLS(thread_state); |
| 41 | int ret, oldstate; |
| 42 | |
| 43 | if (!state->nesting) |
| 44 | return -1; |
| 45 | if (--state->nesting) |
| 46 | goto end; |
| 47 | ret = pthread_setcancelstate(state->oldstate, &oldstate); |
| 48 | if (ret) { |
| 49 | ERR("pthread_setcancelstate: %s", strerror(ret)); |
| 50 | return -1; |
| 51 | } |
| 52 | if (oldstate != PTHREAD_CANCEL_DISABLE) { |
| 53 | ERR("pthread_setcancelstate: unexpected oldstate"); |
| 54 | return -1; |
| 55 | } |
| 56 | end: |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | |