[a3e4a58] | 1 | /* |
---|
| 2 | * COPYRIGHT (c) 1989-2009. |
---|
| 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * |
---|
| 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
| 7 | * http://www.rtems.com/license/LICENSE. |
---|
| 8 | * |
---|
| 9 | * $Id$ |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | #include <pmacros.h> |
---|
| 14 | #include <signal.h> |
---|
| 15 | #include <errno.h> |
---|
| 16 | #include <pthread.h> |
---|
| 17 | #include <sched.h> |
---|
| 18 | |
---|
| 19 | volatile bool Signal_occurred; |
---|
| 20 | volatile pthread_t Signal_thread; |
---|
| 21 | |
---|
| 22 | void Signal_handler( |
---|
| 23 | int signo |
---|
| 24 | ) |
---|
| 25 | { |
---|
| 26 | Signal_occurred = true; |
---|
| 27 | Signal_thread = pthread_self(); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | void Install_Signal_Handler( |
---|
| 31 | const char *task_name |
---|
| 32 | ) |
---|
| 33 | { |
---|
| 34 | int sc; |
---|
| 35 | sigset_t mask; |
---|
| 36 | |
---|
| 37 | sc = sigemptyset( &mask ); |
---|
| 38 | assert( !sc ); |
---|
| 39 | |
---|
| 40 | sc = sigaddset( &mask, SIGUSR1 ); |
---|
| 41 | assert( !sc ); |
---|
| 42 | |
---|
| 43 | printf( "%s - Unblock SIGUSR1\n", task_name ); |
---|
| 44 | sc = pthread_sigmask( SIG_UNBLOCK, &mask, NULL ); |
---|
| 45 | assert( !sc ); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /* |
---|
| 49 | |
---|
| 50 | Tasks and actions, created in this order, all interested in SIGUSR1 |
---|
| 51 | - 20 - interested, suspend? |
---|
| 52 | - 18 - interested, suspend? |
---|
| 53 | - 16 - interested, spins |
---|
| 54 | - 14 - interested, spins |
---|
| 55 | - 12 - interested, sleeps |
---|
| 56 | - 10 - interested, suspends |
---|
| 57 | - 8 - interested, sleeps |
---|
| 58 | |
---|
| 59 | Order is critical because the algorithm works by thread index |
---|
| 60 | */ |
---|
| 61 | |
---|
| 62 | typedef enum { |
---|
| 63 | SUSPEND, |
---|
| 64 | SPIN, |
---|
| 65 | SLEEP |
---|
| 66 | } Action_t; |
---|
| 67 | |
---|
| 68 | const char *Actions[] = { |
---|
| 69 | "Suspends self", |
---|
| 70 | "Spins", |
---|
| 71 | "Sleeps" |
---|
| 72 | }; |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | typedef struct { |
---|
| 76 | int priority; |
---|
| 77 | Action_t action; |
---|
| 78 | const char *name; |
---|
| 79 | } Test_t; |
---|
| 80 | |
---|
| 81 | Test_t Threads[] = { |
---|
| 82 | { 8, SUSPEND, "P8" }, /* base priority */ |
---|
| 83 | { 7, SUSPEND, "P7" }, /* lower priority -- no change */ |
---|
| 84 | { 12, SUSPEND, "P12" }, /* higher priority, blocked */ |
---|
| 85 | { 12, SUSPEND, "P12" }, /* equal priority, blocked */ |
---|
| 86 | { 12, SLEEP, "P12" }, /* equal priority, interruptible */ |
---|
| 87 | { 12, SLEEP, "P12" }, /* equal priority, interruptible */ |
---|
| 88 | { 12, SPIN, "P12" }, /* equal priority, ready */ |
---|
| 89 | { 12, SPIN, "P12" }, /* equal priority, ready -- no change */ |
---|
| 90 | { -1, 0, "" }, |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | void *Test_Thread(void *arg) |
---|
| 94 | { |
---|
| 95 | Test_t *test = (Test_t *)arg; |
---|
| 96 | |
---|
| 97 | Install_Signal_Handler( test->name ); |
---|
| 98 | |
---|
| 99 | printf( "%s - %s\n", test->name, Actions[test->action] ); |
---|
| 100 | switch ( test->action ) { |
---|
| 101 | case SUSPEND: |
---|
| 102 | (void) rtems_task_suspend( RTEMS_SELF ); |
---|
| 103 | break; |
---|
| 104 | case SPIN: |
---|
| 105 | while (1) ; |
---|
| 106 | break; |
---|
| 107 | case SLEEP: |
---|
| 108 | sleep( 30 ); |
---|
| 109 | break; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | printf( "%s - exiting\n", test->name ); |
---|
| 113 | return NULL; |
---|
| 114 | |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void *POSIX_Init( |
---|
| 118 | void *argument |
---|
| 119 | ) |
---|
| 120 | { |
---|
| 121 | int i; |
---|
| 122 | int sc; |
---|
| 123 | pthread_t id; |
---|
| 124 | pthread_attr_t attr; |
---|
| 125 | struct sched_param param; |
---|
| 126 | Test_t *test; |
---|
[a2b8140] | 127 | struct sigaction act; |
---|
[ea7d86b] | 128 | struct timespec delay_request; |
---|
[a3e4a58] | 129 | |
---|
| 130 | puts( "\n\n*** POSIX TEST SIGNAL 02 ***" ); |
---|
| 131 | |
---|
| 132 | Signal_occurred = false; |
---|
| 133 | |
---|
| 134 | act.sa_handler = Signal_handler; |
---|
| 135 | act.sa_flags = 0; |
---|
| 136 | sigaction( SIGUSR1, &act, NULL ); |
---|
| 137 | |
---|
| 138 | puts( "Init - Raise my priority" ); |
---|
| 139 | sc = pthread_attr_init( &attr ); |
---|
| 140 | assert( !sc ); |
---|
| 141 | |
---|
| 142 | param.sched_priority = 30; |
---|
| 143 | sc = pthread_setschedparam( pthread_self(), SCHED_RR, ¶m ); |
---|
| 144 | assert( !sc ); |
---|
| 145 | |
---|
| 146 | for ( i=0, test=Threads ; test->priority != -1 ; i++, test++ ) { |
---|
| 147 | printf( "Init - Create thread %d, priority=%d\n", i, test->priority ); |
---|
| 148 | sc = pthread_attr_init( &attr ); |
---|
| 149 | assert( !sc ); |
---|
| 150 | |
---|
| 151 | sc = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED ); |
---|
| 152 | assert( !sc ); |
---|
| 153 | |
---|
| 154 | sc = pthread_attr_setschedpolicy( &attr, SCHED_RR ); |
---|
| 155 | assert( !sc ); |
---|
| 156 | |
---|
| 157 | param.sched_priority = test->priority; |
---|
| 158 | sc = pthread_attr_setschedparam( &attr, ¶m ); |
---|
| 159 | assert( !sc ); |
---|
| 160 | |
---|
| 161 | sc = pthread_create( &id, &attr, Test_Thread, test ); |
---|
| 162 | assert( !sc ); |
---|
| 163 | |
---|
| 164 | puts( "Init - sleep - let thread settle - OK" ); |
---|
[ea7d86b] | 165 | delay_request.tv_sec = 0; |
---|
| 166 | delay_request.tv_nsec = 50000000; |
---|
| 167 | sc = nanosleep( &delay_request, NULL ); |
---|
| 168 | assert( !sc ); |
---|
[a3e4a58] | 169 | } |
---|
| 170 | |
---|
| 171 | puts( "Init - sending SIGUSR1" ); |
---|
| 172 | sc = kill( getpid(), SIGUSR1 ); |
---|
| 173 | assert( !sc ); |
---|
| 174 | |
---|
| 175 | /* we are just scheduling the signal, not delivering it */ |
---|
| 176 | assert( Signal_occurred == false ); |
---|
| 177 | |
---|
| 178 | puts( "*** END OF POSIX TEST SIGNAL 02 ***" ); |
---|
| 179 | rtems_test_exit(0); |
---|
| 180 | |
---|
| 181 | return NULL; /* just so the compiler thinks we returned something */ |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /* configuration information */ |
---|
| 185 | |
---|
| 186 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
| 187 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
| 188 | |
---|
| 189 | #define CONFIGURE_MICROSECONDS_PER_TICK 1000 |
---|
| 190 | #define CONFIGURE_MAXIMUM_POSIX_THREADS 9 |
---|
| 191 | |
---|
| 192 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
| 193 | |
---|
| 194 | #define CONFIGURE_INIT |
---|
| 195 | #include <rtems/confdefs.h> |
---|