# Exclusion Control (SpinLock): Overview

SpinLock is a mechanism that uses the ARM processor `swp` command to perform exclusion control among threads and processors.

Mutex and Message can also be used to perform exclusion control among threads, but unlike those mechanisms, spin lock does not cause thread switching when locked. Instead, it loops, waiting for the locks to become unlocked.

Spin locks are used for the following purposes within the TWL-SDK.

- Exclusive control between processors for accessing the Game Pak.
- Exclusive control between processors for accessing the Game Card.
- Exclusive control between processors for accessing VRAM C and VRAM D.
- Synchronization between processors when they are initialized (`OS_InitLock`).

This page describes the synchronization processing functions that use these TWL-SDK spin locks.

## Initializing a Spin Lock Used by the SDK

`OS_InitLock()` performs the initialization for spin locks used by the SDK. However, since this is called from inside `OS_Init()`, there is no need to call it with the application if `OS_Init()` is called.

`OS_InitLock()` also performs the process for synchronizing with the ``OS_InitLock() on other processors.``

## Card Access Exclusion Control

Generally, ARM7 has Game Card access privileges. Calling `OS_LockCard()` gives access privileges to ARM9, and locks out access from ARM7.

Calling `OS_UnlockCard()` returns the access privileges to ARM7 and removes the lock.

`OS_LockCard()` does not return from the function until there is a lock, but `OS_TryLockCard()` only attempts the lock once. The return value indicates whether the lock was successful.

## GamePak Access Exclusion Control

Generally, ARM7 has Game Pak access privileges. Calling `OS_LockCartridge()` gives access privileges to ARM9 and locks out access from ARM7.

Calling `OS_UnlockCartridge()` returns the access privileges to ARM7 and removes the lock.

`OS_LockCartridge()` does not return from the function until there is a lock, but `OS_TryLockCartridge()` only attempts the lock once and returns immediately. The return value indicates whether the lock was successful.

## ID Used in the Lock Function

When the ID used in the lock (a `u16` value) attempts to access the same resources between threads or between processors (cards, GamePaks, etc.), they must have different values. The TWL-SDK provides the following functions to manipulate these lock IDs.

`OS_GetLockID()` returns a value that is not in use from among the ID candidates available on ARM9. This value guarantees that there is no conflict with other modules regardless of whether it is on ARM9 or ARM7. However, one of the conditions is that other modules are also using `OS_GetLockID()` to obtain values. The `OS_GetLockID` function is also used in places that need it within the TWL-SDK, so there is no conflict with values set by the application.

Once an ID is no longer in use, unlock it with `OS_ReleaseLockID()`. By calling this function, the specified ID is returned to the `OS_GetLockID()` candidates and can be used in other locks from any subsequent `OS_GetLockID()`.

The values that can be obtained with `OS_GetLockID()` range from `0x40 - 0x6F`.

The same function is also in the ARM7-side library and can obtain values from `0x80 - 0xA0`.

## See Also

`Overview of OS Functions (Exclusion Control)
`

## Revision History

2004/12/14 Corrected typos.
2004/11/15 Initial version.

CONFIDENTIAL