once4.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * once4.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  8. * Copyright(C) 2008 Jason Schmidlapp
  9. *
  10. * Contact Email: [email protected]
  11. *
  12. *
  13. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  14. * Copyright(C) 1998 John E. Bossom
  15. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  16. *
  17. * Contact Email: [email protected]
  18. *
  19. * The original list of contributors to the Pthreads-win32 project
  20. * is contained in the file CONTRIBUTORS.ptw32 included with the
  21. * source code distribution. The list can also be seen at the
  22. * following World Wide Web location:
  23. * http://sources.redhat.com/pthreads-win32/contributors.html
  24. *
  25. * This library is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU Lesser General Public
  27. * License as published by the Free Software Foundation; either
  28. * version 2 of the License, or (at your option) any later version.
  29. *
  30. * This library is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. * Lesser General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Lesser General Public
  36. * License along with this library in the file COPYING.LIB;
  37. * if not, write to the Free Software Foundation, Inc.,
  38. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  39. *
  40. * --------------------------------------------------------------------------
  41. *
  42. * Create several pthread_once objects and channel several threads
  43. * through each. Make the init_routine cancelable and cancel them
  44. * waiters waiting. Vary the priorities.
  45. *
  46. * Depends on API functions:
  47. * pthread_once()
  48. * pthread_create()
  49. * pthread_testcancel()
  50. * pthread_cancel()
  51. * pthread_once()
  52. */
  53. #include "test.h"
  54. #define NUM_THREADS OS_MAX_SIMUL_THREADS / 5 /* Targeting each once control */
  55. #define NUM_ONCE 5
  56. static pthread_once_t o = PTHREAD_ONCE_INIT;
  57. static pthread_once_t once[NUM_ONCE];
  58. typedef struct
  59. {
  60. int i;
  61. int null;
  62. pte_osMutexHandle cs;
  63. } sharedInt_t;
  64. static sharedInt_t numOnce = {0, 0};
  65. static sharedInt_t numThreads = {0, 0};
  66. typedef struct
  67. {
  68. int threadnum;
  69. int oncenum;
  70. int myPrio;
  71. // HANDLE w32Thread;
  72. } bag_t;
  73. static bag_t threadbag[NUM_THREADS][NUM_ONCE];
  74. //static CRITICAL_SECTION print_lock;
  75. static void
  76. mycleanupfunc(void * arg)
  77. {
  78. }
  79. static void
  80. myinitfunc(void)
  81. {
  82. pte_osMutexLock(numOnce.cs);
  83. numOnce.i++;
  84. pte_osMutexUnlock(numOnce.cs);
  85. /* Simulate slow once routine so that following threads pile up behind it */
  86. pte_osThreadSleep(10);
  87. /* test for cancelation late so we're sure to have waiters. */
  88. pthread_testcancel();
  89. }
  90. static 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. pthread_cleanup_push(mycleanupfunc, arg);
  112. assert(pthread_once(&once[bag->oncenum], myinitfunc) == 0);
  113. pthread_cleanup_pop(1);
  114. pte_osMutexLock(numThreads.cs);
  115. numThreads.i++;
  116. pte_osMutexUnlock(numThreads.cs);
  117. return 0;
  118. }
  119. int pthread_test_once4()
  120. {
  121. pthread_t t[NUM_THREADS][NUM_ONCE];
  122. int i, j;
  123. numOnce.i = 0;
  124. numThreads.i = 0;
  125. // pte_osMutexCreate(&print_lock);
  126. // pte_osMutexCreate(&numThreads.cs);
  127. // pte_osMutexCreate(&numOnce.cs);
  128. /*
  129. * Set the priority class to realtime - otherwise normal
  130. * Windows random priority boosting will obscure any problems.
  131. */
  132. //SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
  133. /* Set main thread to lower prio than threads */
  134. //SetThreadPriority(GetCurrentThread(), -2);
  135. for (j = 0; j < NUM_ONCE; j++)
  136. {
  137. once[j] = o;
  138. for (i = 0; i < NUM_THREADS; i++)
  139. {
  140. bag_t * bag = &threadbag[i][j];
  141. bag->threadnum = i;
  142. bag->oncenum = j;
  143. assert(pthread_create(&t[i][j], NULL, mythread, (void *) bag) == 0);
  144. }
  145. }
  146. for (j = 0; j < NUM_ONCE; j++)
  147. for (i = 0; i < NUM_THREADS; i++)
  148. if (pthread_join(t[i][j], NULL) != 0)
  149. assert(0); //Join failed for [thread,once]
  150. /*
  151. * All threads will cancel, none will return normally from
  152. * pthread_once and so numThreads should never be incremented. However,
  153. * numOnce should be incremented by every thread (NUM_THREADS*NUM_ONCE).
  154. */
  155. assert(numOnce.i == NUM_ONCE * NUM_THREADS);
  156. assert(numThreads.i == 0);
  157. // pte_osMutexDelete(&numOnce.cs);
  158. // pte_osMutexDelete(&numThreads.cs);
  159. // pte_osMutexDelete(&print_lock);
  160. return 0;
  161. }