Created
May 9, 2016 17:57
-
-
Save shazow/c8ea76aa6502d9b798500b9f427d954c to your computer and use it in GitHub Desktop.
Safe pointer proxy for lending pointers across runtimes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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