当前位置: 首页>>代码示例>>Golang>>正文


Golang runtime.SetFinalizer函数代码示例

本文整理汇总了Golang中runtime.SetFinalizer函数的典型用法代码示例。如果您正苦于以下问题:Golang SetFinalizer函数的具体用法?Golang SetFinalizer怎么用?Golang SetFinalizer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了SetFinalizer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestProtectFreeNestPtr

func TestProtectFreeNestPtr(t *testing.T) {
	g_t = t

	tlist := New(TestNestData{}, 50000)

	createData(tlist, func(e *Element, i int) {
		v := e.Value().(*TestNestData)
		v.a = i
		v.b = TestDataPtr{a: i, b: &TestData{a: int64(i + 1)}}
		runtime.SetFinalizer(v.b.b, on_gc)
		e.Commit()
	})
	//	fmt.Println(tlist.Front().DumpPicks())

	runtime.GC()

	for e := tlist.Front(); e != nil; e = e.Next() {
		v := e.Value().(*TestNestData)

		runtime.SetFinalizer(v.b.b, nil)

		if !reflect.ValueOf(v.b.b).IsValid() {
			t.Error("FreeNestPtr: data is freed")
		}
	}
}
开发者ID:kazu,项目名称:buffer_list,代码行数:26,代码来源:buffer_list_test.go

示例2: BenchmarkFinalizer

func BenchmarkFinalizer(b *testing.B) {
	const CallsPerSched = 1000
	procs := runtime.GOMAXPROCS(-1)
	N := int32(b.N / CallsPerSched)
	var wg sync.WaitGroup
	wg.Add(procs)
	for p := 0; p < procs; p++ {
		go func() {
			var data [CallsPerSched]*int
			for i := 0; i < CallsPerSched; i++ {
				data[i] = new(int)
			}
			for atomic.AddInt32(&N, -1) >= 0 {
				runtime.Gosched()
				for i := 0; i < CallsPerSched; i++ {
					runtime.SetFinalizer(data[i], fin)
				}
				for i := 0; i < CallsPerSched; i++ {
					runtime.SetFinalizer(data[i], nil)
				}
			}
			wg.Done()
		}()
	}
	wg.Wait()
}
开发者ID:ds2dev,项目名称:gcc,代码行数:26,代码来源:mfinal_test.go

示例3: allocObject

func allocObject(cobj *C.git_object) Object {

	switch ObjectType(C.git_object_type(cobj)) {
	case ObjectCommit:
		commit := &Commit{
			gitObject: gitObject{cobj},
			cast_ptr:  (*C.git_commit)(cobj),
		}
		runtime.SetFinalizer(commit, (*Commit).Free)
		return commit

	case ObjectTree:
		tree := &Tree{
			gitObject: gitObject{cobj},
			cast_ptr:  (*C.git_tree)(cobj),
		}
		runtime.SetFinalizer(tree, (*Tree).Free)
		return tree

	case ObjectBlob:
		blob := &Blob{
			gitObject: gitObject{cobj},
			cast_ptr:  (*C.git_blob)(cobj),
		}
		runtime.SetFinalizer(blob, (*Blob).Free)
		return blob
	}

	return nil
}
开发者ID:jingweno,项目名称:git2go,代码行数:30,代码来源:object.go

示例4: newAsyncWaiter

// newAsyncWaiter creates an asyncWaiterImpl and starts its worker goroutine.
func newAsyncWaiter() *asyncWaiterImpl {
	result, h0, h1 := system.GetCore().CreateMessagePipe(nil)
	if result != system.MOJO_RESULT_OK {
		panic(fmt.Sprintf("can't create message pipe %v", result))
	}
	waitChan := make(chan waitRequest, 10)
	cancelChan := make(chan AsyncWaitId, 10)
	isNotified := new(int32)
	worker := &asyncWaiterWorker{
		[]system.Handle{h1},
		[]system.MojoHandleSignals{system.MOJO_HANDLE_SIGNAL_READABLE},
		[]AsyncWaitId{0},
		[]chan<- WaitResponse{make(chan WaitResponse)},
		isNotified,
		waitChan,
		cancelChan,
		0,
	}
	runtime.SetFinalizer(worker, finalizeWorker)
	go worker.runLoop()
	waiter := &asyncWaiterImpl{
		wakingHandle:     h0,
		isWorkerNotified: isNotified,
		waitChan:         waitChan,
		cancelChan:       cancelChan,
	}
	runtime.SetFinalizer(waiter, finalizeAsyncWaiter)
	return waiter
}
开发者ID:smishenk,项目名称:chromium-crosswalk,代码行数:30,代码来源:async_waiter.go

示例5: CreateContextFromTypeUnsafe

