1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 2<HTML> 3<HEAD> 4<META http-equiv="Content-Type" content="text/html; charset=windows-1252"> 5<META name="GENERATOR" content="IBM WebSphere Studio Homepage Builder Version 7.0.1.0 for Windows"> 6<META http-equiv="Content-Style-Type" content="text/css"> 7<TITLE>Exclusion Control (Spinlock): </TITLE> 8<LINK rel="stylesheet" href="../../css/nitro.css" type="text/css"> 9</HEAD> 10<BODY> 11<H1 align="left">Exclusion Control (Spinlock): Overview<IMG src="../../image/NTR.gif" align="middle"><IMG src="../../image/TWL.gif" align="middle"></H1> 12<P>A spinlock is a mechanism that uses the ARM processor <code>swp</code> instruction to perform exclusion control among threads and processors.</P> 13<P>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.</P> 14<P>Spinlocks are used for the following purposes within the TWL-SDK.</P> 15<UL> 16<LI>Exclusive control between processors for accessing the Game Pak. 17<LI>Exclusive control between processors for accessing the Game Card. 18<LI>Exclusive control between processors for accessing VRAM C and VRAM D. 19<LI>Synchronization between processors when they are initialized (<CODE><A href="OS_InitLock.html">OS_InitLock</A></CODE>). 20</UL> 21<P>This page describes the synchronization processing functions that use these TWL-SDK spinlocks.</P> 22<H2>Initializing a Spinlock Used by the SDK</H2> 23<P><CODE><A href="OS_InitLock.html">OS_InitLock</A></CODE> performs the initialization for spinlocks used by the SDK. This function is called by the <CODE><A href="../init/OS_Init.html">OS_Init</A></CODE> function, so there is no need for the application to call it again.</P> 24<P>The <CODE><A href="OS_InitLock.html">OS_InitLock</A></CODE> function also synchronizes processing with the other processor's <CODE><A href="OS_InitLock.html">OS_InitLock</A></CODE> function.</P> 25<H2>Card Access Exclusion Control</H2> 26<P>Generally, ARM7 has Game Card access privileges. Calling <code><a href="OS_LockCard.html">OS_LockCard</a></code> gives access privileges to ARM9, and locks out access from ARM7.</P> 27<P>Calling <code><a href="OS_UnlockCard.html">OS_UnlockCard</a></code> returns the access privileges to ARM7 and removes the lock.</P> 28<P><code><a href="OS_LockCard.html">OS_LockCard</a></code> does not return from the function until there is a lock, but <code><a href="OS_TryLockCard.html">OS_TryLockCard</a></code> only attempts the lock once. The return value indicates whether the lock was successful.</P> 29<H2>GamePak Access Exclusion Control</H2> 30<P>Generally, ARM7 has Game Pak access privileges. Calling <code><a href="OS_LockCartridge.html">OS_LockCartridge</a></code> gives access privileges to ARM9 and locks out access from ARM7.</P> 31<P>Calling <a href="OS_UnlockCartridge.html"><code>OS_UnlockCartridge</code></a> returns the access privileges to ARM7 and removes the lock.</P> 32<P><code><a href="OS_LockCartridge.html">OS_LockCartridge</a></code> does not return from the function until there is a lock, but <code><a href="OS_TryLockCartridge.html">OS_TryLockCartridge</a></code> only attempts the lock once and returns immediately. The return value indicates whether the lock was successful.</P> 33<H2>ID Used in the Lock Function</H2> 34<P>When the ID used in the lock (a <code>u16</code> 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.</P> 35<P><code><a href="OS_GetLockID.html">OS_GetLockID</a></code> 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 <code><a href="OS_GetLockID.html">OS_GetLockID</a></code> to obtain values. The <CODE><A href="OS_GetLockID.html">OS_GetLockID</A></CODE> function is also used in places that need it within the TWL-SDK, so there is no conflict with values set by the application.</P> 36<P>Once an ID is no longer in use, unlock it with <code><a href="OS_ReleaseLockID.html">OS_ReleaseLockID</a></code>. By calling this function, the specified ID is returned to the <code><a href="OS_GetLockID.html">OS_GetLockID</a></code> candidates and can be used in other locks from any subsequent <code><a href="OS_GetLockID.html">OS_GetLockID</a></code>.</P> 37<P>The values that can be obtained with <code><a href="OS_GetLockID.html">OS_GetLockID</a></code> range from <code>0x40 - 0x6F</code>.</P> 38<P>The same function is also in the ARM7-side library and can obtain values from <code>0x80 - 0xA0</code>. 39</P> 40<H2>See Also</H2> 41<P><CODE><A href="../list_os.html#Mutex">Overview of OS Functions (Exclusion Control)<BR></A></CODE></P> 42<H2>Revision History</H2> 43<P>2009/06/03 Explained that calling <CODE>OS_Init</CODE> is now required.<BR> 2004/12/14 Corrected typos.<br /> 2004/11/15 Initial version.</P> 44<hr><p>CONFIDENTIAL</p></body> 45</HTML>