once2.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * once2.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 static pthread_once objects and channel several threads
  43. * through each.
  44. *
  45. * Depends on API functions:
  46. * pthread_once()
  47. * pthread_create()
  48. */
  49. #include "test.h"
  50. #define NUM_THREADS OS_MAX_SIMUL_THREADS / 5 /* Targeting each once control */
  51. #define NUM_ONCE 5
  52. static pthread_once_t o = PTHREAD_ONCE_INIT;
  53. static pthread_once_t once[NUM_ONCE];
  54. typedef struct
  55. {
  56. int i;
  57. pte_osMutexHandle cs;
  58. } sharedInt_t;
  59. static sharedInt_t numOnce = {0, 0};
  60. static sharedInt_t numThreads = {0, 0};
  61. static void
  62. myfunc(void)
  63. {
  64. pte_osMutexLock(numOnce.cs);
  65. numOnce.i++;
  66. pte_osMutexUnlock(numOnce.cs);
  67. /* Simulate slow once routine so that following threads pile up behind it */
  68. pte_osThreadSleep(100);
  69. }
  70. static void *
  71. mythread(void * arg)
  72. {
  73. assert(pthread_once(&once[(int) arg], myfunc) == 0);
  74. pte_osMutexLock(numThreads.cs);
  75. numThreads.i++;
  76. pte_osMutexUnlock(numThreads.cs);
  77. return 0;
  78. }
  79. int pthread_test_once2()
  80. {
  81. pthread_t t[NUM_THREADS][NUM_ONCE];
  82. int i, j;
  83. int result;
  84. numOnce.i = 0;
  85. numThreads.i = 0;
  86. pte_osMutexCreate(&numThreads.cs);
  87. pte_osMutexCreate(&numOnce.cs);
  88. for (j = 0; j < NUM_ONCE; j++)
  89. {
  90. once[j] = o;
  91. for (i = 0; i < NUM_THREADS; i++)
  92. assert(pthread_create(&t[i][j], NULL, mythread, (void *) j) == 0);
  93. }
  94. for (j = 0; j < NUM_ONCE; j++)
  95. for (i = 0; i < NUM_THREADS; i++)
  96. if ((result = pthread_join(t[i][j], NULL)) != 0)
  97. {
  98. assert(0); //Join failed for [thread,once]
  99. }
  100. /* Stop some compilers from generating warning */
  101. result = result;
  102. pte_osThreadSleep(1000);
  103. assert(numOnce.i == NUM_ONCE);
  104. assert(numThreads.i == NUM_THREADS * NUM_ONCE);
  105. pte_osMutexDelete(numOnce.cs);
  106. pte_osMutexDelete(numThreads.cs);
  107. return 0;
  108. }