bitops or mutex

Valdis.Kletnieks at vt.edu Valdis.Kletnieks at vt.edu
Mon Jan 21 09:33:27 EST 2013


On Mon, 21 Jan 2013 19:16:47 +0530, Prashant Shah said:
> There is a bitmap that needs to be locked across many threads for test
> / set bit operations. Which one is faster - bitops or mutex ?
>
> 1. Bitops :
> set_bit(5, (long unsigned *)&tmp);
>
> 2. Mutex :
> mutex_lock(&m);
> *tmp = (*tmp) | (1 << 5);
> mutex_unlock(&m);

Do you care about "faster" as in "less latency", or "less total cycles
consumed"?  The two can be quite different....

One uses a mutex, the other a spinlock and irq save/restore.  "faster" will
depend on the architecture (irqsave is more expensive on some archs than
others) and how heavily the lock is contended.  If the answer *really* matters,
you better go ahead and instrument the code and actually time it and do the
statistical analysis.

Also, double-check that you don't require *additional* locking. It's pretty
rare that the *entire* critical section is exactly one bit-set operation long

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 865 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20130121/8c248f69/attachment.bin 


More information about the Kernelnewbies mailing list