[13642cbc] | 1 | /* |
---|
| 2 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
| 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * All rights assigned to U.S. Government, 1994. |
---|
| 5 | * |
---|
| 6 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
| 7 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
| 8 | * notice must appear in all copies of this file and its derivatives. |
---|
| 9 | * |
---|
| 10 | * $Id$ |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | #define CONFIGURE_INIT |
---|
| 14 | #include "system.h" |
---|
| 15 | #include <sched.h> |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | void *POSIX_Init( |
---|
| 19 | void *argument |
---|
| 20 | ) |
---|
| 21 | { |
---|
| 22 | int status; |
---|
| 23 | pthread_condattr_t attr; |
---|
[520949bf] | 24 | pthread_condattr_t attr_error; |
---|
[13642cbc] | 25 | int pshared; |
---|
[c9c94387] | 26 | pthread_cond_t cond; |
---|
| 27 | struct timespec timeout; |
---|
[13642cbc] | 28 | |
---|
| 29 | puts( "\n\n*** POSIX TEST 10 ***" ); |
---|
| 30 | |
---|
| 31 | puts( "Init: pthread_condattr_init" ); |
---|
| 32 | status = pthread_condattr_init( &attr ); |
---|
| 33 | assert( !status ); |
---|
| 34 | |
---|
[520949bf] | 35 | puts( "Init: pthread_condattr_init - EINVAL (attribute invalid)" ); |
---|
[13642cbc] | 36 | status = pthread_condattr_init( NULL ); |
---|
| 37 | if ( status != EINVAL ) |
---|
| 38 | printf( "status = %d\n", status ); |
---|
| 39 | assert( status == EINVAL ); |
---|
| 40 | |
---|
| 41 | puts( "Init: pthread_condattr_destroy" ); |
---|
| 42 | status = pthread_condattr_destroy( &attr ); |
---|
| 43 | assert( !status ); |
---|
| 44 | |
---|
[520949bf] | 45 | puts( "Init: pthread_condattr_destroy - EINVAL (attribute invalid)" ); |
---|
[13642cbc] | 46 | status = pthread_condattr_destroy( NULL ); |
---|
| 47 | if ( status != EINVAL ) |
---|
| 48 | printf( "status = %d\n", status ); |
---|
| 49 | assert( status == EINVAL ); |
---|
| 50 | |
---|
[0cd15947] | 51 | puts( "Init: pthread_condattr_init" ); |
---|
| 52 | status = pthread_condattr_init( &attr ); |
---|
| 53 | assert( !status ); |
---|
| 54 | |
---|
[13642cbc] | 55 | puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_SHARED" ); |
---|
| 56 | status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
| 57 | assert( !status ); |
---|
| 58 | |
---|
| 59 | puts( "Init: pthread_condattr_setpshared - PTHREAD_PROCESS_PRIVATE" ); |
---|
| 60 | status = pthread_condattr_setpshared( &attr, PTHREAD_PROCESS_PRIVATE ); |
---|
| 61 | assert( !status ); |
---|
| 62 | |
---|
| 63 | status = pthread_condattr_setpshared( NULL, PTHREAD_PROCESS_PRIVATE ); |
---|
| 64 | if ( status != EINVAL ) |
---|
| 65 | printf( "status = %d\n", status ); |
---|
| 66 | assert( status == EINVAL ); |
---|
[520949bf] | 67 | puts( "Init: pthread_condattr_setpshared - EINVAL (attribute invalid)" ); |
---|
[13642cbc] | 68 | |
---|
| 69 | status = pthread_condattr_setpshared( &attr, 0xFFFFFF ); |
---|
| 70 | if ( status != EINVAL ) |
---|
| 71 | printf( "status = %d\n", status ); |
---|
| 72 | assert( status == EINVAL ); |
---|
[520949bf] | 73 | puts( "Init: pthread_condattr_setpshared - EINVAL (pshared invalid)" ); |
---|
[13642cbc] | 74 | |
---|
| 75 | status = pthread_condattr_getpshared( &attr, &pshared ); |
---|
| 76 | assert( !status ); |
---|
[695dd0b] | 77 | printf( "Init: pthread_condattr_getpshared - %d\n", pshared ); |
---|
[13642cbc] | 78 | |
---|
| 79 | status = pthread_condattr_getpshared( NULL, &pshared ); |
---|
| 80 | if ( status != EINVAL ) |
---|
| 81 | printf( "status = %d\n", status ); |
---|
| 82 | assert( status == EINVAL ); |
---|
[520949bf] | 83 | puts( "Init: pthread_condattr_getpshared - EINVAL (attribute invalid)" ); |
---|
[13642cbc] | 84 | |
---|
[5263668] | 85 | puts( "Init: pthread_cond_init - NULL attr" ); |
---|
| 86 | status = pthread_cond_init( &cond, NULL ); |
---|
| 87 | assert( !status ); |
---|
| 88 | |
---|
[520949bf] | 89 | /* error for attribute not initialized */ |
---|
| 90 | |
---|
| 91 | status = pthread_cond_init( &cond, &attr_error ); |
---|
| 92 | if ( status != EINVAL ) |
---|
| 93 | printf( "status = %d\n", status ); |
---|
| 94 | assert( status == EINVAL ); |
---|
| 95 | puts( "Init: pthread_cond_init - EINVAL (attr not initialized)" ); |
---|
| 96 | |
---|
| 97 | status = pthread_cond_init( &cond, NULL ); |
---|
| 98 | if ( status != ENOMEM ) |
---|
| 99 | printf( "status = %d\n", status ); |
---|
| 100 | assert( status == ENOMEM ); |
---|
| 101 | puts( "Init: pthread_cond_init - ENOMEM (too many conds)" ); |
---|
| 102 | |
---|
[5263668] | 103 | puts( "Init: pthread_cond_destroy" ); |
---|
| 104 | status = pthread_cond_destroy( &cond ); |
---|
| 105 | assert( !status ); |
---|
| 106 | |
---|
[520949bf] | 107 | /* error for bad condition variable passed */ |
---|
| 108 | |
---|
| 109 | status = pthread_cond_destroy( NULL ); |
---|
| 110 | if ( status != EINVAL ) |
---|
| 111 | printf( "status = %d\n", status ); |
---|
| 112 | assert( status == EINVAL ); |
---|
| 113 | puts( "Init: pthread_cond_destroy - EINVAL (cond invalid)" ); |
---|
| 114 | |
---|
[c9c94387] | 115 | /* initiailize the attribute for the rest of the test */ |
---|
| 116 | |
---|
[0cd15947] | 117 | puts( "Init: pthread_cond_init - attr" ); |
---|
[aad3fe6] | 118 | status = pthread_cond_init( &Cond1_id, &attr ); |
---|
[0cd15947] | 119 | assert( !status ); |
---|
| 120 | |
---|
[c9c94387] | 121 | /* signal task1 with a condition variable */ |
---|
[13642cbc] | 122 | |
---|
[aad3fe6] | 123 | empty_line(); |
---|
| 124 | |
---|
| 125 | status = pthread_create( &Task_id, NULL, Task_1, NULL ); |
---|
| 126 | assert( !status ); |
---|
| 127 | |
---|
[c9c94387] | 128 | /* switch to task1 to allow it to wait for a condition variable */ |
---|
| 129 | |
---|
[aad3fe6] | 130 | puts( "Init: sleep to switch to Task_1" ); |
---|
| 131 | sleep( 1 ); |
---|
| 132 | |
---|
[520949bf] | 133 | status = pthread_cond_destroy( &Cond1_id ); |
---|
| 134 | if ( status != EBUSY ) |
---|
| 135 | printf( "status = %d\n", status ); |
---|
| 136 | assert( status == EBUSY ); |
---|
| 137 | puts( "Init: pthread_cond_destroy - EBUSY (task1 waiting)" ); |
---|
| 138 | |
---|
[aad3fe6] | 139 | puts( "Init: pthread_cond_signal" ); |
---|
| 140 | status = pthread_cond_signal( &Cond1_id ); |
---|
| 141 | assert( !status ); |
---|
| 142 | |
---|
| 143 | empty_line(); |
---|
| 144 | |
---|
| 145 | status = pthread_create( &Task2_id, NULL, Task_2, NULL ); |
---|
| 146 | assert( !status ); |
---|
| 147 | |
---|
[c9c94387] | 148 | /* switch to task1 and task2 to allow them to wait for broadcast signal */ |
---|
| 149 | |
---|
[aad3fe6] | 150 | puts( "Init: sleep - switch to Task_1 and Task_2" ); |
---|
| 151 | sleep( 1 ); |
---|
| 152 | |
---|
[c9c94387] | 153 | /* broadcast a condition variable to task1 and task2 */ |
---|
| 154 | |
---|
[aad3fe6] | 155 | puts( "Init: pthread_cond_broadcast" ); |
---|
| 156 | status = pthread_cond_broadcast( &Cond1_id ); |
---|
[13642cbc] | 157 | assert( !status ); |
---|
[aad3fe6] | 158 | |
---|
| 159 | puts( "Init: sleep - switch to Task_1" ); |
---|
[c9c94387] | 160 | sleep( 0 ); |
---|
| 161 | |
---|
| 162 | /* timedwait case - timeout */ |
---|
| 163 | |
---|
| 164 | status = pthread_mutex_lock( &Mutex_id ); |
---|
| 165 | assert( !status ); |
---|
| 166 | |
---|
| 167 | /* set timeout to 3 seconds */ |
---|
| 168 | |
---|
| 169 | timeout.tv_sec = 3; |
---|
| 170 | |
---|
| 171 | puts( "Init: pthread_cond_timedwait for 3 seconds" ); |
---|
| 172 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
| 173 | if ( status != ETIMEDOUT ) |
---|
| 174 | printf( "status = %d\n", status ); |
---|
| 175 | assert( status == ETIMEDOUT ); |
---|
[520949bf] | 176 | puts( "Init: pthread_cond_timedwait - ETIMEDOUT - (mutex not acquired)" ); |
---|
| 177 | |
---|
| 178 | /* remaining error messages */ |
---|
| 179 | |
---|
| 180 | empty_line(); |
---|
[13642cbc] | 181 | |
---|
[520949bf] | 182 | /* errors for bad variable passed */ |
---|
| 183 | |
---|
| 184 | status = pthread_cond_signal( NULL ); |
---|
| 185 | if ( status != EINVAL ) |
---|
| 186 | printf( "status = %d\n", status ); |
---|
| 187 | assert( status == EINVAL ); |
---|
| 188 | puts( "Init: pthread_cond_signal - EINVAL (cond invalid)" ); |
---|
| 189 | |
---|
| 190 | status = pthread_cond_broadcast( NULL ); |
---|
| 191 | if ( status != EINVAL ) |
---|
| 192 | printf( "status = %d\n", status ); |
---|
| 193 | assert( status == EINVAL ); |
---|
| 194 | puts( "Init: pthread_cond_broadcast - EINVAL (cond invalid)" ); |
---|
| 195 | |
---|
| 196 | /* acquire mutex so errors will occur */ |
---|
| 197 | |
---|
| 198 | status = pthread_mutex_lock( &Mutex_id ); |
---|
| 199 | assert( !status ); |
---|
| 200 | |
---|
| 201 | status = pthread_cond_wait( NULL, &Mutex_id ); |
---|
| 202 | if ( status != EINVAL ) |
---|
| 203 | printf( "status = %d\n", status ); |
---|
| 204 | assert( status == EINVAL ); |
---|
| 205 | puts( "Init: pthread_cond_wait - EINVAL (cond invalid)" ); |
---|
| 206 | |
---|
| 207 | status = pthread_cond_timedwait( NULL, &Mutex_id, &timeout ); |
---|
| 208 | if ( status != EINVAL ) |
---|
| 209 | printf( "status = %d\n", status ); |
---|
| 210 | assert( status == EINVAL ); |
---|
| 211 | puts( "Init: pthread_cond_timedwait - EINVAL (cond invalid)" ); |
---|
| 212 | |
---|
| 213 | status = pthread_cond_wait( &Cond1_id, NULL ); |
---|
| 214 | if ( status != EINVAL ) |
---|
| 215 | printf( "status = %d\n", status ); |
---|
| 216 | assert( status == EINVAL ); |
---|
| 217 | puts( "Init: pthread_cond_wait - EINVAL (mutex invalid)" ); |
---|
| 218 | |
---|
| 219 | status = pthread_cond_timedwait( &Cond1_id, NULL, &timeout ); |
---|
| 220 | if ( status != EINVAL ) |
---|
| 221 | printf( "status = %d\n", status ); |
---|
| 222 | assert( status == EINVAL ); |
---|
| 223 | puts( "Init: pthread_cond_timedwait - EINVAL (mutex invalid)" ); |
---|
| 224 | |
---|
| 225 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, NULL ); |
---|
| 226 | if ( status != EINVAL ) |
---|
| 227 | printf( "status = %d\n", status ); |
---|
| 228 | assert( status == EINVAL ); |
---|
| 229 | puts( "Init: pthread_cond_timedwait - EINVAL (abstime NULL)" ); |
---|
| 230 | |
---|
| 231 | timeout.tv_sec = -1; |
---|
| 232 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
| 233 | if ( status != EINVAL ) |
---|
| 234 | printf( "status = %d\n", status ); |
---|
| 235 | assert( status == EINVAL ); |
---|
| 236 | puts( "Init: pthread_cond_timedwait - EINVAL (abstime->tv_sec invalid)" ); |
---|
| 237 | |
---|
| 238 | timeout.tv_sec = 2; |
---|
| 239 | timeout.tv_nsec = -1; |
---|
| 240 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
| 241 | if ( status != EINVAL ) |
---|
| 242 | printf( "status = %d\n", status ); |
---|
| 243 | assert( status == EINVAL ); |
---|
| 244 | puts( "Init: pthread_cond_timedwait - EINVAL (abstime->tv_nsec invalid)" ); |
---|
| 245 | |
---|
| 246 | timeout.tv_sec = 2; |
---|
| 247 | timeout.tv_nsec = 2; |
---|
| 248 | timeout.tv_nsec = 0x7FFFFFFF; |
---|
| 249 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
| 250 | if ( status != EINVAL ) |
---|
| 251 | printf( "status = %d\n", status ); |
---|
| 252 | assert( status == EINVAL ); |
---|
| 253 | puts( "Init: pthread_cond_timedwait - EINVAL (abstime->tv_nsec to large)" ); |
---|
| 254 | |
---|
| 255 | /* unlock mutex for rest of test */ |
---|
| 256 | |
---|
| 257 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
| 258 | assert( !status ); |
---|
| 259 | |
---|
| 260 | /* wait and timedwait without mutex */ |
---|
| 261 | |
---|
| 262 | status = pthread_cond_wait( &Cond1_id, &Mutex_id ); |
---|
| 263 | if ( status != EINVAL ) |
---|
| 264 | printf( "status = %d\n", status ); |
---|
| 265 | assert( status == EINVAL ); |
---|
| 266 | puts( "Init: pthread_cond_wait - EINVAL (mutex not locked before call)" ); |
---|
| 267 | |
---|
| 268 | status = pthread_cond_timedwait( &Cond1_id, &Mutex_id, &timeout ); |
---|
| 269 | if ( status != EINVAL ) |
---|
| 270 | printf( "status = %d\n", status ); |
---|
| 271 | assert( status == EINVAL ); |
---|
| 272 | puts( "Init: pthread_cond_timedwait - EINVAL (mutex not locked before call)"); |
---|
| 273 | |
---|
| 274 | empty_line(); |
---|
| 275 | |
---|
| 276 | status = pthread_create( &Task3_id, NULL, Task_3, NULL ); |
---|
| 277 | assert( !status ); |
---|
| 278 | |
---|
| 279 | /* switch to task3 to allow it to wait for broadcast signal */ |
---|
| 280 | |
---|
| 281 | puts( "Init: sleep - switch to Task_3" ); |
---|
| 282 | sleep( 1 ); |
---|
| 283 | |
---|
| 284 | /* destroy the mutex so Task3 can not acguire at the end of Wait_support */ |
---|
| 285 | |
---|
| 286 | status = pthread_mutex_destroy( &Mutex_id ); |
---|
| 287 | assert( !status ); |
---|
| 288 | |
---|
| 289 | /* signal a condition variable to task3 */ |
---|
| 290 | |
---|
| 291 | puts( "Init: pthread_cond_signal" ); |
---|
| 292 | status = pthread_cond_signal( &Cond1_id ); |
---|
| 293 | |
---|
| 294 | puts( "Init: sleep - switch to Task_3" ); |
---|
| 295 | sleep( 1 ); |
---|
[13642cbc] | 296 | |
---|
[695dd0b] | 297 | puts( "*** END OF POSIX TEST 5 ***" ); |
---|
| 298 | exit( 0 ); |
---|
[13642cbc] | 299 | |
---|
| 300 | return NULL; /* just so the compiler thinks we returned something */ |
---|
| 301 | } |
---|