

A spinlock is a mechanism that uses the ARM processor swp instruction to perform exclusion control among threads and processors.
Mutexes and messages can also be used to perform exclusion control among threads, but unlike those mechanisms, spinlocks do not cause thread switching when locked. Instead, control loops, waiting for the locks to become unlocked.
Spinlocks are used for the following purposes within the TWL-SDK.
OS_InitLock).
This page describes the synchronization processing functions that use these TWL-SDK spinlocks.
OS_InitLock performs the initialization for spinlocks used by the SDK. This function is called by the OS_Init function, so there is no need for the application to call it again.
The OS_InitLock function also synchronizes processing with the other processor's OS_InitLock function.
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.
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.
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.
Overview of OS Functions (Exclusion Control)
2009/06/03 Explained that calling OS_Init is now required.
2004/12/14 Corrected typos.
2004/11/15 Initial version.
CONFIDENTIAL