Skip to content

Commit

Permalink
lockref: Add lockref_put_not_zero
Browse files Browse the repository at this point in the history
Put a lockref unless the lockref is dead or its count would become zero.
This is the same as lockref_put_or_lock except that the lock is never
left held.

Signed-off-by: Andreas Gruenbacher <[email protected]>
Signed-off-by: Bob Peterson <[email protected]>
  • Loading branch information
Andreas Gruenbacher authored and AstralBob committed Apr 12, 2018
1 parent e241e3f commit 450b1f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/linux/lockref.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct lockref {
extern void lockref_get(struct lockref *);
extern int lockref_put_return(struct lockref *);
extern int lockref_get_not_zero(struct lockref *);
extern int lockref_put_not_zero(struct lockref *);
extern int lockref_get_or_lock(struct lockref *);
extern int lockref_put_or_lock(struct lockref *);

Expand Down
28 changes: 28 additions & 0 deletions lib/lockref.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ int lockref_get_not_zero(struct lockref *lockref)
}
EXPORT_SYMBOL(lockref_get_not_zero);

/**
* lockref_put_not_zero - Decrements count unless count <= 1 before decrement
* @lockref: pointer to lockref structure
* Return: 1 if count updated successfully or 0 if count would become zero
*/
int lockref_put_not_zero(struct lockref *lockref)
{
int retval;

CMPXCHG_LOOP(
new.count--;
if (old.count <= 1)
return 0;
,
return 1;
);

spin_lock(&lockref->lock);
retval = 0;
if (lockref->count > 1) {
lockref->count--;
retval = 1;
}
spin_unlock(&lockref->lock);
return retval;
}
EXPORT_SYMBOL(lockref_put_not_zero);

/**
* lockref_get_or_lock - Increments count unless the count is 0 or dead
* @lockref: pointer to lockref structure
Expand Down

0 comments on commit 450b1f6

Please sign in to comment.