1 /*- 2 * Copyright (c) 1982, 1986, 1990, 1993 3 * The Regents of the University of California. 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, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95 30 * $FreeBSD: src/sys/sys/socketvar.h,v 1.46.2.10 2003/08/24 08:24:39 hsu Exp $ 31 */ 32 33 #ifndef _SYS_SOCKETVAR2_H_ 34 #define _SYS_SOCKETVAR2_H_ 35 36 #ifndef _SYS_SOCKETVAR_H_ 37 #include <sys/socketvar.h> 38 #endif 39 #ifndef _SYS_SYSTM_H_ 40 #include <sys/systm.h> 41 #endif 42 #ifndef _SYS_MALLOC_H_ 43 #include <sys/malloc.h> 44 #endif 45 #include <machine/atomic.h> 46 47 /* 48 * Acquire a lock on a signalsockbuf, sleep if the lock is already held. 49 * The sleep is interruptable unless SSB_NOINTR is set in the ssb. 50 * 51 * We also acquire the token on success. This token is used to interlock 52 * frontend/backend operations until the sockbuf itself can be made mpsafe. 53 * 54 * Returns 0 on success, non-zero if the lock could not be acquired. 55 */ 56 static __inline int 57 ssb_lock(struct signalsockbuf *ssb, int wf) 58 { 59 uint32_t flags; 60 61 for (;;) { 62 flags = ssb->ssb_flags; 63 cpu_ccfence(); 64 if (flags & SSB_LOCK) { 65 if (wf == M_WAITOK) 66 return _ssb_lock(ssb); 67 return EWOULDBLOCK; 68 } 69 if (atomic_cmpset_int(&ssb->ssb_flags, flags, flags|SSB_LOCK)) { 70 lwkt_gettoken(&ssb->ssb_token); 71 return(0); 72 } 73 } 74 } 75 76 /* 77 * Release a previously acquired lock on a signalsockbuf. 78 * 79 * Interlocked wakeup if SSB_WANT was also set. 80 */ 81 static __inline void 82 ssb_unlock(struct signalsockbuf *ssb) 83 { 84 uint32_t flags; 85 86 KKASSERT(ssb->ssb_flags & SSB_LOCK); 87 lwkt_reltoken(&ssb->ssb_token); 88 for (;;) { 89 flags = ssb->ssb_flags; 90 cpu_ccfence(); 91 if (atomic_cmpset_int(&ssb->ssb_flags, flags, 92 flags & ~(SSB_LOCK | SSB_WANT))) { 93 if (flags & SSB_WANT) 94 wakeup(&ssb->ssb_flags); 95 break; 96 } 97 } 98 } 99 100 static __inline void 101 sosetstate(struct socket *so, short state) 102 { 103 atomic_set_short(&so->so_state, state); 104 } 105 106 static __inline void 107 soclrstate(struct socket *so, short state) 108 { 109 atomic_clear_short(&so->so_state, state); 110 } 111 112 static __inline void 113 soreference(struct socket *so) 114 { 115 /* 116 * 0 -> 1 transition will happen on an aborted 117 * socket, which is left on the so_comp queue, 118 * e.g. accept(2) the aborted socket, or when 119 * the listen(2) socket owning the so_comp queue 120 * is closed. 121 */ 122 atomic_add_int(&so->so_refs, 1); 123 } 124 125 #endif 126