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