golang sudog指的是什么

 更新时间:2024年02月05日 14:29:59   作者:动态一时爽,重构火葬场  
sudog代表在等待队列中的goroutine,比如channel发送接受,由于goroutine和同步对象的关系是多对多,因此需要sudog映射,本文重点介绍golang sudog指的是什么,感兴趣的朋友一起看看吧

sudog代表在等待队列中的goroutine,比如channel发送接受。由于goroutine和同步对象的关系是多对多,因此需要sudog映射

type sudog struct {
  // 指向的goroutine
	g *g
  // 指向前后sudog的指针
	next *sudog
	prev *sudog
  // 指向数据
	elem unsafe.Pointer // data element (may point to stack)
	// The following fields are never accessed concurrently.
	// For channels, waitlink is only accessed by g.
	// For semaphores, all fields (including the ones above)
	// are only accessed when holding a semaRoot lock.
  // 获取时间
	acquiretime int64
  // 释放时间
	releasetime int64
  // 作为队列元素的标识
	ticket      uint32
	// isSelect indicates g is participating in a select, so
	// g.selectDone must be CAS'd to win the wake-up race.
	isSelect bool
	// success indicates whether communication over channel c
	// succeeded. It is true if the goroutine was awoken because a
	// value was delivered over channel c, and false if awoken
	// because c was closed.
	success bool
	parent   *sudog // semaRoot binary tree
	waitlink *sudog // g.waiting list or semaRoot
	waittail *sudog // semaRoot
	c        *hchan // channel
}

acquireSudog()

func acquireSudog() *sudog {
  // 增加m的锁,防止垃圾回收在此期间被调用
	mp := acquirem()
	pp := mp.p.ptr()
 	// 如果本地缓存为空
	if len(pp.sudogcache) == 0 {
		lock(&sched.sudoglock)
    // 从中心缓存迁移至多一半本地缓存容量的缓存项到本地缓存
		for len(pp.sudogcache) < cap(pp.sudogcache)/2 && sched.sudogcache != nil {
			s := sched.sudogcache
			sched.sudogcache = s.next
			s.next = nil
			pp.sudogcache = append(pp.sudogcache, s)
		}
		unlock(&sched.sudoglock)
    // 若本地缓存仍为空,则新建缓存项
		if len(pp.sudogcache) == 0 {
			pp.sudogcache = append(pp.sudogcache, new(sudog))
		}
	}
  // 从本地缓存中取出最后一个缓存项返回
	n := len(pp.sudogcache)
	s := pp.sudogcache[n-1]
	pp.sudogcache[n-1] = nil
	pp.sudogcache = pp.sudogcache[:n-1]
	if s.elem != nil {
		throw("acquireSudog: found s.elem != nil in cache")
	}
  // 减少m的锁,允许垃圾回收调用
	releasem(mp)
	return s
}

releaseSudog()

func releaseSudog(s *sudog) {
  // 判断sudog各项数据、状态是否正确
	if s.elem != nil {
		throw("runtime: sudog with non-nil elem")
	}
	if s.isSelect {
		throw("runtime: sudog with non-false isSelect")
	}
	if s.next != nil {
		throw("runtime: sudog with non-nil next")
	}
	if s.prev != nil {
		throw("runtime: sudog with non-nil prev")
	}
	if s.waitlink != nil {
		throw("runtime: sudog with non-nil waitlink")
	}
	if s.c != nil {
		throw("runtime: sudog with non-nil c")
	}
	gp := getg()
	if gp.param != nil {
		throw("runtime: releaseSudog with non-nil gp.param")
	}
	mp := acquirem() // avoid rescheduling to another P
	pp := mp.p.ptr()
  // 如果本地缓存满了,就迁移至多一半容量缓存项到中心缓存
	if len(pp.sudogcache) == cap(pp.sudogcache) {
		// Transfer half of local cache to the central cache.
		var first, last *sudog
		for len(pp.sudogcache) > cap(pp.sudogcache)/2 {
			n := len(pp.sudogcache)
			p := pp.sudogcache[n-1]
			pp.sudogcache[n-1] = nil
			pp.sudogcache = pp.sudogcache[:n-1]
			if first == nil {
				first = p
			} else {
				last.next = p
			}
			last = p
		}
		lock(&sched.sudoglock)
    // 将迁移出来的本地缓存链表直接挂到中心缓存中
		last.next = sched.sudogcache
		sched.sudogcache = first
		unlock(&sched.sudoglock)
	}
  // 将释放的sudog添加到本地缓存
	pp.sudogcache = append(pp.sudogcache, s)
	releasem(mp)
}

到此这篇关于golang sudog是什么?的文章就介绍到这了,更多相关golang sudog内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • go语言中Timer和Ticker两种计时器的使用

    go语言中Timer和Ticker两种计时器的使用

    go语言中有Timer和Ticker这样的两种计时器,两种计时器分别实现了不同的计时功能,本文主要介绍了go语言中Timer和Ticker两种计时器的使用,感兴趣的可以了解一下
    2024-08-08
  • Go语言MD5加密用法实例

    Go语言MD5加密用法实例

    这篇文章主要介绍了Go语言MD5加密用法,实例分析了Go语言MD5加密的使用技巧,需要的朋友可以参考下
    2015-03-03
  • 一文教你学会Go中singleflight的使用

    一文教你学会Go中singleflight的使用

    缓存在项目中使用应该是非常频繁的,提到缓存只要了解过 singleflight ,基本都会用于缓存实现的一部分吧,下面就跟随小编一起来学习一下singleflight的使用吧
    2024-02-02
  • Go语言中工作池的原理与实现

    Go语言中工作池的原理与实现

    工作池是一种并发编程模式,它使用一组固定数量的工作线程来执行任务队列中的工作单元,本文将介绍工作池的工作原理,并通过代码示例演示其在实际应用中的用途,有需要的可以参考下
    2023-10-10
  • 详解Go语言中的作用域和变量隐藏

    详解Go语言中的作用域和变量隐藏

    这篇文章主要为大家介绍了Go语言中的作用域和变量隐藏,文中的示例代码讲解详细,对我们学习Go语言有一定的帮助,感兴趣的小伙伴可以了解一下
    2022-04-04
  • Go语言字符串基础示例详解

    Go语言字符串基础示例详解

    这篇文章主要为大家介绍了Go语言字符串基础的示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2021-11-11
  • 深入探索Go语言中unsafe包的使用

    深入探索Go语言中unsafe包的使用

    Go语言的unsafe包被誉为黑科技,它为Go语言提供了底层访问和操控内存的能力,本文将深入探讨Go语言中unsafe包的使用方法和注意事项,需要的可以参考一下
    2023-04-04
  • golang grpc 负载均衡的方法

    golang grpc 负载均衡的方法

    这篇文章主要介绍了golang grpc 负载均衡的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • 详解Go语言中用 os/exec 执行命令的五种方法

    详解Go语言中用 os/exec 执行命令的五种方法

    这篇文章主要介绍了Go语言中用 os/exec 执行命令的五种方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • golang语言map全方位介绍

    golang语言map全方位介绍

    本文主要介绍了golang语言map全方位介绍,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01

最新评论