once3.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * once3.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Create several pthread_once objects and channel several threads
  37. * through each. Make the init_routine cancelable and cancel them with
  38. * waiters waiting.
  39. *
  40. * Depends on API functions:
  41. * pthread_once()
  42. * pthread_create()
  43. * pthread_testcancel()
  44. * pthread_cancel()
  45. * pthread_once()
  46. */
  47. /* #define ASSERT_TRACE */
  48. #include "test.h"
  49. #define NUM_THREADS 100 /* Targeting each once control */
  50. #define NUM_ONCE 10
  51. static pthread_once_t o = PTHREAD_ONCE_INIT;
  52. static pthread_once_t once[NUM_ONCE];
  53. typedef struct {
  54. int i;
  55. CRITICAL_SECTION cs;
  56. } sharedInt_t;
  57. static sharedInt_t numOnce = {0, {0}};
  58. static sharedInt_t numThreads = {0, {0}};
  59. void
  60. myfunc(void)
  61. {
  62. EnterCriticalSection(&numOnce.cs);
  63. numOnce.i++;
  64. assert(numOnce.i > 0);
  65. LeaveCriticalSection(&numOnce.cs);
  66. /* Simulate slow once routine so that following threads pile up behind it */
  67. Sleep(10);
  68. /* test for cancelation late so we're sure to have waiters. */
  69. pthread_testcancel();
  70. }
  71. void *
  72. mythread(void * arg)
  73. {
  74. /*
  75. * Cancel every thread. These threads are deferred cancelable only, so
  76. * only the thread performing the once routine (my_func) will see it (there are
  77. * no other cancelation points here). The result will be that every thread
  78. * eventually cancels only when it becomes the new 'once' thread.
  79. */
  80. assert(pthread_cancel(pthread_self()) == 0);
  81. assert(pthread_once(&once[(int)(size_t)arg], myfunc) == 0);
  82. EnterCriticalSection(&numThreads.cs);
  83. numThreads.i++;
  84. LeaveCriticalSection(&numThreads.cs);
  85. return (void*)(size_t)0;
  86. }
  87. int
  88. main()
  89. {
  90. pthread_t t[NUM_THREADS][NUM_ONCE];
  91. int i, j;
  92. InitializeCriticalSection(&numThreads.cs);
  93. InitializeCriticalSection(&numOnce.cs);
  94. for (j = 0; j < NUM_ONCE; j++)
  95. {
  96. once[j] = o;
  97. for (i = 0; i < NUM_THREADS; i++)
  98. {
  99. /* GCC build: create was failing with EAGAIN after 790 threads */
  100. while (0 != pthread_create(&t[i][j], NULL, mythread, (void *)(size_t)j))
  101. sched_yield();
  102. }
  103. }
  104. for (j = 0; j < NUM_ONCE; j++)
  105. for (i = 0; i < NUM_THREADS; i++)
  106. if (pthread_join(t[i][j], NULL) != 0)
  107. printf("Join failed for [thread,once] = [%d,%d]\n", i, j);
  108. /*
  109. * All threads will cancel, none will return normally from
  110. * pthread_once and so numThreads should never be incremented. However,
  111. * numOnce should be incremented by every thread (NUM_THREADS*NUM_ONCE).
  112. */
  113. assert(numOnce.i == NUM_ONCE * NUM_THREADS);
  114. assert(numThreads.i == 0);
  115. DeleteCriticalSection(&numOnce.cs);
  116. DeleteCriticalSection(&numThreads.cs);
  117. return 0;
  118. }