golang defer实现

derfer : 延迟调用,函数结束返回时执行,多个defer按照先进后出的顺序调用

原理:底层通过链表实现,每次新增的defer调用,通过头插法插入链表;defer执行时,从链表头开始遍历,相当于实现了后加入的defer先执行,先加的defer后执行

defer结构体

type _defer struct {
	started bool
	heap    bool
	// openDefer indicates that this _defer is for a frame with open-coded
	// defers. We have only one defer record for the entire frame (which may
	// currently have 0, 1, or more defers active).
	openDefer bool
	sp        uintptr // sp at time of defer
	pc        uintptr // pc at time of defer
	fn        func()  // can be nil for open-coded defers
	_panic    *_panic // panic that is running defer
	link      *_defer // next defer on G; can point to either heap or stack!

	// If openDefer is true, the fields below record values about the stack
	// frame and associated function that has the open-coded defer(s). sp
	// above will be the sp for the frame, and pc will be address of the
	// deferreturn call in the function.
	fd   unsafe.Pointer // funcdata for the function associated with the frame
	varp uintptr        // value of varp for the stack frame
	// framepc is the current pc associated with the stack frame. Together,
	// with sp above (which is the sp associated with the stack frame),
	// framepc/sp can be used as pc/sp pair to continue a stack trace via
	// gentraceback().
	framepc uintptr
}

defer初始化

// Create a new deferred function fn, which has no arguments and results.
// The compiler turns a defer statement into a call to this.
func deferproc(fn func()) {
	gp := getg()
	if gp.m.curg != gp {
		// go code on the system stack can't defer
		throw("defer on system stack")
	}

	d := newdefer()
	if d._panic != nil {
		throw("deferproc: d.panic != nil after newdefer")
	}
    
    // 这里使用头插法 插入链表
	d.link = gp._defer
	gp._defer = d
    
	d.fn = fn
	d.pc = getcallerpc()
	// We must not be preempted between calling getcallersp and
	// storing it to d.sp because getcallersp's result is a
	// uintptr stack pointer.
	d.sp = getcallersp()

	// deferproc returns 0 normally.
	// a deferred func that stops a panic
	// makes the deferproc return 1.
	// the code the compiler generates always
	// checks the return value and jumps to the
	// end of the function if deferproc returns != 0.
	return0()
	// No code can go here - the C return register has
	// been set and must not be clobbered.
}

defer执行

func deferreturn() {
	gp := getg()
	for {
		d := gp._defer
		if d == nil {
			return
		}
		sp := getcallersp()
		if d.sp != sp {
			return
		}
		if d.openDefer {
			done := runOpenDeferFrame(gp, d)
			if !done {
				throw("unfinished open-coded defers in deferreturn")
			}
			gp._defer = d.link
			freedefer(d)
			// If this frame uses open defers, then this
			// must be the only defer record for the
			// frame, so we can just return.
			return
		}

		fn := d.fn
		d.fn = nil
        
        // 指向下一个defer节点
		gp._defer = d.link
		freedefer(d)
		fn()
	}
}

相关推荐

  1. Spring WebSocket实现实时通信

    2024-04-06 06:08:13       56 阅读
  2. rsync+inotify实现实时同步

    2024-04-06 06:08:13       31 阅读
  3. 通讯录实现

    2024-04-06 06:08:13       63 阅读
  4. Promise实现

    2024-04-06 06:08:13       43 阅读
  5. SpringBoot集成etcd,实现实时监听,实现配置中心

    2024-04-06 06:08:13       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-06 06:08:13       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-06 06:08:13       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-06 06:08:13       87 阅读
  4. Python语言-面向对象

    2024-04-06 06:08:13       96 阅读

热门阅读

  1. 006 CSS常见选择器 CSS伪类 CSS伪元素

    2024-04-06 06:08:13       37 阅读
  2. 《观察者模式(极简c++)》

    2024-04-06 06:08:13       32 阅读
  3. 逻辑回归(Logistic Regression)详解

    2024-04-06 06:08:13       34 阅读
  4. 课时86:流程控制_函数基础_函数退出

    2024-04-06 06:08:13       40 阅读
  5. 提升写作效率:ChatGPT助力学术论文撰写

    2024-04-06 06:08:13       36 阅读
  6. 详解Qt中的容器

    2024-04-06 06:08:13       35 阅读
  7. Qt基本控件

    2024-04-06 06:08:13       30 阅读
  8. centos 7 MySQL 离线一键自动安装脚本

    2024-04-06 06:08:13       35 阅读
  9. 空和null是两回事

    2024-04-06 06:08:13       34 阅读
  10. linux模糊删除文件命令

    2024-04-06 06:08:13       37 阅读