1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-18 10:35:55 +00:00

Add rw_try_rlock() and rw_try_wlock() to rwlocks.

These functions try the specified operation (rlocking and wlocking) and
true is returned if the operation completes, false otherwise.

The KPI is enriched by this commit, so __FreeBSD_version bumping and
manpage updating will happen soon.

Requested by:	jeff, kris
This commit is contained in:
Attilio Rao 2008-04-01 20:31:55 +00:00
parent 60cdfde09f
commit b31a149bbb
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=177843
2 changed files with 53 additions and 0 deletions

View File

@ -203,6 +203,30 @@ _rw_wlock(struct rwlock *rw, const char *file, int line)
curthread->td_locks++;
}
int
_rw_try_wlock(struct rwlock *rw, const char *file, int line)
{
int rval;
KASSERT(rw->rw_lock != RW_DESTROYED,
("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line));
if (rw_wlocked(rw) && (rw->lock_object.lo_flags & RW_RECURSE) != 0) {
rw->rw_recurse++;
rval = 1;
} else
rval = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_UNLOCKED,
(uintptr_t)curthread);
LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line);
if (rval) {
WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
file, line);
curthread->td_locks++;
}
return (rval);
}
void
_rw_wunlock(struct rwlock *rw, const char *file, int line)
{
@ -386,6 +410,31 @@ _rw_rlock(struct rwlock *rw, const char *file, int line)
curthread->td_rw_rlocks++;
}
int
_rw_try_rlock(struct rwlock *rw, const char *file, int line)
{
uintptr_t x;
for (;;) {
x = rw->rw_lock;
KASSERT(rw->rw_lock != RW_DESTROYED,
("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line));
if (!(x & RW_LOCK_READ))
break;
if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, x + RW_ONE_READER)) {
LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file,
line);
WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line);
curthread->td_locks++;
curthread->td_rw_rlocks++;
return (1);
}
}
LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line);
return (0);
}
void
_rw_runlock(struct rwlock *rw, const char *file, int line)
{

View File

@ -132,8 +132,10 @@ void rw_destroy(struct rwlock *rw);
void rw_sysinit(void *arg);
int rw_wowned(struct rwlock *rw);
void _rw_wlock(struct rwlock *rw, const char *file, int line);
int _rw_try_wlock(struct rwlock *rw, const char *file, int line);
void _rw_wunlock(struct rwlock *rw, const char *file, int line);
void _rw_rlock(struct rwlock *rw, const char *file, int line);
int _rw_try_rlock(struct rwlock *rw, const char *file, int line);
void _rw_runlock(struct rwlock *rw, const char *file, int line);
void _rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file,
int line);
@ -165,7 +167,9 @@ void _rw_assert(struct rwlock *rw, int what, const char *file, int line);
#endif
#define rw_rlock(rw) _rw_rlock((rw), LOCK_FILE, LOCK_LINE)
#define rw_runlock(rw) _rw_runlock((rw), LOCK_FILE, LOCK_LINE)
#define rw_try_rlock(rw) _rw_try_rlock((rw), LOCK_FILE, LOCK_LINE)
#define rw_try_upgrade(rw) _rw_try_upgrade((rw), LOCK_FILE, LOCK_LINE)
#define rw_try_wlock(rw) _rw_try_wlock((rw), LOCK_FILE, LOCK_LINE)
#define rw_downgrade(rw) _rw_downgrade((rw), LOCK_FILE, LOCK_LINE)
#define rw_unlock(rw) do { \
if (rw_wowned(rw)) \