once4.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * once4.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
  38. * waiters waiting. Vary the priorities.
  39. *
  40. * Depends on API functions:
  41. * pthread_once()
  42. * pthread_create()
  43. * pthread_testcancel()
  44. * pthread_cancel()
  45. * pthread_once()
  46. */
  47. #include "test.h"
  48. #define NUM_THREADS 100 /* Targeting each once control */
  49. #define NUM_ONCE 10
  50. pthread_once_t o = PTHREAD_ONCE_INIT;
  51. pthread_once_t once[NUM_ONCE];
  52. typedef struct {
  53. int i;
  54. CRITICAL_SECTION cs;
  55. } sharedInt_t;
  56. static sharedInt_t numOnce = {0, {0}};
  57. static sharedInt_t numThreads = {0, {0}};
  58. typedef struct {
  59. int threadnum;
  60. int oncenum;
  61. int myPrio;
  62. HANDLE w32Thread;
  63. } bag_t;
  64. static bag_t threadbag[NUM_THREADS][NUM_ONCE];
  65. CRITICAL_SECTION print_lock;
  66. void
  67. mycleanupfunc(void * arg)
  68. {
  69. bag_t * bag = (bag_t *) arg;
  70. EnterCriticalSection(&print_lock);
  71. /* once thrd prio error */
  72. printf("%4d %4d %4d %4d\n",
  73. bag->oncenum,
  74. bag->threadnum,
  75. bag->myPrio,
  76. bag->myPrio - GetThreadPriority(bag->w32Thread));
  77. LeaveCriticalSection(&print_lock);
  78. }
  79. void
  80. myinitfunc(void)
  81. {
  82. EnterCriticalSection(&numOnce.cs);
  83. numOnce.i++;
  84. LeaveCriticalSection(&numOnce.cs);
  85. /* Simulate slow once routine so that following threads pile up behind it */
  86. Sleep(10);
  87. /* test for cancelation late so we're sure to have waiters. */
  88. pthread_testcancel();
  89. }
  90. void *
  91. mythread(void * arg)
  92. {
  93. bag_t * bag = (bag_t *) arg;
  94. struct sched_param param;
  95. /*
  96. * Cancel every thread. These threads are deferred cancelable only, so
  97. * only the thread performing the init_routine will see it (there are
  98. * no other cancelation points here). The result will be that every thread
  99. * eventually cancels only when it becomes the new initter.
  100. */
  101. pthread_t self = pthread_self();
  102. bag->w32Thread = pthread_getw32threadhandle_np(self);
  103. /*
  104. * Set priority between -2 and 2 inclusive.
  105. */
  106. bag->myPrio = (bag->threadnum % 5) - 2;
  107. param.sched_priority = bag->myPrio;
  108. pthread_setschedparam(self, SCHED_OTHER, &param);
  109. /* Trigger a cancellation at the next cancellation point in this thread */
  110. pthread_cancel(self);
  111. #if 0
  112. pthread_cleanup_push(mycleanupfunc, arg);
  113. assert(pthread_once(&once[bag->oncenum], myinitfunc) == 0);
  114. pthread_cleanup_pop(1);
  115. #else
  116. assert(pthread_once(&once[bag->oncenum], myinitfunc) == 0);
  117. #endif
  118. EnterCriticalSection(&numThreads.cs);
  119. numThreads.i++;
  120. LeaveCriticalSection(&numThreads.cs);
  121. return 0;
  122. }
  123. int
  124. main()
  125. {
  126. pthread_t t[NUM_THREADS][NUM_ONCE];
  127. int i, j;
  128. InitializeCriticalSection(&print_lock);
  129. InitializeCriticalSection(&numThreads.cs);
  130. InitializeCriticalSection(&numOnce.cs);
  131. #if 0
  132. /* once thrd prio change */
  133. printf("once thrd prio error\n");
  134. #endif
  135. /*
  136. * Set the priority class to realtime - otherwise normal
  137. * Windows random priority boosting will obscure any problems.
  138. */
  139. SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
  140. /* Set main thread to lower prio than threads */
  141. SetThreadPriority(GetCurrentThread(), -2);
  142. for (j = 0; j < NUM_ONCE; j++)
  143. {
  144. once[j] = o;
  145. for (i = 0; i < NUM_THREADS; i++)
  146. {
  147. bag_t * bag = &threadbag[i][j];
  148. bag->threadnum = i;
  149. bag->oncenum = j;
  150. /* GCC build: create was failing with EAGAIN after 790 threads */
  151. while (0 != pthread_create(&t[i][j], NULL, mythread, (void *)bag))
  152. sched_yield();
  153. }
  154. }
  155. for (j = 0; j < NUM_ONCE; j++)
  156. for (i = 0; i < NUM_THREADS; i++)
  157. if (pthread_join(t[i][j], NULL) != 0)
  158. printf("Join failed for [thread,once] = [%d,%d]\n", i, j);
  159. /*
  160. * All threads will cancel, none will return normally from
  161. * pthread_once and so numThreads should never be incremented. However,
  162. * numOnce should be incremented by every thread (NUM_THREADS*NUM_ONCE).
  163. */
  164. assert(numOnce.i == NUM_ONCE * NUM_THREADS);
  165. assert(numThreads.i == 0);
  166. DeleteCriticalSection(&numOnce.cs);
  167. DeleteCriticalSection(&numThreads.cs);
  168. DeleteCriticalSection(&print_lock);
  169. return 0;
  170. }