Notas
mutex_lock vs enter_region
Conteúdo das notas (1)
Página 1
enter_region:
TSL REGISTER, LOCK
CMP REGISTER, #0
JNE enter_region
RET
leave_region:
MOVE LOCK, #0
RET
mutex_lock:
TSL REGISTER, MUTEX
CMP REGISTER, #0
JZE ok
CALL thread_yield
JMP mutex_lock
ok: RET
mutex_unlock:
MOVE MUTEX, #0
RET
The code of mutex_lock is similar to the code of enter_region but with a critical difference. When enter_region fails to enter the critical region, it keeps testing the lock repeatedly (busy waiting). Eventually, the clock runs out and some other process is scheduled to run. Sooner or later the process holding the lock gets to run and releases it.
With (user) thread, the situation is different because there is no clock that stops threads that have run too long. Consequently, a thread that tries to acquire a lock by busy waiting will loop forever and never acquire the lock because it never allows any other thread to run and release the lock.
That is where the difference between enter_region and mutex_lock comes in. When the later fails to acquire a lock, it calls thread_yield to give up the CPU to another thread. Consequently, there is no busy waiting. When the thread runs the next time, it tests the lock again.
Since thread_yield is just a call to the thread scheduler in user space, it is very fast. As a consequence, neither mutex_lock nor mutex_unlock requires any kernel calls.
Informações
- Descrição
- The key difference between mutex_lock and enter_region, even though there code look similar.
- Criado por
- Ahmad Abdelwahed
- Visualizações
- 89
- Tags
- Avaliação