- Multiple threads allows the programmer to run particular job independent of all the others. ex:- spell check in wordpad.
- Multiple threads can run on multiple CPUs, providing a performance improvement.
Multithreaded applications requires synchronization.
- mutex - Only the thread that locks a mutex can unlock it.
- semaphore - Binary semaphore is equal to mutex, which can be unlocked by other threads. ex:- with semaphore, a thread to wait for other threads.
The pieces of code protected by mutex and semaphore is called Critical Section.
Implementing Semaphores on ARM Processors
- Semaphores are used to manage access to a shared resource. Unfortunately, semaphore themselves are shared resources. Who will protect semaphore? ha ha ha...
- In single core system, easy way to avoid the issue is, preventing any other interrupts from being served while we access (read–modify–write) the semaphore.
MRS r12, CPSR ; read CPSR ORR r12, r12, #I_bit ; set I bit MSR CPSR_c, r12 ; write back CPSR
CPSID i ; disable IRQ- In multi core system, we need a mechanism to prevent the other core from accessing the system bus, while one task in one core carries out the read–modify–write sequence. SWP disables interrupt and blocks system bus, causing critical performance bottleneck.
LOCKED EQU 0 ; define value indicating LDR r1, <addr> ; load semaphore address LDR r0, =LOCKED ; preload "locked" value spin_lock SWP r0, r0, [r1] ; swap register value with semaphore CMP r0, #LOCKED ; if semaphore was locked already BEQ spin_lock ; retry- A new, non-blocking method is Exclusive load (LDREX) (reads and tags the memory) and Exclusive store (STREX) (stores data to memory only if the tag is still valid). With this mechanism, bus masters won't be locked out from memory access altogether, but only if they access the same memory.
LOCKED EQU 0 ; define value indicating LDR r12, <addr> ; preload semaphore address LDR r1, =LOCKED ; preload "locked" value spin_lock LDREX r0, [r12] ; load semaphore value CMP r0, #LOCKED ; if semaphore was locked already STREXNE r0, r1, [r12] ; try to claim CMPNE r0, #1 ; and check success BEQ spin_lock ; retry if claiming semaphore failed.
Reference:
http://softpixel.com/~cwright/programming/threads/threads.c.php
http://koti.mbnet.fi/niclasw/MutexSemaphore.html
https://www.doulos.com/knowhow/arm/Hints_and_Tips/Implementing_Semaphores/
No comments:
Post a Comment