tst-aligned-alloc-random-thread.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* multi-threaded memory allocation/deallocation test.
  2. Copyright (C) 2024-2026 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 License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. 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; see the file COPYING.LIB. If
  14. not, see <https://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <support/check.h>
  19. #include <support/support.h>
  20. #include <support/xthread.h>
  21. #include <support/test-driver.h>
  22. #include <sys/sysinfo.h>
  23. #include <unistd.h>
  24. #ifndef ITERATIONS
  25. # define ITERATIONS 16
  26. #endif
  27. #ifndef NUM_THREADS
  28. # define NUM_THREADS 8
  29. #endif
  30. #ifndef NUM_ALLOCATIONS
  31. # define NUM_ALLOCATIONS 2048
  32. #endif
  33. static pthread_barrier_t barrier;
  34. __thread unsigned int seed;
  35. typedef struct
  36. {
  37. int id;
  38. pthread_t thread;
  39. } thread;
  40. thread threads[NUM_THREADS];
  41. void *allocations[NUM_THREADS][NUM_ALLOCATIONS];
  42. void
  43. run_thread_dealloc (int id)
  44. {
  45. for (int i = 0; i < NUM_ALLOCATIONS; i++)
  46. {
  47. free (allocations[id][i]);
  48. allocations[id][i] = NULL;
  49. }
  50. }
  51. void
  52. run_thread_alloc (int id)
  53. {
  54. size_t msb, size;
  55. for (int i = 0; i < NUM_ALLOCATIONS; i++)
  56. {
  57. msb = 1 << rand_r (&seed) % 16;
  58. size = msb + rand_r (&seed) % msb;
  59. allocations[id][i] = malloc (size);
  60. TEST_VERIFY_EXIT (allocations[id][i] != NULL);
  61. }
  62. }
  63. void *
  64. run_allocations (void *arg)
  65. {
  66. int id = *((int *) arg);
  67. seed = time (NULL) + id;
  68. /* Stage 1: First half o the threads allocating memory and the second
  69. * half waiting for them to finish
  70. */
  71. if (id < NUM_THREADS / 2)
  72. run_thread_alloc (id);
  73. xpthread_barrier_wait (&barrier);
  74. /* Stage 2: Half of the threads allocationg memory and the other
  75. * half deallocating:
  76. * - In the non cross-thread dealloc scenario the first half will be
  77. * deallocating the memory allocated by themselves in stage 1 and the
  78. * second half will be allocating memory.
  79. * - In the cross-thread dealloc scenario the first half will continue
  80. * to allocate memory and the second half will deallocate the memory
  81. * allocated by the first half in stage 1.
  82. */
  83. if (id < NUM_THREADS / 2)
  84. #ifndef CROSS_THREAD_DEALLOC
  85. run_thread_dealloc (id);
  86. #else
  87. run_thread_alloc (id + NUM_THREADS / 2);
  88. #endif
  89. else
  90. #ifndef CROSS_THREAD_DEALLOC
  91. run_thread_alloc (id);
  92. #else
  93. run_thread_dealloc (id - NUM_THREADS / 2);
  94. #endif
  95. xpthread_barrier_wait (&barrier);
  96. // Stage 3: Second half of the threads deallocating and the first half
  97. // waiting for them to finish.
  98. if (id >= NUM_THREADS / 2)
  99. run_thread_dealloc (id);
  100. return NULL;
  101. }
  102. static int
  103. do_test (void)
  104. {
  105. xpthread_barrier_init (&barrier, NULL, NUM_THREADS);
  106. for (int i = 0; i < ITERATIONS; i++)
  107. {
  108. for (int t = 0; t < NUM_THREADS; t++)
  109. {
  110. threads[t].id = t;
  111. threads[t].thread
  112. = xpthread_create (NULL, run_allocations, &threads[t].id);
  113. }
  114. for (int t = 0; t < NUM_THREADS; t++)
  115. xpthread_join (threads[t].thread);
  116. }
  117. return 0;
  118. }
  119. #include <support/test-driver.c>