欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

golang sudog指的是什么

 更新時(shí)間:2024年02月05日 14:29:59   作者:動(dòng)態(tài)一時(shí)爽,重構(gòu)火葬場(chǎng)  
sudog代表在等待隊(duì)列中的goroutine,比如channel發(fā)送接受,由于goroutine和同步對(duì)象的關(guān)系是多對(duì)多,因此需要sudog映射,本文重點(diǎn)介紹golang sudog指的是什么,感興趣的朋友一起看看吧

sudog代表在等待隊(duì)列中的goroutine,比如channel發(fā)送接受。由于goroutine和同步對(duì)象的關(guān)系是多對(duì)多,因此需要sudog映射

type sudog struct {
  // 指向的goroutine
	g *g
  // 指向前后sudog的指針
	next *sudog
	prev *sudog
  // 指向數(shù)據(jù)
	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.
  // 獲取時(shí)間
	acquiretime int64
  // 釋放時(shí)間
	releasetime int64
  // 作為隊(duì)列元素的標(biāo)識(shí)
	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的鎖,防止垃圾回收在此期間被調(diào)用
	mp := acquirem()
	pp := mp.p.ptr()
 	// 如果本地緩存為空
	if len(pp.sudogcache) == 0 {
		lock(&sched.sudoglock)
    // 從中心緩存遷移至多一半本地緩存容量的緩存項(xiàng)到本地緩存
		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)
    // 若本地緩存仍為空,則新建緩存項(xiàng)
		if len(pp.sudogcache) == 0 {
			pp.sudogcache = append(pp.sudogcache, new(sudog))
		}
	}
  // 從本地緩存中取出最后一個(gè)緩存項(xiàng)返回
	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的鎖,允許垃圾回收調(diào)用
	releasem(mp)
	return s
}

releaseSudog()

func releaseSudog(s *sudog) {
  // 判斷sudog各項(xiàng)數(shù)據(jù)、狀態(tài)是否正確
	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()
  // 如果本地緩存滿了,就遷移至多一半容量緩存項(xiàng)到中心緩存
	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)
}

到此這篇關(guān)于golang sudog是什么?的文章就介紹到這了,更多相關(guān)golang sudog內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • go語(yǔ)言中Timer和Ticker兩種計(jì)時(shí)器的使用

    go語(yǔ)言中Timer和Ticker兩種計(jì)時(shí)器的使用

    go語(yǔ)言中有Timer和Ticker這樣的兩種計(jì)時(shí)器,兩種計(jì)時(shí)器分別實(shí)現(xiàn)了不同的計(jì)時(shí)功能,本文主要介紹了go語(yǔ)言中Timer和Ticker兩種計(jì)時(shí)器的使用,感興趣的可以了解一下
    2024-08-08
  • Go語(yǔ)言MD5加密用法實(shí)例

    Go語(yǔ)言MD5加密用法實(shí)例

    這篇文章主要介紹了Go語(yǔ)言MD5加密用法,實(shí)例分析了Go語(yǔ)言MD5加密的使用技巧,需要的朋友可以參考下
    2015-03-03
  • 一文教你學(xué)會(huì)Go中singleflight的使用

    一文教你學(xué)會(huì)Go中singleflight的使用

    緩存在項(xiàng)目中使用應(yīng)該是非常頻繁的,提到緩存只要了解過?singleflight?,基本都會(huì)用于緩存實(shí)現(xiàn)的一部分吧,下面就跟隨小編一起來學(xué)習(xí)一下singleflight的使用吧
    2024-02-02
  • Go語(yǔ)言中工作池的原理與實(shí)現(xiàn)

    Go語(yǔ)言中工作池的原理與實(shí)現(xiàn)

    工作池是一種并發(fā)編程模式,它使用一組固定數(shù)量的工作線程來執(zhí)行任務(wù)隊(duì)列中的工作單元,本文將介紹工作池的工作原理,并通過代碼示例演示其在實(shí)際應(yīng)用中的用途,有需要的可以參考下
    2023-10-10
  • 詳解Go語(yǔ)言中的作用域和變量隱藏

    詳解Go語(yǔ)言中的作用域和變量隱藏

    這篇文章主要為大家介紹了Go語(yǔ)言中的作用域和變量隱藏,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Go語(yǔ)言有一定的幫助,感興趣的小伙伴可以了解一下
    2022-04-04
  • Go語(yǔ)言字符串基礎(chǔ)示例詳解

    Go語(yǔ)言字符串基礎(chǔ)示例詳解

    這篇文章主要為大家介紹了Go語(yǔ)言字符串基礎(chǔ)的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2021-11-11
  • 深入探索Go語(yǔ)言中unsafe包的使用

    深入探索Go語(yǔ)言中unsafe包的使用

    Go語(yǔ)言的unsafe包被譽(yù)為黑科技,它為Go語(yǔ)言提供了底層訪問和操控內(nèi)存的能力,本文將深入探討Go語(yǔ)言中unsafe包的使用方法和注意事項(xiàng),需要的可以參考一下
    2023-04-04
  • golang grpc 負(fù)載均衡的方法

    golang grpc 負(fù)載均衡的方法

    這篇文章主要介紹了golang grpc 負(fù)載均衡的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • 詳解Go語(yǔ)言中用 os/exec 執(zhí)行命令的五種方法

    詳解Go語(yǔ)言中用 os/exec 執(zhí)行命令的五種方法

    這篇文章主要介紹了Go語(yǔ)言中用 os/exec 執(zhí)行命令的五種方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • golang語(yǔ)言map全方位介紹

    golang語(yǔ)言map全方位介紹

    本文主要介紹了golang語(yǔ)言map全方位介紹,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01

最新評(píng)論