tst-basic2.c 2.6 KB

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