mutex.h revision 329976
1293194Shselasky/*- 2328653Shselasky * Copyright (c) 2010 Isilon Systems, Inc. 3293194Shselasky * Copyright (c) 2010 iX Systems, Inc. 4293194Shselasky * Copyright (c) 2010 Panasas, Inc. 5293194Shselasky * Copyright (c) 2013-2017 Mellanox Technologies, Ltd. 6293194Shselasky * All rights reserved. 7293194Shselasky * 8293194Shselasky * Redistribution and use in source and binary forms, with or without 9293194Shselasky * modification, are permitted provided that the following conditions 10293194Shselasky * are met: 11293194Shselasky * 1. Redistributions of source code must retain the above copyright 12293194Shselasky * notice unmodified, this list of conditions, and the following 13293194Shselasky * disclaimer. 14293194Shselasky * 2. Redistributions in binary form must reproduce the above copyright 15293194Shselasky * notice, this list of conditions and the following disclaimer in the 16293194Shselasky * documentation and/or other materials provided with the distribution. 17293194Shselasky * 18293194Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19293194Shselasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20293194Shselasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21293194Shselasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22293194Shselasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23293194Shselasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24293194Shselasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25293194Shselasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26293194Shselasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27293194Shselasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28293194Shselasky * 29293194Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/mutex.h 329976 2018-02-25 10:40:41Z hselasky $ 30293194Shselasky */ 31328653Shselasky#ifndef _LINUX_MUTEX_H_ 32328653Shselasky#define _LINUX_MUTEX_H_ 33293194Shselasky 34328653Shselasky#include <sys/param.h> 35293194Shselasky#include <sys/proc.h> 36328653Shselasky#include <sys/lock.h> 37293194Shselasky#include <sys/sx.h> 38328653Shselasky 39328653Shselasky#include <linux/spinlock.h> 40293194Shselasky 41328653Shselaskytypedef struct mutex { 42328653Shselasky struct sx sx; 43328653Shselasky} mutex_t; 44293194Shselasky 45328653Shselasky/* 46328653Shselasky * By defining CONFIG_NO_MUTEX_SKIP LinuxKPI mutexes and asserts will 47328653Shselasky * not be skipped during panic(). 48293194Shselasky */ 49328653Shselasky#ifdef CONFIG_NO_MUTEX_SKIP 50328653Shselasky#define MUTEX_SKIP(void) 0 51328653Shselasky#else 52293194Shselasky#define MUTEX_SKIP(void) unlikely(SCHEDULER_STOPPED() || kdb_active) 53328653Shselasky#endif 54328653Shselasky 55328653Shselasky#define mutex_lock(_m) do { \ 56293194Shselasky if (MUTEX_SKIP()) \ 57328653Shselasky break; \ 58328653Shselasky sx_xlock(&(_m)->sx); \ 59328653Shselasky} while (0) 60293194Shselasky 61328653Shselasky#define mutex_lock_nested(_m, _s) mutex_lock(_m) 62328653Shselasky#define mutex_lock_nest_lock(_m, _s) mutex_lock(_m) 63294837Shselasky 64294837Shselasky#define mutex_lock_interruptible(_m) ({ \ 65328653Shselasky MUTEX_SKIP() ? 0 : \ 66328653Shselasky linux_mutex_lock_interruptible(_m); \ 67294837Shselasky}) 68294837Shselasky 69328653Shselasky#define mutex_unlock(_m) do { \ 70328653Shselasky if (MUTEX_SKIP()) \ 71328653Shselasky break; \ 72328653Shselasky sx_xunlock(&(_m)->sx); \ 73328653Shselasky} while (0) 74294837Shselasky 75294837Shselasky#define mutex_trylock(_m) ({ \ 76328653Shselasky MUTEX_SKIP() ? 1 : \ 77329970Shselasky !!sx_try_xlock(&(_m)->sx); \ 78328653Shselasky}) 79328653Shselasky 80329970Shselaskyenum mutex_trylock_recursive_enum { 81328653Shselasky MUTEX_TRYLOCK_FAILED = 0, 82328653Shselasky MUTEX_TRYLOCK_SUCCESS = 1, 83328653Shselasky MUTEX_TRYLOCK_RECURSIVE = 2, 84328653Shselasky}; 85329970Shselasky 86329970Shselaskystatic inline __must_check enum mutex_trylock_recursive_enum 87329970Shselaskymutex_trylock_recursive(struct mutex *lock) 88328653Shselasky{ 89328653Shselasky if (unlikely(sx_xholder(&lock->sx) == curthread)) 90328653Shselasky return (MUTEX_TRYLOCK_RECURSIVE); 91328653Shselasky 92328653Shselasky return (mutex_trylock(lock)); 93328653Shselasky} 94328653Shselasky 95328653Shselasky#define mutex_init(_m) \ 96328653Shselasky linux_mutex_init(_m, mutex_name(#_m), SX_NOWITNESS) 97328653Shselasky 98328653Shselasky#define mutex_init_witness(_m) \ 99328653Shselasky linux_mutex_init(_m, mutex_name(#_m), SX_DUPOK) 100328653Shselasky 101328653Shselasky#define mutex_destroy(_m) \ 102328653Shselasky linux_mutex_destroy(_m) 103345934Shselasky 104345934Shselaskystatic inline bool 105345934Shselaskymutex_is_locked(mutex_t *m) 106345934Shselasky{ 107345934Shselasky return ((struct thread *)SX_OWNER(m->sx.sx_lock) != NULL); 108345934Shselasky} 109293194Shselasky 110static inline bool 111mutex_is_owned(mutex_t *m) 112{ 113 return (sx_xlocked(&m->sx)); 114} 115 116#ifdef WITNESS_ALL 117/* NOTE: the maximum WITNESS name is 64 chars */ 118#define __mutex_name(name, file, line) \ 119 (((const char *){file ":" #line "-" name}) + \ 120 (sizeof(file) > 16 ? sizeof(file) - 16 : 0)) 121#else 122#define __mutex_name(name, file, line) name 123#endif 124#define _mutex_name(...) __mutex_name(__VA_ARGS__) 125#define mutex_name(name) _mutex_name(name, __FILE__, __LINE__) 126 127#define DEFINE_MUTEX(lock) \ 128 mutex_t lock; \ 129 SX_SYSINIT_FLAGS(lock, &(lock).sx, mutex_name(#lock), SX_DUPOK) 130 131static inline void 132linux_mutex_init(mutex_t *m, const char *name, int flags) 133{ 134 memset(m, 0, sizeof(*m)); 135 sx_init_flags(&m->sx, name, flags); 136} 137 138static inline void 139linux_mutex_destroy(mutex_t *m) 140{ 141 if (mutex_is_owned(m)) 142 mutex_unlock(m); 143 sx_destroy(&m->sx); 144} 145 146extern int linux_mutex_lock_interruptible(mutex_t *m); 147 148#endif /* _LINUX_MUTEX_H_ */ 149