|
| 1 | +/* |
| 2 | + * This program is free software; you can redistribute it and/or |
| 3 | + * modify it under the terms of the GNU General Public |
| 4 | + * License v2 as published by the Free Software Foundation. |
| 5 | + * |
| 6 | + * This program is distributed in the hope that it will be useful, |
| 7 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 9 | + * General Public License for more details. |
| 10 | + * |
| 11 | + * You should have received a copy of the GNU General Public |
| 12 | + * License along with this program; if not, write to the |
| 13 | + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 14 | + * Boston, MA 021110-1307, USA. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <string.h> |
| 18 | +#include <signal.h> |
| 19 | +#include <stdlib.h> |
| 20 | +#include "common/compat.h" |
| 21 | + |
| 22 | +#ifdef __ANDROID__ |
| 23 | + |
| 24 | +/* |
| 25 | + * Workaround for pthread_cancel() in Android, using pthread_kill() instead, as |
| 26 | + * Android NDK does not support pthread_cancel(). |
| 27 | + */ |
| 28 | + |
| 29 | +int pthread_setcanceltype(int type, int *oldtype) { return 0; } |
| 30 | + |
| 31 | +int pthread_setcancelstate(int state, int *oldstate) { return 0; } |
| 32 | + |
| 33 | +int pthread_cancel(pthread_t thread_id) { |
| 34 | + int status; |
| 35 | + |
| 36 | + status = btrfs_set_thread_exit_handler(); |
| 37 | + if (status == 0) |
| 38 | + status = pthread_kill(thread_id, SIGUSR1); |
| 39 | + |
| 40 | + return status; |
| 41 | +} |
| 42 | + |
| 43 | +void btrfs_thread_exit_handler(int sig) { |
| 44 | + pthread_exit(0); |
| 45 | +} |
| 46 | + |
| 47 | +int btrfs_set_thread_exit_handler() { |
| 48 | + struct sigaction actions; |
| 49 | + |
| 50 | + memset(&actions, 0, sizeof(actions)); |
| 51 | + sigemptyset(&actions.sa_mask); |
| 52 | + actions.sa_flags = 0; |
| 53 | + actions.sa_handler = btrfs_thread_exit_handler; |
| 54 | + |
| 55 | + return sigaction(SIGUSR1, &actions, NULL); |
| 56 | +} |
| 57 | + |
| 58 | +struct qsort_r_context { |
| 59 | + int (*compare)(const void *a, const void *b, void *context); |
| 60 | + void *arg; |
| 61 | +}; |
| 62 | + |
| 63 | +#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__) |
| 64 | +static _Thread_local struct qsort_r_context *qsort_r_ctx = NULL; |
| 65 | +#else |
| 66 | +static __thread struct qsort_r_context *qsort_r_ctx = NULL; |
| 67 | +#endif |
| 68 | + |
| 69 | +static int qsort_r_stub_compare(const void *a, const void *b) |
| 70 | +{ |
| 71 | + return qsort_r_ctx->compare(a, b, qsort_r_ctx->arg); |
| 72 | +} |
| 73 | + |
| 74 | +void qsort_r(void *base, size_t nel, size_t width, |
| 75 | + int (*compare)(const void *a, const void *b, void *context), |
| 76 | + void *arg) |
| 77 | +{ |
| 78 | + struct qsort_r_context ctx; |
| 79 | + struct qsort_r_context *old_ctx; |
| 80 | + |
| 81 | + if (nel == 0) |
| 82 | + return; |
| 83 | + |
| 84 | + ctx.compare = compare; |
| 85 | + ctx.arg = arg; |
| 86 | + old_ctx = qsort_r_ctx; |
| 87 | + qsort_r_ctx = &ctx; |
| 88 | + qsort(base, nel, width, qsort_r_stub_compare); |
| 89 | + |
| 90 | + /* Restore the old context after qsort is finished. */ |
| 91 | + qsort_r_ctx = old_ctx; |
| 92 | +} |
| 93 | + |
| 94 | +#endif |
0 commit comments