tst-mutex7.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright (C) 2002, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <pthread.h>
  16. #include <stdio.h>
  17. #include <time.h>
  18. #if defined(__GLIBC__) || defined(__UCLIBC__)
  19. #ifndef INIT
  20. # define INIT PTHREAD_MUTEX_INITIALIZER
  21. #endif
  22. static pthread_mutex_t lock = INIT;
  23. #define ROUNDS 1000
  24. #define N 100
  25. static void *
  26. tf (void *arg)
  27. {
  28. int nr = (long int) arg;
  29. int cnt;
  30. struct timespec ts = { .tv_sec = 0, .tv_nsec = 11000 };
  31. for (cnt = 0; cnt < ROUNDS; ++cnt)
  32. {
  33. if (pthread_mutex_lock (&lock) != 0)
  34. {
  35. printf ("thread %d: failed to get the lock\n", nr);
  36. return (void *) 1l;
  37. }
  38. if (pthread_mutex_unlock (&lock) != 0)
  39. {
  40. printf ("thread %d: failed to release the lock\n", nr);
  41. return (void *) 1l;
  42. }
  43. nanosleep (&ts, NULL);
  44. }
  45. return NULL;
  46. }
  47. #endif
  48. static int
  49. do_test (void)
  50. {
  51. #if defined(__GLIBC__) || defined(__UCLIBC__)
  52. pthread_attr_t at;
  53. pthread_t th[N];
  54. int cnt;
  55. if (pthread_attr_init (&at) != 0)
  56. {
  57. puts ("attr_init failed");
  58. return 1;
  59. }
  60. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  61. {
  62. puts ("attr_setstacksize failed");
  63. return 1;
  64. }
  65. if (pthread_mutex_lock (&lock) != 0)
  66. {
  67. puts ("locking in parent failed");
  68. return 1;
  69. }
  70. for (cnt = 0; cnt < N; ++cnt)
  71. if (pthread_create (&th[cnt], &at, tf, (void *) (long int) cnt) != 0)
  72. {
  73. printf ("creating thread %d failed\n", cnt);
  74. return 1;
  75. }
  76. if (pthread_attr_destroy (&at) != 0)
  77. {
  78. puts ("attr_destroy failed");
  79. return 1;
  80. }
  81. if (pthread_mutex_unlock (&lock) != 0)
  82. {
  83. puts ("unlocking in parent failed");
  84. return 1;
  85. }
  86. for (cnt = 0; cnt < N; ++cnt)
  87. if (pthread_join (th[cnt], NULL) != 0)
  88. {
  89. printf ("joining thread %d failed\n", cnt);
  90. return 1;
  91. }
  92. return 0;
  93. #else
  94. return 23;
  95. #endif
  96. }
  97. #define TIMEOUT 60
  98. #define TEST_FUNCTION do_test ()
  99. #include "../test-skeleton.c"