2010-05-17 22:18:00 +00:00
|
|
|
/*****************************************************************************\
|
|
|
|
* Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
|
|
|
|
* Copyright (C) 2007 The Regents of the University of California.
|
|
|
|
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
|
|
|
|
* Written by Brian Behlendorf <behlendorf1@llnl.gov>.
|
2008-05-26 04:38:26 +00:00
|
|
|
* UCRL-CODE-235197
|
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* This file is part of the SPL, Solaris Porting Layer.
|
2013-03-05 01:26:55 +00:00
|
|
|
* For details, see <http://zfsonlinux.org/>.
|
2008-05-26 04:38:26 +00:00
|
|
|
*
|
2010-05-17 22:18:00 +00:00
|
|
|
* The SPL is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* The SPL is distributed in the hope that it will be useful, but WITHOUT
|
2008-05-26 04:38:26 +00:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
2010-05-17 22:18:00 +00:00
|
|
|
* with the SPL. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*****************************************************************************
|
|
|
|
* Solaris Porting Layer (SPL) Reader/Writer Lock Implementation.
|
|
|
|
\*****************************************************************************/
|
2008-05-26 04:38:26 +00:00
|
|
|
|
2008-03-01 00:45:59 +00:00
|
|
|
#include <sys/rwlock.h>
|
2008-02-26 20:36:04 +00:00
|
|
|
|
2008-04-21 17:29:47 +00:00
|
|
|
#ifdef DEBUG_SUBSYSTEM
|
|
|
|
#undef DEBUG_SUBSYSTEM
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEBUG_SUBSYSTEM S_RWLOCK
|
|
|
|
|
2016-07-26 23:37:46 +00:00
|
|
|
#if defined(CONFIG_RWSEM_GENERIC_SPINLOCK)
|
2016-05-25 23:35:42 +00:00
|
|
|
static int
|
|
|
|
__rwsem_tryupgrade(struct rw_semaphore *rwsem)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
spl_rwsem_lock_irqsave(&rwsem->wait_lock, flags);
|
|
|
|
if (RWSEM_COUNT(rwsem) == SPL_RWSEM_SINGLE_READER_VALUE &&
|
|
|
|
list_empty(&rwsem->wait_list)) {
|
|
|
|
ret = 1;
|
|
|
|
RWSEM_COUNT(rwsem) = SPL_RWSEM_SINGLE_WRITER_VALUE;
|
|
|
|
}
|
|
|
|
spl_rwsem_unlock_irqrestore(&rwsem->wait_lock, flags);
|
|
|
|
return (ret);
|
|
|
|
}
|
2016-07-26 23:37:46 +00:00
|
|
|
#elif defined(HAVE_RWSEM_ATOMIC_LONG_COUNT)
|
|
|
|
static int
|
|
|
|
__rwsem_tryupgrade(struct rw_semaphore *rwsem)
|
|
|
|
{
|
|
|
|
long val;
|
|
|
|
val = atomic_long_cmpxchg(&rwsem->count, SPL_RWSEM_SINGLE_READER_VALUE,
|
|
|
|
SPL_RWSEM_SINGLE_WRITER_VALUE);
|
|
|
|
return (val == SPL_RWSEM_SINGLE_READER_VALUE);
|
|
|
|
}
|
2016-05-25 23:35:42 +00:00
|
|
|
#else
|
|
|
|
static int
|
|
|
|
__rwsem_tryupgrade(struct rw_semaphore *rwsem)
|
|
|
|
{
|
|
|
|
typeof (rwsem->count) val;
|
|
|
|
val = cmpxchg(&rwsem->count, SPL_RWSEM_SINGLE_READER_VALUE,
|
|
|
|
SPL_RWSEM_SINGLE_WRITER_VALUE);
|
|
|
|
return (val == SPL_RWSEM_SINGLE_READER_VALUE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
|
|
|
rwsem_tryupgrade(struct rw_semaphore *rwsem)
|
|
|
|
{
|
|
|
|
if (__rwsem_tryupgrade(rwsem)) {
|
|
|
|
rwsem_release(&rwsem->dep_map, 1, _RET_IP_);
|
|
|
|
rwsem_acquire(&rwsem->dep_map, 0, 1, _RET_IP_);
|
|
|
|
#ifdef CONFIG_RWSEM_SPIN_ON_OWNER
|
|
|
|
rwsem->owner = current;
|
|
|
|
#endif
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rwsem_tryupgrade);
|
|
|
|
|
2009-09-25 21:14:35 +00:00
|
|
|
int spl_rw_init(void) { return 0; }
|
|
|
|
void spl_rw_fini(void) { }
|