once3.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * once3.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 with
  44. * waiters waiting.
  45. *
  46. * Depends on API functions:
  47. * pthread_once()
  48. * pthread_create()
  49. * pthread_testcancel()
  50. * pthread_cancel()
  51. * pthread_once()
  52. */
  53. //#define ASSERT_TRACE
  54. #include "test.h"
  55. #define NUM_THREADS OS_MAX_SIMUL_THREADS / 5 /* Targeting each once control */
  56. #define NUM_ONCE 5
  57. static pthread_once_t o = PTHREAD_ONCE_INIT;
  58. static pthread_once_t once[NUM_ONCE];
  59. typedef struct
  60. {
  61. int i;
  62. int null;
  63. pte_osMutexHandle cs;
  64. } sharedInt_t;
  65. static sharedInt_t numOnce = {0, 0, 0};
  66. static sharedInt_t numThreads = {0, 0, 0};
  67. static void
  68. myfunc(void)
  69. {
  70. pte_osMutexLock(numOnce.cs);
  71. numOnce.i++;
  72. assert(numOnce.i > 0);
  73. pte_osMutexUnlock(numOnce.cs);
  74. /* Simulate slow once routine so that following threads pile up behind it */
  75. pte_osThreadSleep(10);
  76. /* test for cancelation late so we're sure to have waiters. */
  77. pthread_testcancel();
  78. }
  79. static void *
  80. mythread(void * arg)
  81. {
  82. /*
  83. * Cancel every thread. These threads are deferred cancelable only, so
  84. * only the thread performing the once routine (my_func) will see it (there are
  85. * no other cancelation points here). The result will be that every thread
  86. * eventually cancels only when it becomes the new once thread.
  87. */
  88. assert(pthread_cancel(pthread_self()) == 0);
  89. assert(pthread_once(&once[(int) arg], myfunc) == 0);
  90. pte_osMutexLock(numThreads.cs);
  91. numThreads.i++;
  92. pte_osMutexUnlock(numThreads.cs);
  93. return 0;
  94. }
  95. int pthread_test_once3()
  96. {
  97. pthread_t t[NUM_THREADS][NUM_ONCE];
  98. int i, j;
  99. pthread_once_t onceinit = PTHREAD_ONCE_INIT;
  100. numOnce.i = 0;
  101. numThreads.i = 0;
  102. o = onceinit;
  103. pte_osMutexCreate(&numThreads.cs);
  104. pte_osMutexCreate(&numOnce.cs);
  105. for (j = 0; j < NUM_ONCE; j++)
  106. {
  107. once[j] = o;
  108. for (i = 0; i < NUM_THREADS; i++)
  109. {
  110. assert(pthread_create(&t[i][j], NULL, mythread, (void *) j) == 0);
  111. }
  112. }
  113. for (j = 0; j < NUM_ONCE; j++)
  114. for (i = 0; i < NUM_THREADS; i++)
  115. if (pthread_join(t[i][j], NULL) != 0)
  116. assert(0); // Join failed for [thread,once]
  117. /*
  118. * All threads will cancel, none will return normally from
  119. * pthread_once and so numThreads should never be incremented. However,
  120. * numOnce should be incremented by every thread (NUM_THREADS*NUM_ONCE).
  121. */
  122. assert(numOnce.i == NUM_ONCE * NUM_THREADS);
  123. assert(numThreads.i == 0);
  124. pte_osMutexDelete(numOnce.cs);
  125. pte_osMutexDelete(numThreads.cs);
  126. return 0;
  127. }