![]() |
![]() |
![]() |
Guard a critical section in an interrupt handler
#include <sys/neutrino.h> void InterruptLock( intrspin_t* spinlock );
libc
The InterruptLock() function guards a critical section by locking the specified spinlock. It may be called from a thread or from an interrupt handler. Before calling this function, the thread must request I/O privity by calling:
ThreadCtl( _NTO_TCTL_IO, 0 );
If the thread doesn't do this, it might SIGSEGV when InterruptUnlock() is called.
This function attempts to acquire the spinlock (a variable shared between the interrupt handler and a thread) while interrupts are disabled. The code spins in a tight loop until the lock is acquired. It's important that the lock be released as soon as possible. Typically, this is a few lines of code without any loops.
![]() |
If spinlock isn't a static variable, it must be
initialized with:
memset( spinlock, 0, sizeof( *spinlock ) ); before being used with InterruptLock(). |
InterruptLock( &spinner ); /* ... critical section */ InterruptUnlock( &spinner );
InterruptLock() solves a common need in many realtime systems to protect access to shared data structures between an interrupt handler and the thread that owns the handler. The traditional POSIX primitives used between threads aren't available for use by an interrupt handler.
The InterruptLock() and InterruptUnlock() functions work on single-processor or multi-processor machines.
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | Yes |
Signal handler | Yes |
Thread | Yes |
InterruptDisable(), InterruptEnable(), InterruptMask(), InterruptUnlock(), InterruptUnmask(), ThreadCtl()
![]() |
![]() |
![]() |