Skip to content

Commit dee5fb5

Browse files
committed
sema basically done
1 parent 5fe50c5 commit dee5fb5

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@
2222
16. [x] [Map](map.md)
2323
17. [x] [Syscall](syscall.md)
2424
18. [x] [Memory](memory.md)
25-
19. [ ] [Semaphore](semaphore.md)
25+
19. [x] [Semaphore](semaphore.md)

semaphore.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -278,22 +278,21 @@ func (root *semaRoot) queue(addr *uint32, s *sudog, lifo bool) {
278278
}
279279
}
280280

281-
// Add s as new leaf in tree of unique addrs.
282-
// The balanced tree is a treap using ticket as the random heap priority.
283-
// That is, it is a binary tree ordered according to the elem addresses,
284-
// but then among the space of possible binary trees respecting those
285-
// addresses, it is kept balanced on average by maintaining a heap ordering
286-
// on the ticket: s.ticket <= both s.prev.ticket and s.next.ticket.
281+
// 把 s 作为树的新的叶子插入进去
282+
// 平衡树使用 ticket 作为堆的权重值,这个 ticket 是随机生成的
283+
// 也就是说,这个结构以元素地址来看的话,是一个二叉搜索树
284+
// 同时用 ticket 值使其同时又是一个小顶堆,满足
285+
// s.ticket <= both s.prev.ticket and s.next.ticket.
287286
// https://en.wikipedia.org/wiki/Treap
288287
// http://faculty.washington.edu/aragon/pubs/rst89.pdf
289288
//
290-
// s.ticket compared with zero in couple of places, therefore set lowest bit.
291-
// It will not affect treap's quality noticeably.
289+
// s.ticket 会在一些地方和 0 相比,因此只设置最低位的 bit
290+
// 这样不会明显地影响 treap 的质量?
292291
s.ticket = fastrand() | 1
293292
s.parent = last
294293
*pt = s
295294

296-
// Rotate up into tree according to ticket (priority).
295+
// 按照 ticket 来进行旋转,以满足 treap 的性质
297296
for s.parent != nil && s.parent.ticket > s.ticket {
298297
if s.parent.prev == s {
299298
root.rotateRight(s.parent)
@@ -306,10 +305,8 @@ func (root *semaRoot) queue(addr *uint32, s *sudog, lifo bool) {
306305
}
307306
}
308307

309-
// dequeue searches for and finds the first goroutine
310-
// in semaRoot blocked on addr.
311-
// If the sudog was being profiled, dequeue returns the time
312-
// at which it was woken up as now. Otherwise now is 0.
308+
// dequeue 会搜索到阻塞在 addr 地址的 semaRoot 中的第一个 goroutine
309+
// 如果这个 sudog 需要进行 profile,dequeue 会返回它被唤醒的时间(now),否则的话 now 为 0
313310
func (root *semaRoot) dequeue(addr *uint32) (found *sudog, now int64) {
314311
ps := &root.treap
315312
s := *ps

0 commit comments

Comments
 (0)