func CreateContextFromTypeUnsafe(properties *C.cl_context_properties, device_type C.cl_device_type, pfn_notify CL_ctx_notify, user_data unsafe.Pointer) (*Context, error) {
	var err C.cl_int
	var clContext C.cl_context
	if pfn_notify != nil {
		var c_user_data []unsafe.Pointer
		c_user_data = make([]unsafe.Pointer, 2)
		c_user_data[0] = user_data
		c_user_data[1] = unsafe.Pointer(&pfn_notify)

		ctx_notify[c_user_data[1]] = pfn_notify

		clContext = C.CLCreateContextFromType(properties, device_type, unsafe.Pointer(&c_user_data), &err)
	} else {
		clContext = C.clCreateContextFromType(properties, device_type, nil, nil, &err)
	}
	if err != C.CL_SUCCESS {
		return nil, toError(err)
	}
	if clContext == nil {
		return nil, ErrUnknown
	}
	contextTmp := &Context{clContext: clContext, devices: nil}
	cDevices, errD := contextTmp.GetDevices()
	if errD != nil {
		runtime.SetFinalizer(contextTmp, releaseContext)
		return contextTmp, toError(err)
	}
	context := &Context{clContext: clContext, devices: cDevices}
	runtime.SetFinalizer(context, releaseContext)
	return context, nil
}
开发者ID:xfong,项目名称:tmpCL,代码行数:31,代码来源:context.go

示例6: remoteCreateCallback

//export remoteCreateCallback
func remoteCreateCallback(cremote unsafe.Pointer, crepo unsafe.Pointer, cname, curl *C.char, payload unsafe.Pointer) C.int {
	name := C.GoString(cname)
	url := C.GoString(curl)
	repo := newRepositoryFromC((*C.git_repository)(crepo))
	// We don't own this repository, so make sure we don't try to free it
	runtime.SetFinalizer(repo, nil)

	if opts, ok := pointerHandles.Get(payload).(CloneOptions); ok {
		remote, err := opts.RemoteCreateCallback(repo, name, url)
		// clear finalizer as the calling C function will
		// free the remote itself
		runtime.SetFinalizer(remote, nil)

		if err == ErrOk && remote != nil {
			cptr := (**C.git_remote)(cremote)
			*cptr = remote.ptr
		} else if err == ErrOk && remote == nil {
			panic("no remote created by callback")
		}

		return C.int(err)
	} else {
		panic("invalid remote create callback")
	}
}
开发者ID:wid,项目名称:git2go,代码行数:26,代码来源:clone.go

示例7: Pipe

