1 /* $NetBSD: cancelpoint.h,v 1.1 2025/04/05 11:22:32 riastradh Exp $ */ 2 3 /* 4 * Copyright (c) 2025 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef TESTS_LIB_LIBPTHREAD_CANCELPOINT_H 30 #define TESTS_LIB_LIBPTHREAD_CANCELPOINT_H 31 32 #include <pthread.h> 33 #include <stdbool.h> 34 #include <stddef.h> 35 36 #include "h_macros.h" 37 38 extern pthread_barrier_t bar; 39 extern bool cleanup_done; 40 41 #if 0 42 atomic_bool cancelpointreadydone; 43 #endif 44 45 static void 46 cleanup(void *cookie) 47 { 48 bool *cleanup_donep = cookie; 49 50 *cleanup_donep = true; 51 } 52 53 static void 54 cancelpointready(void) 55 { 56 57 (void)pthread_barrier_wait(&bar); 58 RL(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)); 59 #if 0 60 atomic_store_release(&cancelpointreadydone, true); 61 #endif 62 } 63 64 static void * 65 thread_cancelpoint(void *cookie) 66 { 67 void (*cancelpoint)(void) = cookie; 68 69 RL(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL)); 70 (void)pthread_barrier_wait(&bar); 71 72 pthread_cleanup_push(&cleanup, &cleanup_done); 73 (*cancelpoint)(); 74 pthread_cleanup_pop(/*execute*/0); 75 76 return NULL; 77 } 78 79 static void 80 test_cancelpoint_before(void (*cancelpoint)(void)) 81 { 82 pthread_t t; 83 void *result; 84 85 RZ(pthread_barrier_init(&bar, NULL, 2)); 86 RZ(pthread_create(&t, NULL, &thread_cancelpoint, cancelpoint)); 87 88 (void)pthread_barrier_wait(&bar); 89 fprintf(stderr, "cancel\n"); 90 RZ(pthread_cancel(t)); 91 (void)pthread_barrier_wait(&bar); 92 93 alarm(1); 94 RZ(pthread_join(t, &result)); 95 ATF_CHECK_MSG(result == PTHREAD_CANCELED, 96 "result=%p PTHREAD_CANCELED=%p", result, PTHREAD_CANCELED); 97 ATF_CHECK(cleanup_done); 98 } 99 100 #if 0 101 static void 102 test_cancelpoint_wakeup(void (*cancelpoint)(void)) 103 { 104 pthread_t t; 105 106 RZ(pthread_barrier_init(&bar, NULL, 2)); 107 RZ(pthread_create(&t, NULL, &cancelpoint_thread, cancelpoint)); 108 109 (void)pthread_barrier_wait(&bar); 110 while (!atomic_load_acquire(&cancelpointreadydone)) 111 continue; 112 while (!pthread_sleeping(t)) /* XXX find a way to do this */ 113 continue; 114 RZ(pthread_cancel(t)); 115 } 116 #endif 117 118 #define TEST_CANCELPOINT(CANCELPOINT, XFAIL) \ 119 ATF_TC(CANCELPOINT); \ 120 ATF_TC_HEAD(CANCELPOINT, tc) \ 121 { \ 122 atf_tc_set_md_var(tc, "descr", "Test cancellation point: " \ 123 #CANCELPOINT); \ 124 } \ 125 ATF_TC_BODY(CANCELPOINT, tc) \ 126 { \ 127 XFAIL; \ 128 test_cancelpoint_before(&CANCELPOINT); \ 129 } 130 #define ADD_TEST_CANCELPOINT(CANCELPOINT) \ 131 ATF_TP_ADD_TC(tp, CANCELPOINT) 132 133 #endif /* TESTS_LIB_LIBPTHREAD_CANCELPOINT_H */ 134