thread_create-source.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Measure pthread_create thread creation with different stack
  2. and guard sizes.
  3. Copyright (C) 2017-2026 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <support/xthread.h>
  19. static size_t pgsize;
  20. static void
  21. thread_create_init (void)
  22. {
  23. pgsize = sysconf (_SC_PAGESIZE);
  24. }
  25. static void *
  26. thread_dummy (void *arg)
  27. {
  28. return NULL;
  29. }
  30. static void
  31. thread_create (int nthreads, size_t stacksize, size_t guardsize)
  32. {
  33. pthread_attr_t attr;
  34. xpthread_attr_init (&attr);
  35. stacksize = stacksize * pgsize;
  36. guardsize = guardsize * pgsize;
  37. xpthread_attr_setstacksize (&attr, stacksize);
  38. xpthread_attr_setguardsize (&attr, guardsize);
  39. pthread_t ts[nthreads];
  40. for (int i = 0; i < nthreads; i++)
  41. ts[i] = xpthread_create (&attr, thread_dummy, NULL);
  42. for (int i = 0; i < nthreads; i++)
  43. xpthread_join (ts[i]);
  44. }