Last active
October 5, 2015 10:27
-
-
Save EricMountain-1A/a3efc06714b6bfba3161 to your computer and use it in GitHub Desktop.
Coring in Go
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
// Produces a core. | |
// Usage: GOTRACEBACK=crash ./example | |
// Check ulimit -c beforehand. | |
// Note the root signal is SEGV, however Go catches this, handles it and raises ABRT. | |
package main | |
import ( | |
"fmt" | |
"sync/atomic" | |
"unsafe" | |
) | |
type T struct { | |
value int | |
} | |
func Swap(dest **T, old, new *T) bool { | |
//udest := (*unsafe.Pointer)(unsafe.Pointer(dest)) | |
udest := (*unsafe.Pointer)(unsafe.Pointer(unsafe.Alignof(1))) | |
return atomic.CompareAndSwapPointer(udest, | |
unsafe.Pointer(old), | |
unsafe.Pointer(new), | |
) | |
} | |
func main() { | |
x := &T{42} | |
n := &T{50} | |
fmt.Println(*x, *n) | |
z := x | |
Swap(&x, z, n) | |
fmt.Println(*x, *n) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also https://gist.github.com/lhuard1A/f88cce7487e84ff7a9e8