tiny对象分配后,对象所在G被抢占了会发生什么?

来源:1-13 Go 语言的内存管理与垃圾回收

快哉快哉

2021-07-01 20:25:51

http://img.mukewang.com/climg/60ddb40a09cdaeff10070562.jpg


假设 G4分配tiny 对象后,被抢占到了另一个M,那么到了另一个M,可以调用之前分配的地址吗?还是重新分配?

写回答

2回答

Xargin

2021-07-02

mallocgc 期间会置:

mp.mallocing = 1

在抢占信号处理的时候,会判断这个值,

func doSigPreempt(gp *g, ctxt *sigctxt) {
   // Check if this G wants to be preempted and is safe to
   // preempt.
   if wantAsyncPreempt(gp) && isAsyncSafePoint(gp, ctxt.sigpc(), ctxt.sigsp(), ctxt.siglr()) {
      // Inject a call to asyncPreempt.
      ctxt.pushCall(funcPC(asyncPreempt))
   }

   // Acknowledge the preemption.
   atomic.Xadd(&gp.m.preemptGen, 1)
   atomic.Store(&gp.m.signalPending, 0)
}

func isAsyncSafePoint(gp *g, pc, sp, lr uintptr) bool {
   mp := gp.m

   // Check M state.
   if mp.p == 0 || !canPreemptM(mp) {
      return false
   }
   .....  
}

// 注意这里面的 mp.mallocing
func canPreemptM(mp *m) bool {
   return mp.locks == 0 && mp.mallocing == 0 && mp.preemptoff == "" && mp.p.ptr().status == _Prunning
}


所以 malloc 完成之前,不可能被抢占。


malloc 完成之后,地址已经被赋予给对象了,这时候再去其它的 M 没啥关系

1

快哉快哉

提问者

2021-07-05

我也如此认为,同事还不信?。
多谢曹大?

0

Go高级工程师实战营

慕课网与 GoCN 社区官方联手打造,定义行业Go高级人才培养标准,4个月,快速晋升为P6+/D7级高级人才。

458 学习 · 266 问题

查看课程