// Pipe creates a synchronous in-memory pipe.
// It can be used to connect code expecting an io.Reader
// with code expecting an io.Writer.
// Reads on one end are matched with writes on the other,
// copying data directly between the two; there is no internal buffering.
func Pipe() (*PipeReader, *PipeWriter) {
	p := &pipe{
		r1:     make(chan []byte),
		r2:     make(chan pipeResult),
		w1:     make(chan []byte),
		w2:     make(chan pipeResult),
		rclose: make(chan os.Error),
		wclose: make(chan os.Error),
		done:   make(chan int),
	}
	go p.run()

	// NOTE: Cannot use composite literal here:
	//	pipeHalf{c1: p.cr1, c2: p.cr2, cclose: p.crclose, cdone: p.cdone}
	// because this implicitly copies the pipeHalf, which copies the inner mutex.

	r := new(PipeReader)
	r.c1 = p.r1
	r.c2 = p.r2
	r.cclose = p.rclose
	r.done = p.done
	runtime.SetFinalizer(r, (*PipeReader).finalizer)

	w := new(PipeWriter)
	w.c1 = p.w1
	w.c2 = p.w2
	w.cclose = p.wclose
	w.done = p.done
	runtime.SetFinalizer(w, (*PipeWriter).finalizer)

	return r, w
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:37,代码来源:pipe.go

示例8: TestWriteHeapDumpFinalizers

func TestWriteHeapDumpFinalizers(t *testing.T) {
	if runtime.GOOS == "nacl" {
		t.Skip("WriteHeapDump is not available on NaCl.")
	}
	f, err := ioutil.TempFile("", "heapdumptest")
	if err != nil {
		t.Fatalf("TempFile failed: %v", err)
	}
	defer os.Remove(f.Name())
	defer f.Close()

	// bug 9172: WriteHeapDump couldn't handle more than one finalizer
	println("allocating objects")
	x := &Obj{}
	runtime.SetFinalizer(x, objfin)
	y := &Obj{}
	runtime.SetFinalizer(y, objfin)

	// Trigger collection of x and y, queueing of their finalizers.
	println("starting gc")
	runtime.GC()

	// Make sure WriteHeapDump doesn't fail with multiple queued finalizers.
	println("starting dump")
	WriteHeapDump(f.Fd())
	println("done dump")
}
开发者ID:Xiahl1990,项目名称:go,代码行数:27,代码来源:heapdump_test.go

示例9: SetAutoGC

func (d *_qt_drv) SetAutoGC(b bool) {
	d.gc = b
	if d.gc {
		runtime.SetFinalizer(d, (*_qt_drv).Delete)
	} else {
		runtime.SetFinalizer(d, nil)
	}
}
开发者ID:rahul-goqt,项目名称:goqt,代码行数:8,代码来源:cdrv.go

示例10: Allocate

func Allocate() {
	p := new(P)
	*p = new(P)
	**p = new(P)
	runtime.SetFinalizer(p, Print)
	runtime.SetFinalizer(*p, Print)
	runtime.SetFinalizer(**p, Print)
}
开发者ID:dylanpoe,项目名称:golang.org,代码行数:8,代码来源:rtype1.go

示例11: run

func (n *node) run() {
	var (
		mate    = time.After(n.delay)
		suiters = make([]evo.Genome, len(n.peers))
		done    = make(chan evo.Genome)
		nextval = n.val
	)

	runtime.SetFinalizer(n.val, nil)
	runtime.SetFinalizer(n.val, func(val evo.Genome) {
		val.Close()
	})

	for {
		select {

		case n.delay = <-n.delayc:

		case n.valc <- n.val:
		case nextval = <-n.valc:

		case <-mate:
			go func(oldval evo.Genome) {
				var ok bool
				for i := range n.peers {
					suiters[i], ok = <-n.peers[i].valc
					if !ok {
						return
					}
				}
				newval := oldval.Evolve(suiters...)
				done <- newval
			}(n.val)

		case val := <-done:
			if nextval == n.val {
				nextval = val
			} else if val != n.val && val != nextval {
				val.Close()
			} else {
				n.val = nextval
				runtime.SetFinalizer(n.val, nil)
				runtime.SetFinalizer(n.val, func(val evo.Genome) {
					val.Close()
				})
			}
			mate = time.After(n.delay)

		case ch := <-n.closec:
			ch <- struct{}{}
			return
		}
	}
}
开发者ID:lucmichalski,项目名称:evo,代码行数:54,代码来源:graph.go

示例12: main

func main() {
	runtime.GOMAXPROCS(4)
	for i = 0; i < N; i++ {
		b := &B{i}
		a := &A{b, i}
		runtime.SetFinalizer(b, finalB)
		runtime.SetFinalizer(a, finalA)
	}
	for i := 0; i < N; i++ {
		runtime.GC()
		runtime.Gosched()
	}
	if nfinal < N*9/10 {
		panic("not enough finalizing:", nfinal, "/", N)
	}
}
开发者ID:ivanwyc,项目名称:google-go,代码行数:16,代码来源:mallocfin.go

示例13: Release

// Release releases any resources associated with the Process.
func (p *Process) Release() Error {
	// NOOP for unix.
	p.Pid = -1
	// no need for a finalizer anymore
	runtime.SetFinalizer(p, nil)
	return nil
}
开发者ID:jnwhiteh,项目名称:go,代码行数:8,代码来源:exec_unix.go

示例14: Prepare

// Prepare query string. Return a new statement.
func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
	pquery := C.CString(query)
	defer C.free(unsafe.Pointer(pquery))
	var s *C.sqlite3_stmt
	var tail *C.char
	rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &tail)
	if rv != C.SQLITE_OK {
		return nil, c.lastError()
	}
	var t string
	if tail != nil && *tail != '\000' {
		t = strings.TrimSpace(C.GoString(tail))
	}
	nv := int(C.sqlite3_bind_parameter_count(s))
	var nn []string
	for i := 0; i < nv; i++ {
		pn := C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1)))
		if len(pn) > 1 && pn[0] == '$' && 48 <= pn[1] && pn[1] <= 57 {
			nn = append(nn, C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1))))
		}
	}
	ss := &SQLiteStmt{c: c, s: s, nv: nv, nn: nn, t: t}
	runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
	return ss, nil
}
开发者ID:Wishing-Wall,项目名称:wishingwall,代码行数:26,代码来源:sqlite3.go

示例15: Close

func (s *driverStmt) Close() error {
	if s != nil && s.res != nil {
		C.PQclear(s.res)
		runtime.SetFinalizer(s, nil)
	}
	return nil
}
开发者ID:NDrive,项目名称:gopgsqldriver,代码行数:7,代码来源:pgdriver.go


注:本文中的runtime.SetFinalizer函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。