Golang源码学习笔记
文章平均质量分 51
个人 Golang 源码学习笔记
仟玑
这个作者很懒,什么都没留下…
展开
-
013-Golang1.17源码分析之Context
Golang1.17源码分析之ContextGolang1.17 学习笔记013context 是一种常用额并发控制技术,与 WaitGroup 最大的区别就是 context 对派生的 G 也有控制权。可以控制多级的 goroutinecontext 译为“上下文”,可以控制一组成树状结构的 goroutine,每个 goroutine 都有相同的 context13.1 实现源码位置:src/context/context.go:Contexttype Context interfac原创 2022-05-13 15:43:30 · 175 阅读 · 0 评论 -
012-Golang1.17源码分析之WaitGroup
Golang1.17源码分析之WaitGroupGolang1.17 学习笔记010WaitGroup,可理解为 Wait-Goroutine-Group,即等待一组 goroutine 结束源码包:sync/waitgroup.gotype WaitGroup struct { noCopy noCopy // 64-bit value: high 32 bits are counter, low 32 bits are waiter count. // 64-bit atomic o原创 2022-04-08 14:54:35 · 196 阅读 · 0 评论 -
011-Golang1.17源码分析之GC
Golang1.17源码分析之GC-011Golang1.17 学习笔记011GC 目前采用的是三色标记清除法三色:黑、灰、白黑色:被引用,不清理,追踪完成,gcmarkBits 对应的位为 1灰色:可能被引用,需要再次扫描,未追踪完成白色:没有被引用,或尚未追踪,gcmarkBits 对应的位为 011.1 内存标记span 中维护了一个个内存块,并由一个位图 allocBits 表示每个内存块的分配情况。在 span 数据结构中还有另一个位图 gcmarkBits 用于标记内存块被原创 2022-04-08 14:52:33 · 352 阅读 · 0 评论 -
010-Golang1.17源码分析之内存管理
Golang1.17源码分析之内存管理-010Golang1.17 学习笔记010为了方便自主管理内存,做法便是先向系统申请一块内存,然后将内存切割成小块,通过一定的内存分配算法管理内存预申请的内存划分为 spans、bitmap、arena 三部分。其中 arena 即为所谓的堆区,应用中需要的内存从这里分配spans 和 bitmap 是为了管理 arena 存在的arena 为了方便管理划分成一个一个的 pagespans 区域存放 span 的指针,每个指针对应一个 page,大小由原创 2022-03-03 21:00:00 · 668 阅读 · 0 评论 -
009-Golang1.17源码分析之goroutine
Golang1.17源码分析之goroutine-009Golang1.17 学习笔记009包位置:runtime/runtime2.go全局变量 g0 主协程、m0 工作线程P 里面只是有个本地 runq,全局的 runq 存储在 sched 中获取任务顺序,先从 m 自身 p 中的 runq 获取,没有就去全局 sched 中获取,没有再去其他 q 中拿一点type g struct { _panic *_panic // innermost panic - offset kno原创 2022-02-22 19:00:00 · 422 阅读 · 0 评论 -
008-Golang1.17源码分析之rwmutex
Golang1.17源码分析之rwmutex-009Golang1.17 学习笔记009解决的问题:写锁需要阻塞写锁:一个协程拥有写锁时,其他协程写锁定需要阻塞写锁需要阻塞读锁:一个协程拥有写锁时,其他协程读锁定需要阻塞读锁需要阻塞写锁:一个协程拥有读锁时,其他协程写锁定需要阻塞读锁不能阻塞读锁:一个协程拥有读锁时,其他协程也可以拥有读锁源代码:src/sync/rwmutex.go数据结构:type RWMutex struct { w Mutex原创 2022-02-22 18:45:00 · 172 阅读 · 0 评论 -
007-Golang1.17源码分析之mutex
Golang1.17源码分析之mutex-007Golang1.17 学习笔记007源代码:sync/mutex.go数据结构:const ( // 锁标识位(state的最后一位) // Mutex.state & mutexLocked==1表示已经上锁;Mutex.state & mutexLocked==0表示已经未锁 mutexLocked = 1 << iota // mutex is locked // 是否存在运行中的协程(原创 2022-02-21 20:00:00 · 172 阅读 · 0 评论 -
006-Golang1.17源码分析之select
Golang1.17源码分析之select-006Golang1.17 学习笔记006源码地址:runtime/select.go数据结构:type scase struct { c *hchan // chan elem unsafe.Pointer // data element}核心函数:selectgo()order0 为一个两倍 cas0 数组长度的 buffer,保存 scase 随机序列 pollorder 和 scase 中 channel 地址原创 2022-02-21 19:00:00 · 152 阅读 · 0 评论 -
005-Golang1.17源码分析之string
Golang1.17源码分析之string-005Golang1.17 学习笔记005源码包:src/runtime/string.go源代码:type stringStruct struct { str unsafe.Pointer len int}注意的就是:byte 和 string 互转是需要重新开辟空间的string 被分配到只读内存中utf-8 编码:变长形式,根据 unicode 值进行编码规则:1-127 占用一个字节,用 0 开头, 0xxxxxx原创 2022-02-19 10:45:00 · 256 阅读 · 0 评论 -
004-Golang1.17源码分析之struct
Golang1.17源码分析之struct-004Golang1.17 学习笔记004源代码地址:reflect/type.go数据结构:// A StructField describes a single field in a struct.type StructField struct { // Name is the field name. Name string // PkgPath is the package path that qualifies a lower cas原创 2022-02-18 19:00:00 · 303 阅读 · 0 评论 -
003-Golang1.17源码分析之map
Golang1.17源码分析之map-003Golang1.17 学习笔记003源码包:runtime/map.go数据结构:// A header for a Go map.type hmap struct { // Note: the format of the hmap is also encoded in cmd/compile/internal/reflectdata/reflect.go. // Make sure this stays in sync with the com原创 2022-02-16 14:31:56 · 279 阅读 · 0 评论 -
002-Golang1.17源码分析之channel
Golang1.17源码分析之channel-002源码包:runtime/chan.go数据结构:type hchan struct { qcount uint // 总容量 dataqsiz uint // 当前数据大小 buf unsafe.Pointer // 环形队列指针,指向 dataqsiz elemsize uint16 // 每个元素的大小 closed uint32原创 2022-02-16 14:09:31 · 9570 阅读 · 0 评论 -
001-Golang1.17源码分析之slice
Golang1.17源码分析之sliceGolang1.17 学习笔记001一、slice源码包:runtime/slice.go数据结构:type slice struct { array unsafe.Pointer // 指向底层数组的指针 len int // 长度 cap int // 容量}创建:这里创建规则跟 go 的内存管理有关,建议看完内存管理之后再看这里小于 32kb 的对象从 per-p 缓存空闲列表中创建原创 2022-02-11 10:48:58 · 489 阅读 · 0 评论