Spin lock Initialization and Use

The interested reader can refer the following articles on spinlocks.

http://www.hitchhikersguidetolearning.com/2021/01/03/linux-kernel-locking-mechanisms-spinlock/

The Spinlock can be initialized via “DEFINE_SPINLOCK” MACRO or  by invoking the “spin_lock_init” MACRO. The “DEFINE_SPINLOCK” MACRO is usually used when the spinlock is statically assigned. The “spin_lock_init” MACRO is usually used when the spinlock variable is part of a structure which is dynamically assigned memory during run time.

The MACRO – DEFINE_SPINLOCK, that is used to initialize a spinlock is provided below (present in spinlock_types.h header file).

 

The macro sets the lock in “unlocked state” that can be used to lock a particular critical section. A sample code depicting the same is provided below

The  MACRO that is used to initialize a spinlock during runtime is the “spin_lock_init” MACRO and is provided below for reference (As defined in spinlock.h 4.15 Linux kernel).

A sample code depicting the usage of the above macro is provided below:

The Makefile to compile the above two sample codes is provided below

For further information on spinlocks – refer the link below

https://www.kernel.org/doc/Documentation/locking/spinlocks.txt

Mutexes in the Linux kernel

Comments

  1. Pingback: spinlock structure in Linux kernel | Hitch Hiker's Guide to Learning

Leave a Reply

Your email address will not be published. Required fields are marked *