Skip to content

Commit

Permalink
kernel: optimize futex with timeout (icexin#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
icexin authored Aug 11, 2021
1 parent 2921eb8 commit 971efad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions kernel/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ func sleeptimeout(addr *uintptr, val uintptr, ts *linux.Timespec) {
deadline := nanosecond() + int64(ts.Nsec) + int64(ts.Sec)*second
// check on every timer intr
now := nanosecond()
t := Mythread()
for now < deadline && *addr == val {
sleepon(&sleeplock)
t.timerKey = uintptr(unsafe.Pointer(&sleeplock))
t.sleepKey = uintptr(unsafe.Pointer(addr))
t.state = SLEEPING
Sched()
t.timerKey = 0
t.sleepKey = 0
now = nanosecond()
}
}
Expand All @@ -61,9 +67,10 @@ func sleepon(lock *uintptr) {
func wakeup(lock *uintptr, n int) {
limit := uint(n)
cnt := uint(0)
lockKey := uintptr(unsafe.Pointer(lock))
for i := 0; i < _NTHREDS; i++ {
t := &threads[i]
if t.sleepKey == uintptr(unsafe.Pointer(lock)) && cnt < limit {
if (t.sleepKey == lockKey || t.timerKey == lockKey) && cnt < limit {
cnt++
t.state = RUNNABLE
}
Expand Down
2 changes: 2 additions & 0 deletions kernel/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type Thread struct {
// sysmon 会调用usleep,进而调用sleepon,如果sleepKey是个指针会触发gcWriteBarrier
// 而sysmon没有P,会导致空指针
sleepKey uintptr
// for sleep timeout
timerKey uintptr

// store goroutine tls
fsBase uintptr
Expand Down

0 comments on commit 971efad

Please sign in to comment.