Skip to content

Instantly share code, notes, and snippets.

@EricMountain-1A
Last active October 5, 2015 10:27
Show Gist options
  • Save EricMountain-1A/a3efc06714b6bfba3161 to your computer and use it in GitHub Desktop.
Save EricMountain-1A/a3efc06714b6bfba3161 to your computer and use it in GitHub Desktop.
Coring in Go
// 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)
}
@EricMountain-1A
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment