Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to prefer between spin_lock_init and DEFINE_SPINLOCK AND WHEN

I have a question. Is DEFINE_SPINLOCK preferable over spin_lock_init? What are the advantages of former over latter, and what are the possible draw-backs?

like image 749
srib Avatar asked Mar 19 '23 05:03

srib


1 Answers

If you have a static data structure, DEFINE_SPINLOCK lets you declare a spinlock variable and initialize it in one line. However for anything allocated at runtime, for example when a spinlock is embedded in a bigger structure, then you need to allocate the memory and then call spin_lock_init().

I guess I would say that I prefer DEFINE_SPINLOCK when it is possible to use it. The advantage is tiny (compile-time initialization vs. runtime initialization, a couple lines less of code) but there's no real drawback. As I mentioned above, it's often not possible to use DEFINE_SPINLOCK, though.

like image 184
Roland Avatar answered Apr 06 '23 10:04

Roland