Skip to content

Instantly share code, notes, and snippets.

@shazow
Created May 9, 2016 17:57
Show Gist options
  • Save shazow/c8ea76aa6502d9b798500b9f427d954c to your computer and use it in GitHub Desktop.
Save shazow/c8ea76aa6502d9b798500b9f427d954c to your computer and use it in GitHub Desktop.
Safe pointer proxy for lending pointers across runtimes
package main
import "sync"
type ptrId uint
type ptrProxy struct {
sync.Mutex
count uint
lookup map[ptrId]interface{}
}
func (p *ptrProxy) Acquire(ptr interface{}) ptrId {
p.Lock()
id := ptrId(p.count)
p.count++
p.lookup[id] = ptr
p.Unlock()
return id
}
func (p *ptrProxy) Release(id ptrId) {
p.Lock()
delete(p.lookup, id)
p.Unlock()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment