• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "test.h"
2 
3 enum {
4   NUMTHREADS = 10000
5 };
6 
7 
8 static long done = 0;
9 
func(void * arg)10 void * func(void * arg)
11 {
12   sched_yield();
13 
14   InterlockedIncrement(&done);
15 
16   return (void *) 0;
17 }
18 
19 int
main()20 main()
21 {
22   pthread_t t[NUMTHREADS];
23   pthread_attr_t attr;
24   int i;
25   unsigned int notUnique = 0,
26 	       totalHandles = 0,
27 	       reuseMax = 0,
28 	       reuseMin = NUMTHREADS;
29 
30   assert(pthread_attr_init(&attr) == 0);
31   assert(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0);
32 
33   for (i = 0; i < NUMTHREADS; i++)
34     {
35       assert(pthread_create(&t[i], &attr, func, NULL) == 0);
36     }
37 
38   while (NUMTHREADS > InterlockedExchangeAdd((LPLONG)&done, 0L))
39     Sleep(100);
40 
41   Sleep(100);
42 
43   /*
44    * Analyse reuse by computing min and max number of times pthread_create()
45    * returned the same pthread_t value.
46    */
47   for (i = 0; i < NUMTHREADS; i++)
48     {
49       if (t[i])
50         {
51           unsigned int j, thisMax;
52 
53           thisMax = t[i];
54 
55           for (j = i+1; j < NUMTHREADS; j++)
56             if (__pth_gpointer_locked (t[i]) == __pth_gpointer_locked (t[j]))
57               {
58 		if (t[i] == t[j])
59 		  notUnique++;
60                 if (thisMax < t[j])
61                   thisMax = t[j];
62                 t[j] = 0;
63               }
64 
65           if (reuseMin > thisMax)
66             reuseMin = thisMax;
67 
68           if (reuseMax < thisMax)
69             reuseMax = thisMax;
70         }
71     }
72 
73   for (i = 0; i < NUMTHREADS; i++)
74     if (t[i] != 0)
75       totalHandles++;
76 
77   /*
78    * pthread_t reuse counts start at 0, so we need to add 1
79    * to the max and min values derived above.
80    */
81   printf("For %d total threads:\n", NUMTHREADS);
82   printf("Non-unique IDs = %d\n", notUnique);
83   printf("Reuse maximum  = %d\n", reuseMax + 1);
84   printf("Reuse minimum  = %d\n", reuseMin + 1);
85   printf("Total handles  = %d\n", totalHandles);
86 
87   return 0;
88 }
89