threadlib.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Multithreading primitives.
  2. Copyright (C) 2005-2016 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 <http://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
  14. #include <config.h>
  15. /* ========================================================================= */
  16. #if USE_POSIX_THREADS
  17. /* Use the POSIX threads library. */
  18. # include <pthread.h>
  19. # include <stdlib.h>
  20. # if PTHREAD_IN_USE_DETECTION_HARD
  21. /* The function to be executed by a dummy thread. */
  22. static void *
  23. dummy_thread_func (void *arg)
  24. {
  25. return arg;
  26. }
  27. int
  28. glthread_in_use (void)
  29. {
  30. static int tested;
  31. static int result; /* 1: linked with -lpthread, 0: only with libc */
  32. if (!tested)
  33. {
  34. pthread_t thread;
  35. if (pthread_create (&thread, NULL, dummy_thread_func, NULL) != 0)
  36. /* Thread creation failed. */
  37. result = 0;
  38. else
  39. {
  40. /* Thread creation works. */
  41. void *retval;
  42. if (pthread_join (thread, &retval) != 0)
  43. abort ();
  44. result = 1;
  45. }
  46. tested = 1;
  47. }
  48. return result;
  49. }
  50. # endif
  51. #endif
  52. /* ========================================================================= */
  53. /* This declaration is solely to ensure that after preprocessing
  54. this file is never empty. */
  55. typedef int dummy;