staticthreads.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* This test program is part of GDB, The GNU debugger.
  2. Copyright 2004-2022 Free Software Foundation, Inc.
  3. Originally written by Jeff Johnston <jjohnstn@redhat.com>,
  4. contributed by Red Hat
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include <pthread.h>
  17. #include <semaphore.h>
  18. #include <stdio.h>
  19. #include <limits.h>
  20. #include <errno.h>
  21. sem_t semaphore;
  22. #ifdef HAVE_TLS
  23. __thread int tlsvar;
  24. #endif
  25. void *
  26. thread_function (void *arg)
  27. {
  28. #ifdef HAVE_TLS
  29. tlsvar = 2;
  30. #endif
  31. while (sem_wait (&semaphore) != 0)
  32. {
  33. if (errno != EINTR)
  34. {
  35. perror ("thread_function");
  36. return NULL;
  37. }
  38. }
  39. printf ("Thread executing\n"); /* tlsvar-is-set */
  40. return NULL;
  41. }
  42. int
  43. main (int argc, char **argv)
  44. {
  45. pthread_attr_t attr;
  46. pthread_attr_init (&attr);
  47. pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
  48. if (sem_init (&semaphore, 0, 0) == -1)
  49. {
  50. perror ("semaphore");
  51. return -1;
  52. }
  53. #ifdef HAVE_TLS
  54. tlsvar = 1;
  55. #endif
  56. /* Create a thread, wait for it to complete. */
  57. {
  58. pthread_t thread;
  59. pthread_create (&thread, &attr, thread_function, NULL);
  60. sem_post (&semaphore);
  61. pthread_join (thread, NULL);
  62. }
  63. pthread_attr_destroy (&attr);
  64. return 0;
  65. }