xref: /dragonfly/sys/sys/socketvar2.h (revision d50f9ae3)
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 _KERNEL
37 #error "This file should not be included by userland programs."
38 #endif
39 
40 #ifndef _SYS_SOCKETVAR_H_
41 #include <sys/socketvar.h>
42 #endif
43 #ifndef _SYS_SYSTM_H_
44 #include <sys/systm.h>
45 #endif
46 #include <machine/atomic.h>
47 
48 /*
49  * Acquire a lock on a signalsockbuf, sleep if the lock is already held.
50  * The sleep is interruptable unless SSB_NOINTR is set in the ssb.
51  *
52  * We also acquire the token on success.  This token is used to interlock
53  * frontend/backend operations until the sockbuf itself can be made mpsafe.
54  *
55  * Returns 0 on success, non-zero if the lock could not be acquired.
56  */
57 #ifdef MALLOC_DEFINE
58 static __inline int
59 ssb_lock(struct signalsockbuf *ssb, int wf)
60 {
61 	uint32_t flags;
62 
63 	for (;;) {
64 		flags = ssb->ssb_flags;
65 		cpu_ccfence();
66 		if (flags & SSB_LOCK) {
67 			if (wf == M_WAITOK)
68 				return _ssb_lock(ssb);
69 			return EWOULDBLOCK;
70 		}
71 		if (atomic_cmpset_int(&ssb->ssb_flags, flags, flags|SSB_LOCK)) {
72 			lwkt_gettoken(&ssb->ssb_token);
73 			return(0);
74 		}
75 	}
76 }
77 #endif
78 
79 /*
80  * Release a previously acquired lock on a signalsockbuf.
81  *
82  * Interlocked wakeup if SSB_WANT was also set.
83  */
84 static __inline void
85 ssb_unlock(struct signalsockbuf *ssb)
86 {
87 	uint32_t flags;
88 
89 	KKASSERT(ssb->ssb_flags & SSB_LOCK);
90 	lwkt_reltoken(&ssb->ssb_token);
91 	for (;;) {
92 		flags = ssb->ssb_flags;
93 		cpu_ccfence();
94 		if (atomic_cmpset_int(&ssb->ssb_flags, flags,
95 				      flags & ~(SSB_LOCK | SSB_WANT))) {
96 			if (flags & SSB_WANT)
97 				wakeup(&ssb->ssb_flags);
98 			break;
99 		}
100 	}
101 }
102 
103 static __inline void
104 sosetstate(struct socket *so, short state)
105 {
106 	atomic_set_short(&so->so_state, state);
107 }
108 
109 static __inline void
110 soclrstate(struct socket *so, short state)
111 {
112 	atomic_clear_short(&so->so_state, state);
113 }
114 
115 static __inline void
116 soreference(struct socket *so)
117 {
118 	/*
119 	 * 0 -> 1 transition will happen on an aborted
120 	 * socket, which is left on the so_comp queue,
121 	 * e.g. accept(2) the aborted socket, or when
122 	 * the listen(2) socket owning the so_comp queue
123 	 * is closed.
124 	 */
125 	atomic_add_int(&so->so_refs, 1);
126 }
127 
128 #endif
129