File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -115,6 +115,12 @@ treap 结构:
115115└───────────────────────┘ └───────────────────────┘
116116```
117117
118+ 在这个 treap 结构里,从 elem 的视角(其实就是 lock 的 addr)来看,这个结构是个二叉搜索树。从 ticket 的角度来看,整个结构就是一个小顶堆。
119+
120+ 所以才叫树堆(treap)。
121+
122+ 相同 addr,即对同一个 mutex 上锁的 g,会阻塞在同一个地址上。这些阻塞在同一个地址上的 goroutine 会被打包成 sudog,组成一个链表。用 sudog 的 waitlink 相连:
123+
118124```
119125┌──────────┐ ┌──────────┐ ┌──────────┐
120126│ sudog │ ┌─────▶│ sudog │ ┌─────▶│ sudog │
@@ -125,6 +131,8 @@ treap 结构:
125131└───────────────────────┘ └───────────────────────┘ └───────────────────────┘
126132```
127133
134+ 中间的元素的 waittail 都会指向最后一个元素:
135+
128136```
129137┌──────────┐
130138│ sudog │
@@ -433,7 +441,7 @@ Found:
433441 now = cputicks ()
434442 }
435443 if t := s.waitlink ; t != nil {
436- // Substitute t, also waiting on addr, for s in root tree of unique addrs.
444+ // 替换掉同样在 addr 上等待的 t。
437445 *ps = t
438446 t.ticket = s.ticket
439447 t.parent = s.parent
@@ -454,7 +462,7 @@ Found:
454462 s.waitlink = nil
455463 s.waittail = nil
456464 } else {
457- // Rotate s down to be leaf of tree for removal, respecting priorities.
465+ // 向下旋转 s 到叶节点,以进行删除,同时要考虑优先级
458466 for s.next != nil || s.prev != nil {
459467 if s.next == nil || s.prev != nil && s.prev .ticket < s.next .ticket {
460468 root.rotateRight (s)
@@ -463,6 +471,7 @@ Found:
463471 }
464472 }
465473 // Remove s, now a leaf.
474+ // 删除 s,现在是叶子节点了
466475 if s.parent != nil {
467476 if s.parent .prev == s {
468477 s.parent .prev = nil
You can’t perform that action at this time.
0 commit comments