1 /* 2 * File: reuse2.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: jschmidlapp@users.sourceforge.net 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: rpj@callisto.canberra.edu.au 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 * Test Synopsis: 43 * - Test that thread reuse works for detached threads. 44 * - Analyse thread struct reuse. 45 * 46 * Test Method (Validation or Falsification): 47 * - 48 * 49 * Requirements Tested: 50 * - 51 * 52 * Features Tested: 53 * - 54 * 55 * Cases Tested: 56 * - 57 * 58 * Description: 59 * - 60 * 61 * Environment: 62 * - This test is implementation specific 63 * because it uses knowledge of internals that should be 64 * opaque to an application. 65 * 66 * Input: 67 * - None. 68 * 69 * Output: 70 * - File name, Line number, and failed expression on failure. 71 * - No output on success. 72 * 73 * Assumptions: 74 * - 75 * 76 * Pass Criteria: 77 * - Process returns zero exit status. 78 * 79 * Fail Criteria: 80 * - Process returns non-zero exit status. 81 */ 82 83 #include "test.h" 84 85 #include "implement.h" 86 87 /* 88 */ 89 90 enum 91 { 92 NUMTHREADS = 100 93 }; 94 95 96 static int done = 0; 97 98 static void * func(void * arg) 99 { 100 sched_yield(); 101 102 PTE_ATOMIC_INCREMENT(&done); 103 104 return (void *) 0; 105 } 106 107 int pthread_test_reuse2() 108 { 109 pthread_t t[NUMTHREADS]; 110 pthread_attr_t attr; 111 int i; 112 unsigned int notUnique = 0, 113 totalHandles = 0, 114 reuseMax = 0, 115 reuseMin = NUMTHREADS; 116 117 assert(pthread_attr_init(&attr) == 0); 118 assert(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0); 119 120 for (i = 0; i < NUMTHREADS; i++) 121 { 122 assert(pthread_create(&t[i], &attr, func, NULL) == 0); 123 } 124 125 while (NUMTHREADS > PTE_ATOMIC_EXCHANGE_ADD(&done, 0L)) 126 pte_osThreadSleep(100); 127 128 pte_osThreadSleep(100); 129 130 /* 131 * Analyse reuse by computing min and max number of times pthread_create() 132 * returned the same pthread_t value. 133 */ 134 for (i = 0; i < NUMTHREADS; i++) 135 { 136 if (t[i].p != NULL) 137 { 138 unsigned int j, thisMax; 139 140 thisMax = t[i].x; 141 142 for (j = i+1; j < NUMTHREADS; j++) 143 if (t[i].p == t[j].p) 144 { 145 if (t[i].x == t[j].x) 146 notUnique++; 147 if (thisMax < t[j].x) 148 thisMax = t[j].x; 149 t[j].p = NULL; 150 } 151 152 if (reuseMin > thisMax) 153 reuseMin = thisMax; 154 155 if (reuseMax < thisMax) 156 reuseMax = thisMax; 157 } 158 } 159 160 for (i = 0; i < NUMTHREADS; i++) 161 if (t[i].p != NULL) 162 totalHandles++; 163 164 /* 165 * pthread_t reuse counts start at 0, so we need to add 1 166 * to the max and min values derived above. 167 */ 168 169 return 0; 170 } 171