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: rpj@callisto.canberra.edu.au
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
44 #include "test.h"
45
46 #define NUM_THREADS 100 /* Targeting each once control */
47 #define NUM_ONCE 10
48
49 pthread_once_t o = PTHREAD_ONCE_INIT;
50 pthread_once_t once[NUM_ONCE];
51
52 typedef struct {
53 int i;
54 CRITICAL_SECTION cs;
55 } sharedInt_t;
56
57 static sharedInt_t numOnce = {0, {0}};
58 static sharedInt_t numThreads = {0, {0}};
59
60 void
myfunc(void)61 myfunc(void)
62 {
63 EnterCriticalSection(&numOnce.cs);
64 numOnce.i++;
65 LeaveCriticalSection(&numOnce.cs);
66 /* Simulate slow once routine so that following threads pile up behind it */
67 Sleep(100);
68 }
69
70 void *
mythread(void * arg)71 mythread(void * arg)
72 {
73 assert(pthread_once(&once[(int) (size_t) arg], myfunc) == 0);
74 EnterCriticalSection(&numThreads.cs);
75 numThreads.i++;
76 LeaveCriticalSection(&numThreads.cs);
77 return 0;
78 }
79
80 int
main()81 main()
82 {
83 pthread_t t[NUM_THREADS][NUM_ONCE];
84 int i, j;
85
86 InitializeCriticalSection(&numThreads.cs);
87 InitializeCriticalSection(&numOnce.cs);
88
89 for (j = 0; j < NUM_ONCE; j++)
90 {
91 once[j] = o;
92
93 for (i = 0; i < NUM_THREADS; i++)
94 { int rslt1;
95 rslt1 = pthread_create(&t[i][j], NULL, mythread, (void *) (size_t) j);
96 if (rslt1 == EAGAIN)
97 {
98 Sleep (0);
99 i--;
100 continue;
101 }
102 if (rslt1 != 0)
103 {
104 fprintf (stderr, "%d has result %d (EAGAIN:%d)\n", j, rslt1, EAGAIN);
105 assert (rslt1 != 0);
106 }
107 }
108 }
109
110 for (j = 0; j < NUM_ONCE; j++)
111 for (i = 0; i < NUM_THREADS; i++)
112 if (pthread_join(t[i][j], NULL) != 0)
113 printf("Join failed for [thread,once] = [%d,%d]\n", i, j);
114
115 assert(numOnce.i == NUM_ONCE);
116 assert(numThreads.i == NUM_THREADS * NUM_ONCE);
117
118 DeleteCriticalSection(&numOnce.cs);
119 DeleteCriticalSection(&numThreads.cs);
120
121 return 0;
122 }
123