sigprocmask.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !windows
  5. #include <errno.h>
  6. #include <signal.h>
  7. #include <stdlib.h>
  8. #include <pthread.h>
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12. extern void IntoGoAndBack();
  13. int CheckBlocked() {
  14. sigset_t mask;
  15. sigprocmask(SIG_BLOCK, NULL, &mask);
  16. return sigismember(&mask, SIGIO);
  17. }
  18. static void* sigthreadfunc(void* unused) {
  19. sigset_t mask;
  20. sigemptyset(&mask);
  21. sigaddset(&mask, SIGIO);
  22. sigprocmask(SIG_BLOCK, &mask, NULL);
  23. IntoGoAndBack();
  24. return NULL;
  25. }
  26. int RunSigThread() {
  27. int tries;
  28. pthread_t thread;
  29. int r;
  30. struct timespec ts;
  31. for (tries = 0; tries < 20; tries++) {
  32. r = pthread_create(&thread, NULL, &sigthreadfunc, NULL);
  33. if (r == 0) {
  34. return pthread_join(thread, NULL);
  35. }
  36. if (r != EAGAIN) {
  37. return r;
  38. }
  39. ts.tv_sec = 0;
  40. ts.tv_nsec = (tries + 1) * 1000 * 1000; // Milliseconds.
  41. nanosleep(&ts, NULL);
  42. }
  43. return EAGAIN;
  44. }