[9594cdb] | 1 | /* |
---|
[60b791ad] | 2 | * COPYRIGHT (c) 1989-1998. |
---|
[abd604a] | 3 | * On-Line Applications Research Corporation (OAR). |
---|
[03f2154e] | 4 | * Copyright assigned to U.S. Government, 1994. |
---|
[abd604a] | 5 | * |
---|
[98e4ebf5] | 6 | * The license and distribution terms for this file may be |
---|
| 7 | * found in the file LICENSE in this distribution or at |
---|
[03f2154e] | 8 | * http://www.OARcorp.com/rtems/license.html. |
---|
[abd604a] | 9 | * |
---|
| 10 | * $Id$ |
---|
| 11 | */ |
---|
| 12 | |
---|
[0252200] | 13 | #define CONFIGURE_INIT |
---|
[abd604a] | 14 | #include "system.h" |
---|
[0a359a7] | 15 | #include <sched.h> |
---|
[abd604a] | 16 | |
---|
[be1c11ed] | 17 | void *POSIX_Init( |
---|
[0252200] | 18 | void *argument |
---|
[abd604a] | 19 | ) |
---|
| 20 | { |
---|
[e2ceeca7] | 21 | struct timespec tv; |
---|
| 22 | struct timespec tr; |
---|
[9594cdb] | 23 | int status; |
---|
[95645a4] | 24 | int priority; |
---|
[9594cdb] | 25 | pthread_t thread_id; |
---|
| 26 | time_t seconds; |
---|
[d60d9505] | 27 | time_t seconds1; |
---|
[e984c645] | 28 | time_t remaining; |
---|
[9594cdb] | 29 | struct tm tm; |
---|
[86fc81e] | 30 | |
---|
[0252200] | 31 | puts( "\n\n*** POSIX TEST 1 ***" ); |
---|
[86fc81e] | 32 | |
---|
[e2ceeca7] | 33 | build_time( &tm, TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
| 34 | |
---|
[c4f5e752] | 35 | /* error cases in clock_gettime and clock_settime */ |
---|
| 36 | |
---|
| 37 | puts( "Init: clock_gettime - EINVAL (invalid clockid)" ); |
---|
| 38 | status = clock_settime( -1, &tv ); |
---|
| 39 | assert( status == -1 ); |
---|
| 40 | assert( errno == EINVAL ); |
---|
| 41 | |
---|
| 42 | puts( "Init: clock_settime - EINVAL (invalid clockid)" ); |
---|
| 43 | status = clock_settime( -1, &tv ); |
---|
| 44 | assert( status == -1 ); |
---|
| 45 | assert( errno == EINVAL ); |
---|
| 46 | |
---|
[5da2ae8] | 47 | /* exercise clock_getres */ |
---|
| 48 | |
---|
| 49 | puts( "Init: clock_getres - EINVAL (invalid clockid)" ); |
---|
| 50 | status = clock_getres( -1, &tv ); |
---|
| 51 | assert( status == -1 ); |
---|
| 52 | assert( errno == EINVAL ); |
---|
| 53 | |
---|
| 54 | puts( "Init: clock_getres - EINVAL (NULL resolution)" ); |
---|
| 55 | status = clock_getres( CLOCK_REALTIME, NULL ); |
---|
| 56 | assert( status == -1 ); |
---|
| 57 | assert( errno == EINVAL ); |
---|
| 58 | |
---|
| 59 | puts( "Init: clock_getres - SUCCESSFUL" ); |
---|
| 60 | status = clock_getres( CLOCK_REALTIME, &tv ); |
---|
| 61 | printf( "Init: resolution = sec (%ld), nsec (%ld)\n", tv.tv_sec, tv.tv_nsec ); |
---|
| 62 | assert( !status ); |
---|
| 63 | |
---|
| 64 | /* set the time of day, and print our buffer in multiple ways */ |
---|
| 65 | |
---|
| 66 | tv.tv_sec = mktime( &tm ); |
---|
| 67 | assert( tv.tv_sec != -1 ); |
---|
| 68 | |
---|
| 69 | tv.tv_nsec = 0; |
---|
| 70 | |
---|
[c4f5e752] | 71 | /* now set the time of day */ |
---|
| 72 | |
---|
[5da2ae8] | 73 | empty_line(); |
---|
| 74 | |
---|
[c4f5e752] | 75 | printf( asctime( &tm ) ); |
---|
| 76 | puts( "Init: clock_settime - SUCCESSFUL" ); |
---|
[9594cdb] | 77 | status = clock_settime( CLOCK_REALTIME, &tv ); |
---|
| 78 | assert( !status ); |
---|
| 79 | |
---|
| 80 | printf( asctime( &tm ) ); |
---|
| 81 | printf( ctime( &tv.tv_sec ) ); |
---|
| 82 | |
---|
[19962562] | 83 | /* use sleep to delay */ |
---|
| 84 | |
---|
[e984c645] | 85 | remaining = sleep( 3 ); |
---|
| 86 | assert( !remaining ); |
---|
[19962562] | 87 | |
---|
| 88 | /* print new times to make sure it has changed and we can get the realtime */ |
---|
[9594cdb] | 89 | |
---|
| 90 | status = clock_gettime( CLOCK_REALTIME, &tv ); |
---|
| 91 | assert( !status ); |
---|
| 92 | |
---|
| 93 | printf( ctime( &tv.tv_sec ) ); |
---|
| 94 | |
---|
| 95 | seconds = time( NULL ); |
---|
| 96 | printf( ctime( &seconds ) ); |
---|
| 97 | |
---|
[d60d9505] | 98 | /* just to have the value copied out through the parameter */ |
---|
| 99 | |
---|
| 100 | seconds = time( &seconds1 ); |
---|
| 101 | assert( seconds == seconds1 ); |
---|
| 102 | |
---|
[e984c645] | 103 | /* check the time remaining */ |
---|
| 104 | |
---|
[644c0fa6] | 105 | printf( "Init: seconds remaining (%d)\n", (int)remaining ); |
---|
[e984c645] | 106 | assert( !remaining ); |
---|
| 107 | |
---|
[c4f5e752] | 108 | /* error cases in nanosleep */ |
---|
| 109 | |
---|
[5da2ae8] | 110 | empty_line(); |
---|
[c4f5e752] | 111 | puts( "Init: nanosleep - EINVAL (NULL time)" ); |
---|
| 112 | status = nanosleep ( NULL, &tr ); |
---|
| 113 | assert( status == -1 ); |
---|
| 114 | assert( errno == EINVAL ); |
---|
| 115 | |
---|
| 116 | tv.tv_sec = 0; |
---|
| 117 | tv.tv_nsec = TOD_NANOSECONDS_PER_SECOND * 2; |
---|
| 118 | puts( "Init: nanosleep - EINVAL (too many nanoseconds)" ); |
---|
| 119 | status = nanosleep ( &tv, &tr ); |
---|
| 120 | assert( status == -1 ); |
---|
| 121 | assert( errno == EINVAL ); |
---|
| 122 | |
---|
[f254b93f] | 123 | /* this is actually a small delay or yield */ |
---|
| 124 | tv.tv_sec = -1; |
---|
| 125 | tv.tv_nsec = 0; |
---|
| 126 | puts( "Init: nanosleep - negative seconds small delay " ); |
---|
| 127 | status = nanosleep ( &tv, &tr ); |
---|
| 128 | assert( !status ); |
---|
| 129 | |
---|
[c4f5e752] | 130 | /* use nanosleep to yield */ |
---|
| 131 | |
---|
| 132 | tv.tv_sec = 0; |
---|
| 133 | tv.tv_nsec = 0; |
---|
| 134 | |
---|
| 135 | puts( "Init: nanosleep - yield" ); |
---|
| 136 | status = nanosleep ( &tv, &tr ); |
---|
| 137 | assert( !status ); |
---|
| 138 | assert( !tr.tv_sec ); |
---|
| 139 | assert( !tr.tv_nsec ); |
---|
| 140 | |
---|
[19962562] | 141 | /* use nanosleep to delay */ |
---|
| 142 | |
---|
| 143 | tv.tv_sec = 3; |
---|
| 144 | tv.tv_nsec = 500000; |
---|
| 145 | |
---|
[c4f5e752] | 146 | puts( "Init: nanosleep - 3.05 seconds" ); |
---|
[19962562] | 147 | status = nanosleep ( &tv, &tr ); |
---|
| 148 | assert( !status ); |
---|
| 149 | |
---|
| 150 | /* print the current real time again */ |
---|
| 151 | |
---|
| 152 | status = clock_gettime( CLOCK_REALTIME, &tv ); |
---|
| 153 | assert( !status ); |
---|
| 154 | |
---|
| 155 | printf( ctime( &tv.tv_sec ) ); |
---|
| 156 | |
---|
| 157 | /* check the time remaining */ |
---|
| 158 | |
---|
[644c0fa6] | 159 | printf( "Init: sec (%ld), nsec (%ld) remaining\n", tr.tv_sec, tr.tv_nsec ); |
---|
[19962562] | 160 | assert( !tr.tv_sec && !tr.tv_nsec ); |
---|
| 161 | |
---|
[15974b65] | 162 | /* get id of this thread */ |
---|
| 163 | |
---|
| 164 | Init_id = pthread_self(); |
---|
[644c0fa6] | 165 | printf( "Init: ID is 0x%08x\n", Init_id ); |
---|
[15974b65] | 166 | |
---|
[f643e230] | 167 | /* exercise get minimum priority */ |
---|
[95645a4] | 168 | |
---|
| 169 | priority = sched_get_priority_min( SCHED_FIFO ); |
---|
[f643e230] | 170 | printf( "Init: sched_get_priority_min (SCHED_FIFO) -- %d\n", priority ); |
---|
[95645a4] | 171 | assert( priority != -1 ); |
---|
| 172 | |
---|
[f643e230] | 173 | puts( "Init: sched_get_priority_min -- EINVAL (invalid policy)" ); |
---|
| 174 | priority = sched_get_priority_min( -1 ); |
---|
| 175 | assert( priority == -1 ); |
---|
| 176 | assert( errno == EINVAL ); |
---|
| 177 | |
---|
| 178 | /* exercise get maximum priority */ |
---|
[95645a4] | 179 | |
---|
| 180 | priority = sched_get_priority_max( SCHED_FIFO ); |
---|
[f643e230] | 181 | printf( "Init: sched_get_priority_max (SCHED_FIFO) -- %d\n", priority ); |
---|
[95645a4] | 182 | assert( priority != -1 ); |
---|
| 183 | |
---|
[f643e230] | 184 | puts( "Init: sched_get_priority_min -- EINVAL (invalid policy)" ); |
---|
| 185 | priority = sched_get_priority_min( -1 ); |
---|
| 186 | assert( priority == -1 ); |
---|
| 187 | assert( errno == EINVAL ); |
---|
| 188 | |
---|
[95645a4] | 189 | /* print the round robin time quantum */ |
---|
| 190 | |
---|
| 191 | status = sched_rr_get_interval( getpid(), &tr ); |
---|
| 192 | printf( |
---|
[644c0fa6] | 193 | "Init: Round Robin quantum is %ld seconds, %ld nanoseconds\n", |
---|
| 194 | tr.tv_sec, |
---|
| 195 | tr.tv_nsec |
---|
[95645a4] | 196 | ); |
---|
| 197 | assert( !status ); |
---|
| 198 | |
---|
[9594cdb] | 199 | /* create a thread */ |
---|
[86fc81e] | 200 | |
---|
[f643e230] | 201 | puts( "Init: pthread_create - SUCCESSFUL" ); |
---|
| 202 | status = pthread_create( &thread_id, NULL, Task_1_through_3, NULL ); |
---|
| 203 | assert( !status ); |
---|
| 204 | |
---|
| 205 | /* too may threads error */ |
---|
| 206 | |
---|
[fdf6917] | 207 | puts( "Init: pthread_create - EAGAIN (too many threads)" ); |
---|
[2197a2e1] | 208 | status = pthread_create( &thread_id, NULL, Task_1_through_3, NULL ); |
---|
[fdf6917] | 209 | assert( status == EAGAIN ); |
---|
[f643e230] | 210 | |
---|
| 211 | puts( "Init: sched_yield to Task_1" ); |
---|
| 212 | status = sched_yield(); |
---|
[2197a2e1] | 213 | assert( !status ); |
---|
| 214 | |
---|
[f643e230] | 215 | /* switch to Task_1 */ |
---|
| 216 | |
---|
[9594cdb] | 217 | /* exit this thread */ |
---|
| 218 | |
---|
[f643e230] | 219 | puts( "Init: pthread_exit" ); |
---|
[0252200] | 220 | pthread_exit( NULL ); |
---|
[2197a2e1] | 221 | |
---|
[f643e230] | 222 | /* switch to Task_1 */ |
---|
| 223 | |
---|
[893103c] | 224 | return NULL; /* just so the compiler thinks we returned something */ |
---|
[abd604a] | 225 | } |
---|