|
|
Log in / Subscribe / Register

Lockless patterns: more read-modify-write operations

Lockless patterns: more read-modify-write operations

Posted Mar 20, 2021 23:48 UTC (Sat) by dgc (subscriber, #6611)
Parent article: Lockless patterns: more read-modify-write operations

Isn't your put_gadget() example just demonstrating why the atomic_dec_and_lock() primitive exists?

Wouldn't it be better to teach people this pattern for atomically grabbing a lock when dropping the last reference to an object as it is much easier to understand and hard to get wrong?

put_gadget()
{
       if (!atomic_dec_and_lock(&g->refcnt, &lock))
             return;
       list_del(&g->node);
       spin_unlock(&lock);
}

This is assuming, of course, that an unlocked get side is using atomic_inc_not_zero() to avoid get/put races...

-Dave.


The LWN site is currently under high scraper load, so comment display has been suppressed for anonymous users. If you are a human, you may read the comments by clicking the button below:

Note: you can avoid this step in the future by logging into your LWN account.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds