threadlib.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Multithreading primitives.
  2. Copyright (C) 2005-2021 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
  14. #include <config.h>
  15. /* ========================================================================= */
  16. #if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS
  17. /* Use the POSIX threads library. */
  18. # include <errno.h>
  19. # include <pthread.h>
  20. # include <stdlib.h>
  21. # if PTHREAD_IN_USE_DETECTION_HARD
  22. # if defined __FreeBSD__ || defined __DragonFly__ /* FreeBSD */
  23. /* Test using pthread_key_create. */
  24. int
  25. glthread_in_use (void)
  26. {
  27. static int tested;
  28. static int result; /* 1: linked with -lpthread, 0: only with libc */
  29. if (!tested)
  30. {
  31. pthread_key_t key;
  32. int err = pthread_key_create (&key, NULL);
  33. if (err == ENOSYS)
  34. result = 0;
  35. else
  36. {
  37. result = 1;
  38. if (err == 0)
  39. pthread_key_delete (key);
  40. }
  41. tested = 1;
  42. }
  43. return result;
  44. }
  45. # else /* Solaris, HP-UX */
  46. /* Test using pthread_create. */
  47. /* The function to be executed by a dummy thread. */
  48. static void *
  49. dummy_thread_func (void *arg)
  50. {
  51. return arg;
  52. }
  53. int
  54. glthread_in_use (void)
  55. {
  56. static int tested;
  57. static int result; /* 1: linked with -lpthread, 0: only with libc */
  58. if (!tested)
  59. {
  60. pthread_t thread;
  61. if (pthread_create (&thread, NULL, dummy_thread_func, NULL) != 0)
  62. /* Thread creation failed. */
  63. result = 0;
  64. else
  65. {
  66. /* Thread creation works. */
  67. void *retval;
  68. if (pthread_join (thread, &retval) != 0)
  69. abort ();
  70. result = 1;
  71. }
  72. tested = 1;
  73. }
  74. return result;
  75. }
  76. # endif
  77. # endif
  78. #endif
  79. /* ========================================================================= */
  80. /* This declaration is solely to ensure that after preprocessing
  81. this file is never empty. */
  82. typedef int dummy;