I'm thinking about how the feature "prevent from context switch" is achieved in Linux for a spinlock. A big picture / pseudocode is acceptable since I just what to know the logic behind it.
Spinlock is not a Linux feature, it is a very simple CPU feature, at least on most CPUs.
Spinlock doesn't prevent from context switching. Disabling context switching would also disable multitasking, which is unwanted on Linux in most cases.
However, spinlocks are protected from context switching. The essence of the spinlock is that a lock attempt happens in a single CPU command. No context switching can break a process inside a CPU command, only after that.
Implementation is not OS-specific, it is CPU-specific: it requires a mnemonic which can exchange the content of a register and a memory address, atomically. On x86, it can happen by xchg.