- movl 0(%edi),%eax /* spin checking lock value in cache */
- testl %eax,%eax
- je 6f /* zero => unlocked, try to grab it */
- decl %edx /* decrement inner loop count */
- jnz 5b /* time to check for timeout? */
-
- /*
- * Here after spinning INNER_LOOP_COUNT times, check for timeout
- */
- rdtsc /* cyclecount into %edx:%eax */
- cmpl %ecx,%edx /* compare high-order 32-bits */
- jb 4b /* continue spinning if less, or */
- cmpl %ebx,%eax /* compare low-order 32-bits */
- jb 5b /* continue if less, else bail */
- xor %eax,%eax /* with 0 return value */
- pop %ebx
- pop %edi
- LEAF_RET
-
-6:
- /*
- * Here to try to grab the lock that now appears to be free
- * after contention.
- */
- movl 8+L_PC,%edx /* calling pc (8+ for pushed regs) */
- DISABLE_PREEMPTION
- lock; cmpxchgl %edx,0(%edi) /* try to acquire the HW lock */
- jne 3b /* no - spin again */
- movl $1,%eax /* yes */
- pop %ebx
- pop %edi
- LEAF_RET
-
-/*
- * void hw_lock_unlock(hw_lock_t)
- *
- * Unconditionally release lock.
- * MACH_RT: release preemption level.
- */
-LEAF_ENTRY(hw_lock_unlock)
- movl L_ARG0,%edx /* fetch lock pointer */
- movl $0,0(%edx) /* clear the lock */
- ENABLE_PREEMPTION
- LEAF_RET
-
-/*
- * unsigned int hw_lock_try(hw_lock_t)
- * MACH_RT: returns with preemption disabled on success.
- */
-LEAF_ENTRY(hw_lock_try)
- movl L_ARG0,%edx /* fetch lock pointer */
-
- movl L_PC,%ecx
- DISABLE_PREEMPTION
- movl 0(%edx),%eax
- testl %eax,%eax
- jne 1f
- lock; cmpxchgl %ecx,0(%edx) /* try to acquire the HW lock */
- jne 1f
-
- movl $1,%eax /* success */
- LEAF_RET
-
-1: ENABLE_PREEMPTION /* failure: release preemption... */
- xorl %eax,%eax /* ...and return failure */
- LEAF_RET
-
-/*
- * unsigned int hw_lock_held(hw_lock_t)
- * MACH_RT: doesn't change preemption state.
- * N.B. Racy, of course.
- */
-LEAF_ENTRY(hw_lock_held)
- movl L_ARG0,%edx /* fetch lock pointer */
-
- movl 0(%edx),%eax /* check lock value */
- testl %eax,%eax
- movl $1,%ecx
- cmovne %ecx,%eax /* 0 => unlocked, 1 => locked */
- LEAF_RET
-
-LEAF_ENTRY(mutex_init)
- movl L_ARG0,%edx /* fetch lock pointer */
- xorl %eax,%eax
- movl %eax,M_ILK /* clear interlock */
- movl %eax,M_LOCKED /* clear locked flag */
- movw %ax,M_WAITERS /* init waiter count */
- movw %ax,M_PROMOTED_PRI
-
-#if MACH_LDEBUG
- movl $ MUTEX_TAG,M_TYPE /* set lock type */
- movl %eax,M_PC /* init caller pc */
- movl %eax,M_THREAD /* and owning thread */
-#endif
-
- LEAF_RET
-
-NONLEAF_ENTRY2(mutex_lock,_mutex_lock)
-
- movl B_ARG0,%edx /* fetch lock pointer */
-
- CHECK_MUTEX_TYPE()
- CHECK_NO_SIMPLELOCKS()
- CHECK_PREEMPTION_LEVEL()
-
- pushf /* save interrupt state */
- cli /* disable interrupts */
-
-ml_retry:
- movl B_PC,%ecx
-
-ml_get_hw:
- movl M_ILK,%eax /* read interlock */
- testl %eax,%eax /* unlocked? */
- je 1f /* yes - attempt to lock it */
- PAUSE /* no - pause */
- jmp ml_get_hw /* try again */
-1:
- lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
- jne ml_get_hw /* branch on failure to retry */
-
- movl M_LOCKED,%ecx /* get lock owner */
- testl %ecx,%ecx /* is the mutex locked? */
- jne ml_fail /* yes, we lose */
- movl %gs:CPU_ACTIVE_THREAD,%ecx
- movl %ecx,M_LOCKED
-
-#if MACH_LDEBUG
- movl %ecx,M_THREAD
- movl B_PC,%ecx
- movl %ecx,M_PC
-#endif
-
- pushl %edx /* save mutex address */
- pushl %edx
- call EXT(lck_mtx_lock_acquire)
- addl $4,%esp
- popl %edx /* restore mutex address */
-
- xorl %eax,%eax
- movl %eax,M_ILK
-
- popf /* restore interrupt state */
-
- NONLEAF_RET
-
-ml_fail:
-ml_block:
- CHECK_MYLOCK(M_THREAD)
- pushl M_LOCKED
- pushl %edx /* push mutex address */
- call EXT(lck_mtx_lock_wait) /* wait for the lock */
- addl $8,%esp
- movl B_ARG0,%edx /* refetch mutex address */
- jmp ml_retry /* and try again */
-
-NONLEAF_ENTRY2(mutex_try,_mutex_try)
-
- movl B_ARG0,%edx /* fetch lock pointer */
-
- CHECK_MUTEX_TYPE()
- CHECK_NO_SIMPLELOCKS()
-
- movl B_PC,%ecx
-
- pushf /* save interrupt state */
- cli /* disable interrupts */
-
-mt_get_hw:
- movl M_ILK,%eax /* read interlock */
- testl %eax,%eax /* unlocked? */
- je 1f /* yes - attempt to lock it */
- PAUSE /* no - pause */
- jmp mt_get_hw /* try again */
-1:
- lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
- jne mt_get_hw /* branch on failure to retry */
-
- movl M_LOCKED,%ecx /* get lock owner */
- testl %ecx,%ecx /* is the mutex locked? */
- jne mt_fail /* yes, we lose */
- movl %gs:CPU_ACTIVE_THREAD,%ecx
- movl %ecx,M_LOCKED
-
-#if MACH_LDEBUG
- movl %ecx,M_THREAD
- movl B_PC,%ecx
- movl %ecx,M_PC
-#endif
-
- pushl %edx /* save mutex address */
- pushl %edx
- call EXT(lck_mtx_lock_acquire)
- addl $4,%esp
- popl %edx /* restore mutex address */
-
- xorl %eax,%eax
- movl %eax,M_ILK
-
- popf /* restore interrupt state */
-
- movl $1,%eax
-
- NONLEAF_RET
-
-mt_fail:
- xorl %eax,%eax
- movl %eax,M_ILK
-
- popf /* restore interrupt state */
-
- xorl %eax,%eax
-
- NONLEAF_RET
-
-NONLEAF_ENTRY(mutex_unlock)
- movl B_ARG0,%edx /* fetch lock pointer */
-
- CHECK_MUTEX_TYPE()
- CHECK_THREAD(M_THREAD)
-
- movl B_PC,%ecx
-
- pushf /* save interrupt state */
- cli /* disable interrupts */
-
-mu_get_hw:
- movl M_ILK,%eax /* read interlock */
- testl %eax,%eax /* unlocked? */
- je 1f /* yes - attempt to lock it */
- PAUSE /* no - pause */
- jmp mu_get_hw /* try again */
-1:
- lock; cmpxchgl %ecx,M_ILK /* atomic compare and exchange */
- jne mu_get_hw /* branch on failure to retry */
-
- cmpw $0,M_WAITERS /* are there any waiters? */
- jne mu_wakeup /* yes, more work to do */
-
-mu_doit:
-
-#if MACH_LDEBUG
- movl $0,M_THREAD /* disown thread */
-#endif
-
- xorl %ecx,%ecx
- movl %ecx,M_LOCKED /* unlock the mutex */
-
- movl %ecx,M_ILK
-
- popf /* restore interrupt state */
-
- NONLEAF_RET
-
-mu_wakeup:
- pushl M_LOCKED
- pushl %edx /* push mutex address */
- call EXT(lck_mtx_unlock_wakeup)/* yes, wake a thread */
- addl $8,%esp
- movl B_ARG0,%edx /* restore lock pointer */
- jmp mu_doit