Synchronization Locks
The kernel
provides two types of locks; interrupt level and thread level locks.
Interrupt locks achieve synchronization by disabling maskable interrupts. Interrupt locks are intended to be used when
protecting a resource that is touched by an interrupt. The amount of time that interrupt locks are held should be minimized
since that time is directly added to the overall interrupt latency.
Thread locks achieve synchronization by disabling thread switching within the kernel. Although thread locks do not add
any interrupt latency, they do add latency to any higher priority threads.
Basic Usage
The code snippet below shows how to use an interrupt lock. The lock must be allocated on the stack (local/automatic variable).
#include "Kernel/kernel.h"
void function(void)
{
INTERRUPTLOCK lock; /* Allocate storage for the lock */
ACQUIREINTERRUPTLOCK();
/* This is now safe from thread
switches and interrupts */
RELEASEINTERRUPTLOCK();
}
The code snippet below demonstrates how to use a thread lock.
#include "Kernel/kernel.h"
#include <assert.h>
void function(void)
{
STATUS status;
status = THREAD_AcquireLock(); /* Acquire lock to suspend further thread switches */
assert(status == SUCCESS);
/* This is now safe from thread switches */
status = THREAD_ReleaseLock(); /* Release the lock and allow thread switches */
assert(status == SUCCESS);
}
API Reference
void ACQUIREINTERRUPTLOCK(void)
Acquires an interrupt lock by disabling maskable interrupts.
None.
void RELEASEINTERRUPTLOCK(void)
Releases a previously acquired interrupt lock by re-enabling maskable interrupts.
None.
STATUS THREAD_AcquireLock(void)
Acquires a lock upon the calling thread by suspending further context switches until this lock is released.
SUCCESS | The thread has been locked. |
ERR_INVALIDCONTEXT | The operation is not supported from the context of an interrupt service routine (ISR). |
STATUS THREAD_ReleaseLock(void)
Releases a previously acquired lock upon the current thread and resumes normal scheduling of threads.
SUCCESS | The thread lock has been released. |
ERR_INVALIDCONTEXT | The operation is not supported from the context of an interrupt service routine (ISR). |
ERR_INVALIDOPERATION | The calling thread is not locked. |