12 KiB
Development Guidelines
Usage Scenarios
A spinlock provides a mutual exclusion mechanism to prevent two tasks from accessing the same shared resource at the same time.
Functions
The spinlock module of Huawei LiteOS provides the following functions. For details about the APIs, see the API reference.
Development Process
The typical spinlock development process is as follows:
-
Spinlocks depend on the SMP. Run the make menuconfig command and choose Kernel > Enable Kernel SMP to configure the spinlock.
-
To create a spinlock, call the LOS_SpinInit API to initialize the spinlock, or call SPIN_LOCK_INIT API to initialize the spinlock of the static memory.
-
Call the LOS_SpinLock, LOS_SpinTrylock, or LOS_SpinLockSave API to request a spinlock. If the request is successful, the code for lock protection is executed. If the request fails, the system enters a busy loop till a spinlock is obtained.
-
Call the LOS_SpinUnlock/LOS_SpinUnlockRestore API to release a spinlock. After the lock protection code is executed, the corresponding spinlock is released, so that other cores can request the spinlock.