1 /* 2 * Copyright (c) 2005, David Xu<davidxu@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $DragonFly: src/lib/libthread_xu/thread/thr_cancel.c,v 1.1 2005/02/01 12:38:27 davidxu Exp $ 27 */ 28 29 #include <pthread.h> 30 #include "thr_private.h" 31 32 __weak_reference(_pthread_cancel, pthread_cancel); 33 __weak_reference(_pthread_setcancelstate, pthread_setcancelstate); 34 __weak_reference(_pthread_setcanceltype, pthread_setcanceltype); 35 __weak_reference(_pthread_testcancel, pthread_testcancel); 36 37 int _pthread_setcanceltype(int type, int *oldtype); 38 39 int 40 _pthread_cancel(pthread_t pthread) 41 { 42 struct pthread *curthread = _get_curthread(); 43 int oldval, newval = 0; 44 int oldtype; 45 int ret; 46 47 /* 48 * POSIX says _pthread_cancel should be async cancellation safe, 49 * so we temporarily disable async cancellation. 50 */ 51 _pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); 52 if ((ret = _thr_ref_add(curthread, pthread, 0)) != 0) { 53 _pthread_setcanceltype(oldtype, NULL); 54 return (ret); 55 } 56 57 do { 58 oldval = pthread->cancelflags; 59 if (oldval & THR_CANCEL_NEEDED) 60 break; 61 newval = oldval | THR_CANCEL_NEEDED; 62 } while (!atomic_cmpset_acq_int(&pthread->cancelflags, oldval, newval)); 63 64 if (!(oldval & THR_CANCEL_NEEDED) && SHOULD_ASYNC_CANCEL(newval)) 65 _thr_send_sig(pthread, SIGCANCEL); 66 67 _thr_ref_delete(curthread, pthread); 68 _pthread_setcanceltype(oldtype, NULL); 69 return (0); 70 } 71 72 static inline void 73 testcancel(struct pthread *curthread) 74 { 75 int newval; 76 77 newval = curthread->cancelflags; 78 if (SHOULD_CANCEL(newval)) 79 pthread_exit(PTHREAD_CANCELED); 80 } 81 82 int 83 _pthread_setcancelstate(int state, int *oldstate) 84 { 85 struct pthread *curthread = _get_curthread(); 86 int oldval, ret; 87 88 oldval = curthread->cancelflags; 89 if (oldstate != NULL) 90 *oldstate = ((oldval & THR_CANCEL_DISABLE) ? 91 PTHREAD_CANCEL_DISABLE : PTHREAD_CANCEL_ENABLE); 92 switch (state) { 93 case PTHREAD_CANCEL_DISABLE: 94 atomic_set_int(&curthread->cancelflags, THR_CANCEL_DISABLE); 95 ret = 0; 96 break; 97 case PTHREAD_CANCEL_ENABLE: 98 atomic_clear_int(&curthread->cancelflags, THR_CANCEL_DISABLE); 99 testcancel(curthread); 100 ret = 0; 101 break; 102 default: 103 ret = EINVAL; 104 } 105 106 return (ret); 107 } 108 109 int 110 _pthread_setcanceltype(int type, int *oldtype) 111 { 112 struct pthread *curthread = _get_curthread(); 113 int oldval, ret; 114 115 oldval = curthread->cancelflags; 116 if (oldtype != NULL) 117 *oldtype = ((oldval & THR_CANCEL_AT_POINT) ? 118 PTHREAD_CANCEL_ASYNCHRONOUS : 119 PTHREAD_CANCEL_DEFERRED); 120 switch (type) { 121 case PTHREAD_CANCEL_ASYNCHRONOUS: 122 atomic_set_int(&curthread->cancelflags, THR_CANCEL_AT_POINT); 123 testcancel(curthread); 124 ret = 0; 125 break; 126 case PTHREAD_CANCEL_DEFERRED: 127 atomic_clear_int(&curthread->cancelflags, THR_CANCEL_AT_POINT); 128 ret = 0; 129 break; 130 default: 131 ret = EINVAL; 132 } 133 134 return (ret); 135 } 136 137 void 138 _pthread_testcancel(void) 139 { 140 testcancel(_get_curthread()); 141 } 142 143 int 144 _thr_cancel_enter(struct pthread *curthread) 145 { 146 int oldval; 147 148 oldval = curthread->cancelflags; 149 if (!(oldval & THR_CANCEL_AT_POINT)) { 150 atomic_set_int(&curthread->cancelflags, THR_CANCEL_AT_POINT); 151 testcancel(curthread); 152 } 153 return (oldval); 154 } 155 156 void 157 _thr_cancel_leave(struct pthread *curthread, int previous) 158 { 159 if (!(previous & THR_CANCEL_AT_POINT)) 160 atomic_clear_int(&curthread->cancelflags, THR_CANCEL_AT_POINT); 161 } 162