Advertisement
yerden

preemption signal blocking [WIP]

Oct 25th, 2022
1,584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.21 KB | None | 0 0
  1. package main
  2.  
  3. /*
  4. #include <pthread.h>
  5. #include <signal.h>
  6.  
  7. static int ignore_sigurg()
  8. {
  9.     sigset_t set;
  10.     sigemptyset(&set);
  11.     sigaddset(&set, SIGURG);
  12.     return pthread_sigmask(SIG_BLOCK, &set, NULL);
  13. }
  14.  
  15. static int handleURG_alt() {
  16.     struct sigaction sigact;
  17.     sigact.sa_handler = SIG_BLOCK;
  18.     sigemptyset(&sigact.sa_mask);
  19.     sigact.sa_flags = 0;
  20.     sigaction(SIGURG, &sigact, NULL);
  21.     return 0;
  22. }
  23. */
  24. import "C"
  25.  
  26. import (
  27.     "flag"
  28.     "fmt"
  29.     "os"
  30.     "os/signal"
  31.     "runtime"
  32.     "sync"
  33.     "syscall"
  34. )
  35.  
  36. var ignorePreemption = flag.Bool("ignore", false, "Specify true to block SIGURG")
  37.  
  38. func main() {
  39.     flag.Parse()
  40.  
  41.     // tight loop
  42.     go func() {
  43.         for {
  44.         }
  45.     }()
  46.  
  47.     var wg sync.WaitGroup
  48.  
  49.     for i := 0; i < 2; i++ {
  50.         wg.Add(1)
  51.         go func(n int) {
  52.             defer wg.Done()
  53.             runtime.LockOSThread()
  54.  
  55.             if n == 0 && *ignorePreemption {
  56.                 if rc := C.ignore_sigurg(); rc != 0 {
  57.                     panic(rc)
  58.                 }
  59.                 fmt.Println("blocked SIGURG on goroutine", n)
  60.             }
  61.  
  62.             ch := make(chan os.Signal, 1)
  63.             signal.Notify(ch, syscall.SIGURG, os.Interrupt)
  64.  
  65.             for sig := range ch {
  66.                 fmt.Printf("got signal in goroutine %d: %v\n", n, sig)
  67.  
  68.                 if sig == os.Interrupt {
  69.                     break
  70.                 }
  71.             }
  72.         }(i)
  73.     }
  74.  
  75.     wg.Wait()
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement