Created
August 31, 2012 17:34
Revisions
-
ecin revised this gist
Sep 2, 2012 . No changes.There are no files selected for viewing
-
ecin revised this gist
Sep 2, 2012 . 2 changed files with 9 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ golang*::Ping: { printf("Ping hits, counter goes up to #%d!", arg0); } golang*::Pong: { printf("Pong connects, counter goes down to #%d!", arg0); } -
ecin revised this gist
Sep 2, 2012 . 4 changed files with 22 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,16 +7,16 @@ */ #include "probes.h" void Ping(int n) { GOLANG_PING(n); } int Ping_enabled() { return GOLANG_PING_ENABLED(); } void Pong(int n) { GOLANG_PONG(n); } int Pong_enabled() { This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,6 @@ dtrace -o probes.h -h -s probes.d */ provider golang { probe ping(int); probe pong(int); }; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,10 @@ package main /* Ping vs Pong: A Gladiatorial Match Two goroutines enter, only one leaves... */ /* #cgo LDFLAGS: -lprobes -L/usr/local/lib #include "probes.h" @@ -13,6 +18,7 @@ import ( func ping(c chan int, q chan int) { for ;; { // Prepare a swing time.Sleep(time.Duration(rand.Intn(5)) * time.Second) n := <- c n += 1 @@ -22,12 +28,14 @@ func ping(c chan int, q chan int) { } c <- n // Dtrace spectators: roar for Ping! C.Ping(C.int(n)) } } func pong(c chan int, q chan int) { for ;; { // Charge an attack time.Sleep(time.Duration(rand.Intn(5)) * time.Second) n := <- c n -= 1 @@ -37,6 +45,7 @@ func pong(c chan int, q chan int) { } c <- n // Dtrace spectators: cheer for Pong! C.Pong(C.int(n)) } } @@ -45,17 +54,19 @@ func main() { c := make(chan int, 2) q := make(chan int) // Ping and Pong enter the arena go ping(c, q) go pong(c, q) // The match starts! c <- 5 // Wait for victor signal winner := <- q if winner == 0 { println("After a long and arduous battle, Pong falls to its knees, its body a weary mess. Ping wins!") } else { println("Overcome by one too many wounds, Ping rushes to the ground. Pong wins!") } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ /* ... */ /* probes.c functions */ void Ping(int); int Ping_enabled(); void Pong(int); int Pong_enabled(); -
ecin revised this gist
Sep 2, 2012 . 1 changed file with 61 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ package main /* #cgo LDFLAGS: -lprobes -L/usr/local/lib #include "probes.h" */ import "C" import ( "time" "math/rand" ) func ping(c chan int, q chan int) { for ;; { time.Sleep(time.Duration(rand.Intn(5)) * time.Second) n := <- c n += 1 if n == 10 { q <- 0 } c <- n C.Ping(C.int(n)) } } func pong(c chan int, q chan int) { for ;; { time.Sleep(time.Duration(rand.Intn(5)) * time.Second) n := <- c n -= 1 if n == 0 { q <- 1 } c <- n C.Pong(C.int(n)) } } func main() { c := make(chan int, 2) q := make(chan int) go ping(c, q) go pong(c, q) c <- 5 // Wait for quit winner := <- q if winner == 0 { println("Ping wins!") } else { println("Pong wins!") } } -
ecin revised this gist
Aug 31, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ /* Header file generated by dtrace */ /* ... */ /* probes.c functions */ void Ping(); int Ping_enabled(); void Pong(); -
ecin revised this gist
Aug 31, 2012 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ /* Header file generated by dtrace */ /* ... */ void Ping(); int Ping_enabled(); void Pong(); int Pong_enabled(); -
ecin revised this gist
Aug 31, 2012 . 1 changed file with 10 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,24 @@ /* Compile probes.c as a shared library In Mac OS X (http://www.finkproject.org/doc/porting/shared.php#build-lib): cc -fno-common -c probes.c cc -dynamiclib -install_name /usr/local/lib/libprobes.dylib -o libprobes.dylib probes.o cp libprobes.dylib /usr/local/lib/ */ #include "probes.h" void Ping() { GOLANG_PING(); } int Ping_enabled() { return GOLANG_PING_ENABLED(); } void Pong() { GOLANG_PONG(); } int Pong_enabled() { return GOLANG_PONG_ENABLED(); } -
ecin revised this gist
Aug 31, 2012 . 1 changed file with 17 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ #include "probes.h" void Ping() { GOLANG_PING(); } bool Ping_enabled() { return GOLANG_PING_ENABLED(); } void Pong() { GOLANG_PONG(); } bool Pong_enabled() { return GOLANG_PONG_ENABLED(); } -
ecin created this gist
Aug 31, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ /* Generate probes.h header file with: dtrace -o probes.h -h -s probes.d */ provider golang { probe ping(); probe pong(); };