tst-basic2.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright (C) 2002, 2003, 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 <stdint.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #define N 20
  21. static pthread_t th[N];
  22. static pthread_mutex_t lock[N];
  23. static void *tf (void *a)
  24. {
  25. uintptr_t idx = (uintptr_t) a;
  26. pthread_mutex_lock (&lock[idx]);
  27. return pthread_equal (pthread_self (), th[idx]) ? NULL : (void *) 1l;
  28. }
  29. int
  30. do_test (void)
  31. {
  32. if (pthread_equal (pthread_self (), pthread_self ()) == 0)
  33. {
  34. puts ("pthread_equal (pthread_self (), pthread_self ()) failed");
  35. exit (1);
  36. }
  37. pthread_attr_t at;
  38. if (pthread_attr_init (&at) != 0)
  39. {
  40. puts ("attr_init failed");
  41. return 1;
  42. }
  43. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  44. {
  45. puts ("attr_setstacksize failed");
  46. return 1;
  47. }
  48. int i;
  49. for (i = 0; i < N; ++i)
  50. {
  51. if (pthread_mutex_init (&lock[i], NULL) != 0)
  52. {
  53. puts ("mutex_init failed");
  54. exit (1);
  55. }
  56. if (pthread_mutex_lock (&lock[i]) != 0)
  57. {
  58. puts ("mutex_lock failed");
  59. exit (1);
  60. }
  61. if (pthread_create (&th[i], &at, tf, (void *) (long int) i) != 0)
  62. {
  63. puts ("create failed");
  64. exit (1);
  65. }
  66. if (pthread_mutex_unlock (&lock[i]) != 0)
  67. {
  68. puts ("mutex_unlock failed");
  69. exit (1);
  70. }
  71. printf ("created thread %d\n", i);
  72. }
  73. if (pthread_attr_destroy (&at) != 0)
  74. {
  75. puts ("attr_destroy failed");
  76. return 1;
  77. }
  78. int result = 0;
  79. for (i = 0; i < N; ++i)
  80. {
  81. void *r;
  82. int e;
  83. if ((e = pthread_join (th[i], &r)) != 0)
  84. {
  85. printf ("join failed: %d\n", e);
  86. _exit (1);
  87. }
  88. else if (r != NULL)
  89. result = 1;
  90. }
  91. return 0;
  92. }
  93. #define TEST_FUNCTION do_test ()
  94. #include "../test-skeleton.c"