michael@0: /*- michael@0: * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. michael@0: * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved. michael@0: * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions are met: michael@0: * michael@0: * a) Redistributions of source code must retain the above copyright notice, michael@0: * this list of conditions and the following disclaimer. michael@0: * michael@0: * b) Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in michael@0: * the documentation and/or other materials provided with the distribution. michael@0: * michael@0: * c) Neither the name of Cisco Systems, Inc. nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, michael@0: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF michael@0: * THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #ifndef __sctp_process_lock_h__ michael@0: #define __sctp_process_lock_h__ michael@0: michael@0: /* michael@0: * Need to yet define five atomic fuctions or michael@0: * their equivalant. michael@0: * - atomic_add_int(&foo, val) - add atomically the value michael@0: * - atomic_fetchadd_int(&foo, val) - does same as atomic_add_int michael@0: * but value it was is returned. michael@0: * - atomic_subtract_int(&foo, val) - can be made from atomic_add_int() michael@0: * michael@0: * - atomic_cmpset_int(&foo, value, newvalue) - Does a set of newvalue michael@0: * in foo if and only if michael@0: * foo is value. Returns 0 michael@0: * on success. michael@0: */ michael@0: michael@0: #ifdef SCTP_PER_SOCKET_LOCKING michael@0: /* michael@0: * per socket level locking michael@0: */ michael@0: michael@0: #if defined(__Userspace_os_Windows) michael@0: /* Lock for INFO stuff */ michael@0: #define SCTP_INP_INFO_LOCK_INIT() michael@0: #define SCTP_INP_INFO_RLOCK() michael@0: #define SCTP_INP_INFO_RUNLOCK() michael@0: #define SCTP_INP_INFO_WLOCK() michael@0: #define SCTP_INP_INFO_WUNLOCK() michael@0: #define SCTP_INP_INFO_LOCK_DESTROY() michael@0: #define SCTP_IPI_COUNT_INIT() michael@0: #define SCTP_IPI_COUNT_DESTROY() michael@0: #else michael@0: #define SCTP_INP_INFO_LOCK_INIT() michael@0: #define SCTP_INP_INFO_RLOCK() michael@0: #define SCTP_INP_INFO_RUNLOCK() michael@0: #define SCTP_INP_INFO_WLOCK() michael@0: #define SCTP_INP_INFO_WUNLOCK() michael@0: #define SCTP_INP_INFO_LOCK_DESTROY() michael@0: #define SCTP_IPI_COUNT_INIT() michael@0: #define SCTP_IPI_COUNT_DESTROY() michael@0: #endif michael@0: michael@0: #define SCTP_TCB_SEND_LOCK_INIT(_tcb) michael@0: #define SCTP_TCB_SEND_LOCK_DESTROY(_tcb) michael@0: #define SCTP_TCB_SEND_LOCK(_tcb) michael@0: #define SCTP_TCB_SEND_UNLOCK(_tcb) michael@0: michael@0: /* Lock for INP */ michael@0: #define SCTP_INP_LOCK_INIT(_inp) michael@0: #define SCTP_INP_LOCK_DESTROY(_inp) michael@0: michael@0: #define SCTP_INP_RLOCK(_inp) michael@0: #define SCTP_INP_RUNLOCK(_inp) michael@0: #define SCTP_INP_WLOCK(_inp) michael@0: #define SCTP_INP_WUNLOCK(_inep) michael@0: #define SCTP_INP_INCR_REF(_inp) michael@0: #define SCTP_INP_DECR_REF(_inp) michael@0: michael@0: #define SCTP_ASOC_CREATE_LOCK_INIT(_inp) michael@0: #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp) michael@0: #define SCTP_ASOC_CREATE_LOCK(_inp) michael@0: #define SCTP_ASOC_CREATE_UNLOCK(_inp) michael@0: michael@0: #define SCTP_INP_READ_INIT(_inp) michael@0: #define SCTP_INP_READ_DESTROY(_inp) michael@0: #define SCTP_INP_READ_LOCK(_inp) michael@0: #define SCTP_INP_READ_UNLOCK(_inp) michael@0: michael@0: /* Lock for TCB */ michael@0: #define SCTP_TCB_LOCK_INIT(_tcb) michael@0: #define SCTP_TCB_LOCK_DESTROY(_tcb) michael@0: #define SCTP_TCB_LOCK(_tcb) michael@0: #define SCTP_TCB_TRYLOCK(_tcb) 1 michael@0: #define SCTP_TCB_UNLOCK(_tcb) michael@0: #define SCTP_TCB_UNLOCK_IFOWNED(_tcb) michael@0: #define SCTP_TCB_LOCK_ASSERT(_tcb) michael@0: michael@0: #else michael@0: /* michael@0: * per tcb level locking michael@0: */ michael@0: #define SCTP_IPI_COUNT_INIT() michael@0: michael@0: #if defined(__Userspace_os_Windows) michael@0: #define SCTP_WQ_ADDR_INIT() \ michael@0: InitializeCriticalSection(&SCTP_BASE_INFO(wq_addr_mtx)) michael@0: #define SCTP_WQ_ADDR_DESTROY() \ michael@0: DeleteCriticalSection(&SCTP_BASE_INFO(wq_addr_mtx)) michael@0: #define SCTP_WQ_ADDR_LOCK() \ michael@0: EnterCriticalSection(&SCTP_BASE_INFO(wq_addr_mtx)) michael@0: #define SCTP_WQ_ADDR_UNLOCK() \ michael@0: LeaveCriticalSection(&SCTP_BASE_INFO(wq_addr_mtx)) michael@0: michael@0: michael@0: #define SCTP_INP_INFO_LOCK_INIT() \ michael@0: InitializeCriticalSection(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: #define SCTP_INP_INFO_LOCK_DESTROY() \ michael@0: DeleteCriticalSection(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: #define SCTP_INP_INFO_RLOCK() \ michael@0: EnterCriticalSection(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: #define SCTP_INP_INFO_TRYLOCK() \ michael@0: TryEnterCriticalSection(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: #define SCTP_INP_INFO_WLOCK() \ michael@0: EnterCriticalSection(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: #define SCTP_INP_INFO_RUNLOCK() \ michael@0: LeaveCriticalSection(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: #define SCTP_INP_INFO_WUNLOCK() \ michael@0: LeaveCriticalSection(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: michael@0: #define SCTP_IP_PKTLOG_INIT() \ michael@0: InitializeCriticalSection(&SCTP_BASE_INFO(ipi_pktlog_mtx)) michael@0: #define SCTP_IP_PKTLOG_DESTROY () \ michael@0: DeleteCriticalSection(&SCTP_BASE_INFO(ipi_pktlog_mtx)) michael@0: #define SCTP_IP_PKTLOG_LOCK() \ michael@0: EnterCriticalSection(&SCTP_BASE_INFO(ipi_pktlog_mtx)) michael@0: #define SCTP_IP_PKTLOG_UNLOCK() \ michael@0: LeaveCriticalSection(&SCTP_BASE_INFO(ipi_pktlog_mtx)) michael@0: michael@0: /* michael@0: * The INP locks we will use for locking an SCTP endpoint, so for example if michael@0: * we want to change something at the endpoint level for example random_store michael@0: * or cookie secrets we lock the INP level. michael@0: */ michael@0: #define SCTP_INP_READ_INIT(_inp) \ michael@0: InitializeCriticalSection(&(_inp)->inp_rdata_mtx) michael@0: #define SCTP_INP_READ_DESTROY(_inp) \ michael@0: DeleteCriticalSection(&(_inp)->inp_rdata_mtx) michael@0: #define SCTP_INP_READ_LOCK(_inp) \ michael@0: EnterCriticalSection(&(_inp)->inp_rdata_mtx) michael@0: #define SCTP_INP_READ_UNLOCK(_inp) \ michael@0: LeaveCriticalSection(&(_inp)->inp_rdata_mtx) michael@0: michael@0: #define SCTP_INP_LOCK_INIT(_inp) \ michael@0: InitializeCriticalSection(&(_inp)->inp_mtx) michael@0: michael@0: #define SCTP_ASOC_CREATE_LOCK_INIT(_inp) \ michael@0: InitializeCriticalSection(&(_inp)->inp_create_mtx) michael@0: michael@0: #define SCTP_INP_LOCK_DESTROY(_inp) \ michael@0: DeleteCriticalSection(&(_inp)->inp_mtx) michael@0: michael@0: #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp) \ michael@0: DeleteCriticalSection(&(_inp)->inp_create_mtx) michael@0: michael@0: #ifdef SCTP_LOCK_LOGGING michael@0: #define SCTP_INP_RLOCK(_inp) do { \ michael@0: if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_INP);\ michael@0: EnterCriticalSection(&(_inp)->inp_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_INP_WLOCK(_inp) do { \ michael@0: sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_INP);\ michael@0: EnterCriticalSection(&(_inp)->inp_mtx); \ michael@0: } while (0) michael@0: #else michael@0: michael@0: #define SCTP_INP_RLOCK(_inp) do { \ michael@0: EnterCriticalSection(&(_inp)->inp_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_INP_WLOCK(_inp) do { \ michael@0: EnterCriticalSection(&(_inp)->inp_mtx); \ michael@0: } while (0) michael@0: #endif michael@0: michael@0: michael@0: #define SCTP_TCB_SEND_LOCK_INIT(_tcb) \ michael@0: InitializeCriticalSection(&(_tcb)->tcb_send_mtx) michael@0: michael@0: #define SCTP_TCB_SEND_LOCK_DESTROY(_tcb) \ michael@0: DeleteCriticalSection(&(_tcb)->tcb_send_mtx) michael@0: michael@0: #define SCTP_TCB_SEND_LOCK(_tcb) do { \ michael@0: EnterCriticalSection(&(_tcb)->tcb_send_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_TCB_SEND_UNLOCK(_tcb) \ michael@0: LeaveCriticalSection(&(_tcb)->tcb_send_mtx) michael@0: michael@0: #define SCTP_INP_INCR_REF(_inp) atomic_add_int(&((_inp)->refcount), 1) michael@0: #define SCTP_INP_DECR_REF(_inp) atomic_add_int(&((_inp)->refcount), -1) michael@0: michael@0: #ifdef SCTP_LOCK_LOGGING michael@0: #define SCTP_ASOC_CREATE_LOCK(_inp) do { \ michael@0: if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_CREATE); \ michael@0: EnterCriticalSection(&(_inp)->inp_create_mtx); \ michael@0: } while (0) michael@0: #else michael@0: #define SCTP_ASOC_CREATE_LOCK(_inp) do { \ michael@0: EnterCriticalSection(&(_inp)->inp_create_mtx); \ michael@0: } while (0) michael@0: #endif michael@0: michael@0: #define SCTP_INP_RUNLOCK(_inp) \ michael@0: LeaveCriticalSection(&(_inp)->inp_mtx) michael@0: #define SCTP_INP_WUNLOCK(_inp) \ michael@0: LeaveCriticalSection(&(_inp)->inp_mtx) michael@0: #define SCTP_ASOC_CREATE_UNLOCK(_inp) \ michael@0: LeaveCriticalSection(&(_inp)->inp_create_mtx) michael@0: michael@0: /* michael@0: * For the majority of things (once we have found the association) we will michael@0: * lock the actual association mutex. This will protect all the assoiciation michael@0: * level queues and streams and such. We will need to lock the socket layer michael@0: * when we stuff data up into the receiving sb_mb. I.e. we will need to do an michael@0: * extra SOCKBUF_LOCK(&so->so_rcv) even though the association is locked. michael@0: */ michael@0: michael@0: #define SCTP_TCB_LOCK_INIT(_tcb) \ michael@0: InitializeCriticalSection(&(_tcb)->tcb_mtx) michael@0: michael@0: #define SCTP_TCB_LOCK_DESTROY(_tcb) \ michael@0: DeleteCriticalSection(&(_tcb)->tcb_mtx) michael@0: michael@0: #ifdef SCTP_LOCK_LOGGING michael@0: #define SCTP_TCB_LOCK(_tcb) do { \ michael@0: if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_tcb->sctp_ep, _tcb, SCTP_LOG_LOCK_TCB); \ michael@0: EnterCriticalSection(&(_tcb)->tcb_mtx); \ michael@0: } while (0) michael@0: michael@0: #else michael@0: #define SCTP_TCB_LOCK(_tcb) do { \ michael@0: EnterCriticalSection(&(_tcb)->tcb_mtx); \ michael@0: } while (0) michael@0: #endif michael@0: michael@0: #define SCTP_TCB_TRYLOCK(_tcb) ((TryEnterCriticalSection(&(_tcb)->tcb_mtx))) michael@0: michael@0: #define SCTP_TCB_UNLOCK(_tcb) do { \ michael@0: LeaveCriticalSection(&(_tcb)->tcb_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_TCB_LOCK_ASSERT(_tcb) michael@0: michael@0: #else /* all Userspaces except Windows */ michael@0: #define SCTP_WQ_ADDR_INIT() \ michael@0: (void)pthread_mutex_init(&SCTP_BASE_INFO(wq_addr_mtx), NULL) michael@0: #define SCTP_WQ_ADDR_DESTROY() \ michael@0: (void)pthread_mutex_destroy(&SCTP_BASE_INFO(wq_addr_mtx)) michael@0: #define SCTP_WQ_ADDR_LOCK() \ michael@0: (void)pthread_mutex_lock(&SCTP_BASE_INFO(wq_addr_mtx)) michael@0: #define SCTP_WQ_ADDR_UNLOCK() \ michael@0: (void)pthread_mutex_unlock(&SCTP_BASE_INFO(wq_addr_mtx)) michael@0: michael@0: michael@0: #define SCTP_INP_INFO_LOCK_INIT() \ michael@0: (void)pthread_mutex_init(&SCTP_BASE_INFO(ipi_ep_mtx), NULL) michael@0: #define SCTP_INP_INFO_LOCK_DESTROY() \ michael@0: (void)pthread_mutex_destroy(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: #define SCTP_INP_INFO_RLOCK() \ michael@0: (void)pthread_mutex_lock(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: #define SCTP_INP_INFO_TRYLOCK() \ michael@0: (!(pthread_mutex_trylock(&SCTP_BASE_INFO(ipi_ep_mtx)))) michael@0: #define SCTP_INP_INFO_WLOCK() \ michael@0: (void)pthread_mutex_lock(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: #define SCTP_INP_INFO_RUNLOCK() \ michael@0: (void)pthread_mutex_unlock(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: #define SCTP_INP_INFO_WUNLOCK() \ michael@0: (void)pthread_mutex_unlock(&SCTP_BASE_INFO(ipi_ep_mtx)) michael@0: michael@0: #define SCTP_IP_PKTLOG_INIT() \ michael@0: (void)pthread_mutex_init(&SCTP_BASE_INFO(ipi_pktlog_mtx), NULL) michael@0: #define SCTP_IP_PKTLOG_DESTROY() \ michael@0: (void)pthread_mutex_destroy(&SCTP_BASE_INFO(ipi_pktlog_mtx)) michael@0: #define SCTP_IP_PKTLOG_LOCK() \ michael@0: (void)pthread_mutex_lock(&SCTP_BASE_INFO(ipi_pktlog_mtx)) michael@0: #define SCTP_IP_PKTLOG_UNLOCK() \ michael@0: (void)pthread_mutex_unlock(&SCTP_BASE_INFO(ipi_pktlog_mtx)) michael@0: michael@0: michael@0: michael@0: /* michael@0: * The INP locks we will use for locking an SCTP endpoint, so for example if michael@0: * we want to change something at the endpoint level for example random_store michael@0: * or cookie secrets we lock the INP level. michael@0: */ michael@0: #define SCTP_INP_READ_INIT(_inp) \ michael@0: (void)pthread_mutex_init(&(_inp)->inp_rdata_mtx, NULL) michael@0: michael@0: #define SCTP_INP_READ_DESTROY(_inp) \ michael@0: (void)pthread_mutex_destroy(&(_inp)->inp_rdata_mtx) michael@0: michael@0: #define SCTP_INP_READ_LOCK(_inp) do { \ michael@0: (void)pthread_mutex_lock(&(_inp)->inp_rdata_mtx); \ michael@0: } while (0) michael@0: michael@0: michael@0: #define SCTP_INP_READ_UNLOCK(_inp) \ michael@0: (void)pthread_mutex_unlock(&(_inp)->inp_rdata_mtx) michael@0: michael@0: #define SCTP_INP_LOCK_INIT(_inp) \ michael@0: (void)pthread_mutex_init(&(_inp)->inp_mtx, NULL) michael@0: michael@0: #define SCTP_ASOC_CREATE_LOCK_INIT(_inp) \ michael@0: (void)pthread_mutex_init(&(_inp)->inp_create_mtx, NULL) michael@0: michael@0: #define SCTP_INP_LOCK_DESTROY(_inp) \ michael@0: (void)pthread_mutex_destroy(&(_inp)->inp_mtx) michael@0: michael@0: #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp) \ michael@0: (void)pthread_mutex_destroy(&(_inp)->inp_create_mtx) michael@0: michael@0: #ifdef SCTP_LOCK_LOGGING michael@0: #define SCTP_INP_RLOCK(_inp) do { \ michael@0: if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_INP);\ michael@0: (void)pthread_mutex_lock(&(_inp)->inp_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_INP_WLOCK(_inp) do { \ michael@0: sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_INP);\ michael@0: (void)pthread_mutex_lock(&(_inp)->inp_mtx); \ michael@0: } while (0) michael@0: michael@0: #else michael@0: michael@0: #define SCTP_INP_RLOCK(_inp) do { \ michael@0: (void)pthread_mutex_lock(&(_inp)->inp_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_INP_WLOCK(_inp) do { \ michael@0: (void)pthread_mutex_lock(&(_inp)->inp_mtx); \ michael@0: } while (0) michael@0: #endif michael@0: michael@0: michael@0: #define SCTP_TCB_SEND_LOCK_INIT(_tcb) \ michael@0: (void)pthread_mutex_init(&(_tcb)->tcb_send_mtx, NULL) michael@0: michael@0: #define SCTP_TCB_SEND_LOCK_DESTROY(_tcb) \ michael@0: (void)pthread_mutex_destroy(&(_tcb)->tcb_send_mtx) michael@0: michael@0: #define SCTP_TCB_SEND_LOCK(_tcb) do { \ michael@0: (void)pthread_mutex_lock(&(_tcb)->tcb_send_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_TCB_SEND_UNLOCK(_tcb) \ michael@0: (void)pthread_mutex_unlock(&(_tcb)->tcb_send_mtx) michael@0: michael@0: #define SCTP_INP_INCR_REF(_inp) atomic_add_int(&((_inp)->refcount), 1) michael@0: #define SCTP_INP_DECR_REF(_inp) atomic_add_int(&((_inp)->refcount), -1) michael@0: michael@0: #ifdef SCTP_LOCK_LOGGING michael@0: #define SCTP_ASOC_CREATE_LOCK(_inp) do { \ michael@0: if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_inp, (struct sctp_tcb *)NULL, SCTP_LOG_LOCK_CREATE); \ michael@0: (void)pthread_mutex_lock(&(_inp)->inp_create_mtx); \ michael@0: } while (0) michael@0: #else michael@0: #define SCTP_ASOC_CREATE_LOCK(_inp) do { \ michael@0: (void)pthread_mutex_lock(&(_inp)->inp_create_mtx); \ michael@0: } while (0) michael@0: #endif michael@0: michael@0: #define SCTP_INP_RUNLOCK(_inp) \ michael@0: (void)pthread_mutex_unlock(&(_inp)->inp_mtx) michael@0: #define SCTP_INP_WUNLOCK(_inp) \ michael@0: (void)pthread_mutex_unlock(&(_inp)->inp_mtx) michael@0: #define SCTP_ASOC_CREATE_UNLOCK(_inp) \ michael@0: (void)pthread_mutex_unlock(&(_inp)->inp_create_mtx) michael@0: michael@0: /* michael@0: * For the majority of things (once we have found the association) we will michael@0: * lock the actual association mutex. This will protect all the assoiciation michael@0: * level queues and streams and such. We will need to lock the socket layer michael@0: * when we stuff data up into the receiving sb_mb. I.e. we will need to do an michael@0: * extra SOCKBUF_LOCK(&so->so_rcv) even though the association is locked. michael@0: */ michael@0: michael@0: #define SCTP_TCB_LOCK_INIT(_tcb) \ michael@0: (void)pthread_mutex_init(&(_tcb)->tcb_mtx, NULL) michael@0: michael@0: #define SCTP_TCB_LOCK_DESTROY(_tcb) \ michael@0: (void)pthread_mutex_destroy(&(_tcb)->tcb_mtx) michael@0: michael@0: #ifdef SCTP_LOCK_LOGGING michael@0: #define SCTP_TCB_LOCK(_tcb) do { \ michael@0: if(SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOCK_LOGGING_ENABLE) sctp_log_lock(_tcb->sctp_ep, _tcb, SCTP_LOG_LOCK_TCB); \ michael@0: (void)pthread_mutex_lock(&(_tcb)->tcb_mtx); \ michael@0: } while (0) michael@0: michael@0: #else michael@0: #define SCTP_TCB_LOCK(_tcb) do { \ michael@0: (void)pthread_mutex_lock(&(_tcb)->tcb_mtx); \ michael@0: } while (0) michael@0: #endif michael@0: michael@0: #define SCTP_TCB_TRYLOCK(_tcb) (!(pthread_mutex_trylock(&(_tcb)->tcb_mtx))) michael@0: michael@0: #define SCTP_TCB_UNLOCK(_tcb) (void)pthread_mutex_unlock(&(_tcb)->tcb_mtx) michael@0: michael@0: #define SCTP_TCB_LOCK_ASSERT(_tcb) michael@0: #endif michael@0: michael@0: #endif /* SCTP_PER_SOCKET_LOCKING */ michael@0: michael@0: michael@0: /* michael@0: * common locks michael@0: */ michael@0: michael@0: /* copied over to compile */ michael@0: #define SCTP_INP_LOCK_CONTENDED(_inp) (0) /* Don't know if this is possible */ michael@0: #define SCTP_INP_READ_CONTENDED(_inp) (0) /* Don't know if this is possible */ michael@0: #define SCTP_ASOC_CREATE_LOCK_CONTENDED(_inp) (0) /* Don't know if this is possible */ michael@0: michael@0: michael@0: /* socket locks */ michael@0: michael@0: #if defined(__Userspace__) michael@0: #if defined(__Userspace_os_Windows) michael@0: #define SOCKBUF_LOCK_ASSERT(_so_buf) michael@0: #define SOCKBUF_LOCK(_so_buf) EnterCriticalSection(&(_so_buf)->sb_mtx) michael@0: #define SOCKBUF_UNLOCK(_so_buf) LeaveCriticalSection(&(_so_buf)->sb_mtx) michael@0: #define SOCK_LOCK(_so) SOCKBUF_LOCK(&(_so)->so_rcv) michael@0: #define SOCK_UNLOCK(_so) SOCKBUF_UNLOCK(&(_so)->so_rcv) michael@0: #else michael@0: #define SOCKBUF_LOCK_ASSERT(_so_buf) KASSERT(pthread_mutex_trylock(SOCKBUF_MTX(_so_buf)) == EBUSY, ("%s: socket buffer not locked", __func__)) michael@0: #define SOCKBUF_LOCK(_so_buf) pthread_mutex_lock(SOCKBUF_MTX(_so_buf)) michael@0: #define SOCKBUF_UNLOCK(_so_buf) pthread_mutex_unlock(SOCKBUF_MTX(_so_buf)) michael@0: #define SOCK_LOCK(_so) SOCKBUF_LOCK(&(_so)->so_rcv) michael@0: #define SOCK_UNLOCK(_so) SOCKBUF_UNLOCK(&(_so)->so_rcv) michael@0: #endif michael@0: #else michael@0: #define SOCK_LOCK(_so) michael@0: #define SOCK_UNLOCK(_so) michael@0: #define SOCKBUF_LOCK(_so_buf) michael@0: #define SOCKBUF_UNLOCK(_so_buf) michael@0: #define SOCKBUF_LOCK_ASSERT(_so_buf) michael@0: #endif michael@0: michael@0: #define SCTP_STATLOG_INIT_LOCK() michael@0: #define SCTP_STATLOG_LOCK() michael@0: #define SCTP_STATLOG_UNLOCK() michael@0: #define SCTP_STATLOG_DESTROY() michael@0: michael@0: #if defined(__Userspace_os_Windows) michael@0: /* address list locks */ michael@0: #define SCTP_IPI_ADDR_INIT() \ michael@0: InitializeCriticalSection(&SCTP_BASE_INFO(ipi_addr_mtx)) michael@0: #define SCTP_IPI_ADDR_DESTROY() \ michael@0: DeleteCriticalSection(&SCTP_BASE_INFO(ipi_addr_mtx)) michael@0: michael@0: #define SCTP_IPI_ADDR_RLOCK() \ michael@0: do { \ michael@0: EnterCriticalSection(&SCTP_BASE_INFO(ipi_addr_mtx)); \ michael@0: } while (0) michael@0: #define SCTP_IPI_ADDR_RUNLOCK() \ michael@0: LeaveCriticalSection(&SCTP_BASE_INFO(ipi_addr_mtx)) michael@0: michael@0: #define SCTP_IPI_ADDR_WLOCK() \ michael@0: do { \ michael@0: EnterCriticalSection(&SCTP_BASE_INFO(ipi_addr_mtx)); \ michael@0: } while (0) michael@0: #define SCTP_IPI_ADDR_WUNLOCK() \ michael@0: LeaveCriticalSection(&SCTP_BASE_INFO(ipi_addr_mtx)) michael@0: michael@0: michael@0: /* iterator locks */ michael@0: #define SCTP_ITERATOR_LOCK_INIT() \ michael@0: InitializeCriticalSection(&sctp_it_ctl.it_mtx) michael@0: michael@0: #define SCTP_ITERATOR_LOCK() \ michael@0: do { \ michael@0: EnterCriticalSection(&sctp_it_ctl.it_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_ITERATOR_UNLOCK() \ michael@0: LeaveCriticalSection(&sctp_it_ctl.it_mtx) michael@0: michael@0: #define SCTP_ITERATOR_LOCK_DESTROY() \ michael@0: DeleteCriticalSection(&sctp_it_ctl.it_mtx) michael@0: michael@0: michael@0: #define SCTP_IPI_ITERATOR_WQ_INIT() \ michael@0: InitializeCriticalSection(&sctp_it_ctl.ipi_iterator_wq_mtx) michael@0: michael@0: #define SCTP_IPI_ITERATOR_WQ_DESTROY() \ michael@0: DeleteCriticalSection(&sctp_it_ctl.ipi_iterator_wq_mtx) michael@0: michael@0: #define SCTP_IPI_ITERATOR_WQ_LOCK() \ michael@0: do { \ michael@0: EnterCriticalSection(&sctp_it_ctl.ipi_iterator_wq_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_IPI_ITERATOR_WQ_UNLOCK() \ michael@0: LeaveCriticalSection(&sctp_it_ctl.ipi_iterator_wq_mtx) michael@0: michael@0: #else /* end of __Userspace_os_Windows */ michael@0: /* address list locks */ michael@0: #define SCTP_IPI_ADDR_INIT() \ michael@0: (void)pthread_mutex_init(&SCTP_BASE_INFO(ipi_addr_mtx), NULL) michael@0: #define SCTP_IPI_ADDR_DESTROY() \ michael@0: (void)pthread_mutex_destroy(&SCTP_BASE_INFO(ipi_addr_mtx)) michael@0: michael@0: #define SCTP_IPI_ADDR_RLOCK() \ michael@0: do { \ michael@0: (void)pthread_mutex_lock(&SCTP_BASE_INFO(ipi_addr_mtx)); \ michael@0: } while (0) michael@0: #define SCTP_IPI_ADDR_RUNLOCK() \ michael@0: (void)pthread_mutex_unlock(&SCTP_BASE_INFO(ipi_addr_mtx)) michael@0: michael@0: #define SCTP_IPI_ADDR_WLOCK() \ michael@0: do { \ michael@0: (void)pthread_mutex_lock(&SCTP_BASE_INFO(ipi_addr_mtx)); \ michael@0: } while (0) michael@0: #define SCTP_IPI_ADDR_WUNLOCK() \ michael@0: (void)pthread_mutex_unlock(&SCTP_BASE_INFO(ipi_addr_mtx)) michael@0: michael@0: michael@0: /* iterator locks */ michael@0: #define SCTP_ITERATOR_LOCK_INIT() \ michael@0: (void)pthread_mutex_init(&sctp_it_ctl.it_mtx, NULL) michael@0: michael@0: #define SCTP_ITERATOR_LOCK() \ michael@0: do { \ michael@0: (void)pthread_mutex_lock(&sctp_it_ctl.it_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_ITERATOR_UNLOCK() \ michael@0: (void)pthread_mutex_unlock(&sctp_it_ctl.it_mtx) michael@0: michael@0: #define SCTP_ITERATOR_LOCK_DESTROY() \ michael@0: (void)pthread_mutex_destroy(&sctp_it_ctl.it_mtx) michael@0: michael@0: michael@0: #define SCTP_IPI_ITERATOR_WQ_INIT() \ michael@0: (void)pthread_mutex_init(&sctp_it_ctl.ipi_iterator_wq_mtx, NULL) michael@0: michael@0: #define SCTP_IPI_ITERATOR_WQ_DESTROY() \ michael@0: (void)pthread_mutex_destroy(&sctp_it_ctl.ipi_iterator_wq_mtx) michael@0: michael@0: #define SCTP_IPI_ITERATOR_WQ_LOCK() \ michael@0: do { \ michael@0: (void)pthread_mutex_lock(&sctp_it_ctl.ipi_iterator_wq_mtx); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_IPI_ITERATOR_WQ_UNLOCK() \ michael@0: (void)pthread_mutex_unlock(&sctp_it_ctl.ipi_iterator_wq_mtx) michael@0: #endif michael@0: michael@0: #define SCTP_INCR_EP_COUNT() \ michael@0: do { \ michael@0: atomic_add_int(&SCTP_BASE_INFO(ipi_count_ep), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_DECR_EP_COUNT() \ michael@0: do { \ michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ep), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_INCR_ASOC_COUNT() \ michael@0: do { \ michael@0: atomic_add_int(&SCTP_BASE_INFO(ipi_count_asoc), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_DECR_ASOC_COUNT() \ michael@0: do { \ michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_asoc), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_INCR_LADDR_COUNT() \ michael@0: do { \ michael@0: atomic_add_int(&SCTP_BASE_INFO(ipi_count_laddr), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_DECR_LADDR_COUNT() \ michael@0: do { \ michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_laddr), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_INCR_RADDR_COUNT() \ michael@0: do { \ michael@0: atomic_add_int(&SCTP_BASE_INFO(ipi_count_raddr), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_DECR_RADDR_COUNT() \ michael@0: do { \ michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_raddr), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_INCR_CHK_COUNT() \ michael@0: do { \ michael@0: atomic_add_int(&SCTP_BASE_INFO(ipi_count_chunk), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_DECR_CHK_COUNT() \ michael@0: do { \ michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_chunk), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_INCR_READQ_COUNT() \ michael@0: do { \ michael@0: atomic_add_int(&SCTP_BASE_INFO(ipi_count_readq), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_DECR_READQ_COUNT() \ michael@0: do { \ michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_readq), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_INCR_STRMOQ_COUNT() \ michael@0: do { \ michael@0: atomic_add_int(&SCTP_BASE_INFO(ipi_count_strmoq), 1); \ michael@0: } while (0) michael@0: michael@0: #define SCTP_DECR_STRMOQ_COUNT() \ michael@0: do { \ michael@0: atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_strmoq), 1); \ michael@0: } while (0) michael@0: michael@0: #endif