read ->0
read ->1
write 0
write 1
write 0/1
reset
Not taken
state
Taken
state
Preliminary
Spinlock
www.ti.com
1.14.3.3 About Spinlocks
Spinlocks are present to solve the need for synchronization and mutual exclusion between heterogeneous
processors and those not operating under a single, shared operating system. There is no alternative
mechanism to accomplish these operations between processors in separate subsystems.
Spinlocks are not the best way to synchronize between tasks or threads on one CPU. Instead, spinlocks
are for use in synchronization between different subsystems in the device that don't have any other means
of hardware-based synchronization.
Spinlocks do not solve all system synchronization issues. They have limited applicability and should be
used with care to implement higher level synchronization protocols.
A spinlock is appropriate for mutual exclusion for access to a shared data structure. It should be used only
when:
•
The time to hold the lock is predictable and small (for example, a maximum hold time of less than 200
CPU cycles may be acceptable).
•
The locking task cannot be preempted, suspended, or interrupted while holding the lock (this would
make the hold time large and unpredictable).
•
The lock is lightly contended, that is the chance of any other process (or processor) trying to acquire
the lock while it is held is small.
If these conditions are met, then the locking code can retry a failed attempt to acquire the lock until
success.
If the conditions are not met, then a spinlock is not a good candidate. One alternative is to use a spinlock
for critical section control (engineered to meet the conditions) to implement a higher level semaphore that
can support preemption, notification, timeout or other higher level properties.
1.14.3.4 Functional Operation
The Spinlock module supports 64 spinlocks. It accepts only a single command at a time and processes
the command fully before accepting the next command. A lock is requested by reading the
SPINLOCK_LOCK_REG_i[0] TAKEN bit. There are two states: Taken (SPINLOCK_LOCK_REG_i[0]
TAKEN = 1) or Not Taken (SPINLOCK_LOCK_REG_i[0] TAKEN = 0).
When the status of lock i (where i = 0 to 63) is Not Taken (free), a read from the
SPINLOCK_LOCK_REG_i register returns 0 and sets the lock to Taken (locked). When the status of lock i
is Taken, a read returns 1 and does not change the state of the lock. A write to the
SPINLOCK_LOCK_REG_i register does not change the state of lock, unless when writing 0 when the lock
is in Taken state. By doing this, the requester frees the lock.
CAUTION
Only 32-bit reads and writes are supported.
Figure 1-90. SPINLOCK_LOCK_REG_i Register State Diagram
240
Chip Level Resources
SPRUGX9 – 15 April 2011
© 2011, Texas Instruments Incorporated