Changeset View
Changeset View
Standalone View
Standalone View
head/sys/kern/kern_rangelock.c
Show First 20 Lines • Show All 135 Lines • ▼ Show 20 Lines | for (entry = lock->rl_currdep; entry != NULL; entry = nextentry) { | ||||
wakeup(entry); | wakeup(entry); | ||||
} | } | ||||
out: | out: | ||||
lock->rl_currdep = entry; | lock->rl_currdep = entry; | ||||
} | } | ||||
static void | static void | ||||
rangelock_unlock_locked(struct rangelock *lock, struct rl_q_entry *entry, | rangelock_unlock_locked(struct rangelock *lock, struct rl_q_entry *entry, | ||||
struct mtx *ilk) | struct mtx *ilk, bool do_calc_block) | ||||
{ | { | ||||
MPASS(lock != NULL && entry != NULL && ilk != NULL); | MPASS(lock != NULL && entry != NULL && ilk != NULL); | ||||
mtx_assert(ilk, MA_OWNED); | mtx_assert(ilk, MA_OWNED); | ||||
if (!do_calc_block) { | |||||
/* | |||||
* This is the case where rangelock_enqueue() has been called | |||||
* with trylock == true and just inserted this entry in the | |||||
* queue. | |||||
* If rl_currdep is this entry, rl_currdep needs to | |||||
* be set to the next entry in the rl_waiters list. | |||||
* However, since this entry is the last entry in the | |||||
* list, the next entry is NULL. | |||||
*/ | |||||
if (lock->rl_currdep == entry) { | |||||
KASSERT(TAILQ_NEXT(lock->rl_currdep, rl_q_link) == NULL, | |||||
("rangelock_enqueue: next entry not NULL")); | |||||
lock->rl_currdep = NULL; | |||||
} | |||||
} else | |||||
KASSERT(entry != lock->rl_currdep, ("stuck currdep")); | KASSERT(entry != lock->rl_currdep, ("stuck currdep")); | ||||
TAILQ_REMOVE(&lock->rl_waiters, entry, rl_q_link); | TAILQ_REMOVE(&lock->rl_waiters, entry, rl_q_link); | ||||
if (do_calc_block) | |||||
rangelock_calc_block(lock); | rangelock_calc_block(lock); | ||||
mtx_unlock(ilk); | mtx_unlock(ilk); | ||||
if (curthread->td_rlqe == NULL) | if (curthread->td_rlqe == NULL) | ||||
curthread->td_rlqe = entry; | curthread->td_rlqe = entry; | ||||
else | else | ||||
rlqentry_free(entry); | rlqentry_free(entry); | ||||
} | } | ||||
void | void | ||||
rangelock_unlock(struct rangelock *lock, void *cookie, struct mtx *ilk) | rangelock_unlock(struct rangelock *lock, void *cookie, struct mtx *ilk) | ||||
{ | { | ||||
MPASS(lock != NULL && cookie != NULL && ilk != NULL); | MPASS(lock != NULL && cookie != NULL && ilk != NULL); | ||||
mtx_lock(ilk); | mtx_lock(ilk); | ||||
rangelock_unlock_locked(lock, cookie, ilk); | rangelock_unlock_locked(lock, cookie, ilk, true); | ||||
} | } | ||||
/* | /* | ||||
* Unlock the sub-range of granted lock. | * Unlock the sub-range of granted lock. | ||||
*/ | */ | ||||
void * | void * | ||||
rangelock_unlock_range(struct rangelock *lock, void *cookie, off_t start, | rangelock_unlock_range(struct rangelock *lock, void *cookie, off_t start, | ||||
off_t end, struct mtx *ilk) | off_t end, struct mtx *ilk) | ||||
{ | { | ||||
struct rl_q_entry *entry; | struct rl_q_entry *entry; | ||||
MPASS(lock != NULL && cookie != NULL && ilk != NULL); | MPASS(lock != NULL && cookie != NULL && ilk != NULL); | ||||
entry = cookie; | entry = cookie; | ||||
KASSERT(entry->rl_q_flags & RL_LOCK_GRANTED, | KASSERT(entry->rl_q_flags & RL_LOCK_GRANTED, | ||||
("Unlocking non-granted lock")); | ("Unlocking non-granted lock")); | ||||
KASSERT(entry->rl_q_start == start, ("wrong start")); | KASSERT(entry->rl_q_start == start, ("wrong start")); | ||||
KASSERT(entry->rl_q_end >= end, ("wrong end")); | KASSERT(entry->rl_q_end >= end, ("wrong end")); | ||||
mtx_lock(ilk); | mtx_lock(ilk); | ||||
if (entry->rl_q_end == end) { | if (entry->rl_q_end == end) { | ||||
rangelock_unlock_locked(lock, cookie, ilk); | rangelock_unlock_locked(lock, cookie, ilk, true); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
entry->rl_q_end = end; | entry->rl_q_end = end; | ||||
rangelock_calc_block(lock); | rangelock_calc_block(lock); | ||||
mtx_unlock(ilk); | mtx_unlock(ilk); | ||||
return (cookie); | return (cookie); | ||||
} | } | ||||
/* | /* | ||||
* Add the lock request to the queue of the pending requests for | * Add the lock request to the queue of the pending requests for | ||||
* rangelock. Sleep until the request can be granted. | * rangelock. Sleep until the request can be granted unless trylock == true. | ||||
*/ | */ | ||||
static void * | static void * | ||||
rangelock_enqueue(struct rangelock *lock, off_t start, off_t end, int mode, | rangelock_enqueue(struct rangelock *lock, off_t start, off_t end, int mode, | ||||
struct mtx *ilk) | struct mtx *ilk, bool trylock) | ||||
{ | { | ||||
struct rl_q_entry *entry; | struct rl_q_entry *entry; | ||||
struct thread *td; | struct thread *td; | ||||
MPASS(lock != NULL && ilk != NULL); | MPASS(lock != NULL && ilk != NULL); | ||||
td = curthread; | td = curthread; | ||||
if (td->td_rlqe != NULL) { | if (td->td_rlqe != NULL) { | ||||
Show All 9 Lines | rangelock_enqueue(struct rangelock *lock, off_t start, off_t end, int mode, | ||||
mtx_lock(ilk); | mtx_lock(ilk); | ||||
/* | /* | ||||
* XXXKIB TODO. Check that a thread does not try to enqueue a | * XXXKIB TODO. Check that a thread does not try to enqueue a | ||||
* lock that is incompatible with another request from the same | * lock that is incompatible with another request from the same | ||||
* thread. | * thread. | ||||
*/ | */ | ||||
TAILQ_INSERT_TAIL(&lock->rl_waiters, entry, rl_q_link); | TAILQ_INSERT_TAIL(&lock->rl_waiters, entry, rl_q_link); | ||||
/* | |||||
* If rl_currdep == NULL, there is no entry waiting for a conflicting | |||||
* range to be resolved, so set rl_currdep to this entry. If there is | |||||
* no conflicting entry for this entry, rl_currdep will be set back to | |||||
* NULL by rangelock_calc_block(). | |||||
*/ | |||||
if (lock->rl_currdep == NULL) | if (lock->rl_currdep == NULL) | ||||
lock->rl_currdep = entry; | lock->rl_currdep = entry; | ||||
rangelock_calc_block(lock); | rangelock_calc_block(lock); | ||||
while (!(entry->rl_q_flags & RL_LOCK_GRANTED)) | while (!(entry->rl_q_flags & RL_LOCK_GRANTED)) { | ||||
if (trylock) { | |||||
/* | |||||
* For this case, the range is not actually locked | |||||
* yet, but removal from the list requires the same | |||||
* steps, except for not doing a rangelock_calc_block() | |||||
* call, since rangelock_calc_block() was called above. | |||||
*/ | |||||
rangelock_unlock_locked(lock, entry, ilk, false); | |||||
return (NULL); | |||||
} | |||||
msleep(entry, ilk, 0, "range", 0); | msleep(entry, ilk, 0, "range", 0); | ||||
} | |||||
mtx_unlock(ilk); | mtx_unlock(ilk); | ||||
return (entry); | return (entry); | ||||
} | } | ||||
void * | void * | ||||
rangelock_rlock(struct rangelock *lock, off_t start, off_t end, struct mtx *ilk) | rangelock_rlock(struct rangelock *lock, off_t start, off_t end, struct mtx *ilk) | ||||
{ | { | ||||
return (rangelock_enqueue(lock, start, end, RL_LOCK_READ, ilk)); | return (rangelock_enqueue(lock, start, end, RL_LOCK_READ, ilk, false)); | ||||
} | } | ||||
void * | void * | ||||
rangelock_tryrlock(struct rangelock *lock, off_t start, off_t end, | |||||
struct mtx *ilk) | |||||
{ | |||||
return (rangelock_enqueue(lock, start, end, RL_LOCK_READ, ilk, true)); | |||||
} | |||||
void * | |||||
rangelock_wlock(struct rangelock *lock, off_t start, off_t end, struct mtx *ilk) | rangelock_wlock(struct rangelock *lock, off_t start, off_t end, struct mtx *ilk) | ||||
{ | { | ||||
return (rangelock_enqueue(lock, start, end, RL_LOCK_WRITE, ilk)); | return (rangelock_enqueue(lock, start, end, RL_LOCK_WRITE, ilk, false)); | ||||
} | |||||
void * | |||||
rangelock_trywlock(struct rangelock *lock, off_t start, off_t end, | |||||
struct mtx *ilk) | |||||
{ | |||||
return (rangelock_enqueue(lock, start, end, RL_LOCK_WRITE, ilk, true)); | |||||
} | } |