# 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.

Function Category

API

Description

Spinlock initialization

LOS_SpinInit

Dynamically initializes a spinlock.

SPIN_LOCK_INIT

Statically initializes a spinlock.

Spinlock request and release

LOS_SpinLock

Requests a specified spinlock. If the spinlock cannot be obtained, the system waits in a loop.

LOS_SpinTrylock

Attempts to request a specified spinlock. If the spinlock cannot be obtained, a failure message is returned, and the system does not wait in a loop.

LOS_SpinUnlock

Releases a spinlock.

Spinlock request and release (with disabled interrupt)

LOS_SpinLockSave

Requests a spinlock after the interrupt is disabled.

LOS_SpinUnlockRestore

Releases a spinlock and restores the interrupt.

Spinlock holding status

LOS_SpinHeld

Checks whether a spinlock is held.

## Development Process The typical spinlock development process is as follows: 1. Spinlocks depend on the SMP. Run the **make menuconfig** command and choose **Kernel** \> **Enable Kernel SMP** to configure the spinlock.

Configuration Item

Description

Value Range

Default Value

Dependency

LOSCFG_KERNEL_SMP

Whether to enable the SMP.

YES/NO

YES

Multi-core hardware is supported.

LOSCFG_KERNEL_SMP_CORE_NUM

Number of cores in the multi-core scenario.

Depends on the architecture.

2

None

2. 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. 3. 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. 4. 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.