test-dlclose-exit-race-helper.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Helper for exit/dlclose race test (Bug 22180).
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <stdbool.h>
  17. #include <stdlib.h>
  18. #include <semaphore.h>
  19. #include <unistd.h>
  20. #include <support/check.h>
  21. #include <support/xthread.h>
  22. /* Semaphore defined in executable to ensure we have a happens-before
  23. between the first function starting and exit being called. */
  24. extern sem_t order1;
  25. /* Semaphore defined in executable to ensure we have a happens-before
  26. between the second function starting and the first function returning. */
  27. extern sem_t order2;
  28. /* glibc function for registering DSO-specific exit functions. */
  29. extern int __cxa_atexit (void (*func) (void *), void *arg, void *dso_handle);
  30. /* Hidden compiler handle to this shared object. */
  31. extern void *__dso_handle __attribute__ ((__weak__));
  32. static void
  33. first (void *start)
  34. {
  35. /* Let the exiting thread run. */
  36. sem_post (&order1);
  37. /* Wait for exiting thread to finish. */
  38. sem_wait (&order2);
  39. printf ("first\n");
  40. }
  41. static void
  42. second (void *start)
  43. {
  44. /* We may be called from different threads.
  45. This lock protects called. */
  46. static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
  47. static bool called = false;
  48. xpthread_mutex_lock (&mtx);
  49. if (called)
  50. FAIL_EXIT1 ("second called twice!");
  51. called = true;
  52. xpthread_mutex_unlock (&mtx);
  53. printf ("second\n");
  54. }
  55. __attribute__ ((constructor)) static void
  56. constructor (void)
  57. {
  58. sem_init (&order1, 0, 0);
  59. sem_init (&order2, 0, 0);
  60. __cxa_atexit (second, NULL, __dso_handle);
  61. __cxa_atexit (first, NULL, __dso_handle);
  62. }