once2.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * once2.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 static pthread_once objects and channel several threads
  37. * through each.
  38. *
  39. * Depends on API functions:
  40. * pthread_once()
  41. * pthread_create()
  42. */
  43. #include "test.h"
  44. #define NUM_THREADS 100 /* Targeting each once control */
  45. #define NUM_ONCE 10
  46. pthread_once_t o = PTHREAD_ONCE_INIT;
  47. pthread_once_t once[NUM_ONCE];
  48. typedef struct {
  49. int i;
  50. CRITICAL_SECTION cs;
  51. } sharedInt_t;
  52. static sharedInt_t numOnce = {0, {0}};
  53. static sharedInt_t numThreads = {0, {0}};
  54. void
  55. myfunc(void)
  56. {
  57. EnterCriticalSection(&numOnce.cs);
  58. numOnce.i++;
  59. LeaveCriticalSection(&numOnce.cs);
  60. /* Simulate slow once routine so that following threads pile up behind it */
  61. Sleep(100);
  62. }
  63. void *
  64. mythread(void * arg)
  65. {
  66. assert(pthread_once(&once[(int)(size_t)arg], myfunc) == 0);
  67. EnterCriticalSection(&numThreads.cs);
  68. numThreads.i++;
  69. LeaveCriticalSection(&numThreads.cs);
  70. return (void*)(size_t)0;
  71. }
  72. int
  73. main()
  74. {
  75. pthread_t t[NUM_THREADS][NUM_ONCE];
  76. int i, j;
  77. InitializeCriticalSection(&numThreads.cs);
  78. InitializeCriticalSection(&numOnce.cs);
  79. for (j = 0; j < NUM_ONCE; j++)
  80. {
  81. once[j] = o;
  82. for (i = 0; i < NUM_THREADS; i++)
  83. {
  84. /* GCC build: create was failing with EAGAIN after 790 threads */
  85. while (0 != pthread_create(&t[i][j], NULL, mythread, (void *)(size_t)j))
  86. sched_yield();
  87. }
  88. }
  89. for (j = 0; j < NUM_ONCE; j++)
  90. for (i = 0; i < NUM_THREADS; i++)
  91. if (pthread_join(t[i][j], NULL) != 0)
  92. printf("Join failed for [thread,once] = [%d,%d]\n", i, j);
  93. assert(numOnce.i == NUM_ONCE);
  94. assert(numThreads.i == NUM_THREADS * NUM_ONCE);
  95. DeleteCriticalSection(&numOnce.cs);
  96. DeleteCriticalSection(&numThreads.cs);
  97. return 0;
  98. }