Skip to content

Commit

Permalink
remake project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
bootun committed Mar 30, 2023
1 parent e5b3db2 commit 8619509
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 5 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ Go剖析系列

## 目录
### 数据结构
- [切片剖析](./articles/slice.md)
- [数组剖析](./articles/array.md)
- [map剖析(写作中)](./articles/map.md)
- [切片剖析](./articles/container/slice.md)
- [数组剖析](./articles/container/array.md)
- [map剖析(写作中)](./articles/container/map.md)

### 并发
- [channel剖析(写作中)](./articles/channel.md)
- [channel剖析(写作中)](./articles/concurrent/channel.md)
<!-- - [select](./articles/select.md) -->
<!-- - [WaitGroup]() -->

Expand Down
File renamed without changes.
33 changes: 33 additions & 0 deletions articles/concurrent/mutex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Mutex fairness
```go
// Mutex fairness
//
// Mutex can be in 2 modes of operations: normal and starvation.
// In normal mode waiters are queued in FIFO order, but a woken up waiter
// does not own the mutex and competes with new arriving goroutines over
// the ownership. New arriving goroutines have an advantage -- they are
// already running on CPU and there can be lots of them, so a woken up
// waiter has good chances of losing. In such case it is queued at front
// of the wait queue. If a waiter fails to acquire the mutex for more than 1ms,
// it switches mutex to the starvation mode.
//
// In starvation mode ownership of the mutex is directly handed off from
// the unlocking goroutine to the waiter at the front of the queue.
// New arriving goroutines don't try to acquire the mutex even if it appears
// to be unlocked, and don't try to spin. Instead they queue themselves at
// the tail of the wait queue.
//
// If a waiter receives ownership of the mutex and sees that either
// (1) it is the last waiter in the queue, or (2) it waited for less than 1 ms,
// it switches mutex back to normal operation mode.
//
// Normal mode has considerably better performance as a goroutine can acquire
// a mutex several times in a row even if there are blocked waiters.
// Starvation mode is important to prevent pathological cases of tail latency.
```

## 基本流程
互斥锁的本质就是: 一堆人同时调用`Lock`方法, 只有一个人能顺利return,继续执行代码,也就是所谓的"拿到锁"。其他人会被阻塞在`Lock`方法里,直到有别人将它们唤醒。

在了解了大概的流程之后,我们再来看看它具体的实现:
TODO: 互斥锁很推荐《Go 并发编程实战课》的相关章节
2 changes: 2 additions & 0 deletions articles/concurrent/syncMap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sync.Map
===
18 changes: 18 additions & 0 deletions articles/concurrent/waitGroup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
sync.WaitGroup
===
## 使用
WaitGroup的注释已经很好的描述了它的用法:

> `WaitGroup`用来等待goroutine集合完成。主goroutine调用`Add`来设置等待的数量。然后每一个goroutine在运行完成时调用`Done``Wait`在所有goroutine都完成之前会一直阻塞。
## 内部实现
WaitGroup的实现比较简单,整个`waitgroup.go`带上注释也只有100多行。
WaitGroup结构体的定义如下:
```go
type WaitGroup struct {
noCopy noCopy

state atomic.Uint64 // high 32 bits are counter, low 32 bits are waiter count.
sema uint32
}
```
File renamed without changes.
File renamed without changes
10 changes: 9 additions & 1 deletion articles/map.md → articles/container/map.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ func maplit(n *ir.CompLitExpr, m ir.Node, init *ir.Nodes) {

// Build list of var[c] = expr.
}
```
```

## 运行时的map

### 数据结构
### 访问
### 赋值
### 删除
### 扩容
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit 8619509

Please sign in to comment.