本文整理汇总了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")
}
}
}
示例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()
}
示例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
}
示例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
}
示例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
}
示例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")
}
}
示例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
}
示例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")
}
示例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)
}
}
示例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)
}
示例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
}
}
}
示例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)
}
}
示例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
}
示例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
}
示例15: Close
func (s *driverStmt) Close() error {
if s != nil && s.res != nil {
C.PQclear(s.res)
runtime.SetFinalizer(s, nil)
}
return nil
}