1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2013. |
---|
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.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <errno.h> |
---|
15 | #include <fcntl.h> |
---|
16 | #include <semaphore.h> |
---|
17 | #include <coverhd.h> |
---|
18 | #include <tmacros.h> |
---|
19 | #include <timesys.h> |
---|
20 | #include "test_support.h" |
---|
21 | #include <pthread.h> |
---|
22 | #include <sched.h> |
---|
23 | #include <rtems/timerdrv.h> |
---|
24 | |
---|
25 | const char rtems_test_name[] = "PSXTMSEM 05"; |
---|
26 | |
---|
27 | /* forward declarations to avoid warnings */ |
---|
28 | void *POSIX_Init(void *argument); |
---|
29 | void *Middle(void *argument); |
---|
30 | void *Low(void *argument); |
---|
31 | |
---|
32 | #define MAX_SEMS 2 |
---|
33 | |
---|
34 | sem_t sem1; |
---|
35 | |
---|
36 | void *Low( |
---|
37 | void *argument |
---|
38 | ) |
---|
39 | { |
---|
40 | int status; |
---|
41 | benchmark_timer_t end_time; |
---|
42 | |
---|
43 | status = sem_wait(&sem1); /* semaphore blocks */ |
---|
44 | end_time = benchmark_timer_read(); |
---|
45 | |
---|
46 | rtems_test_assert( status == 0 ); |
---|
47 | |
---|
48 | put_time( |
---|
49 | "sem_post: thread waiting preempt", |
---|
50 | end_time, |
---|
51 | OPERATION_COUNT, |
---|
52 | 0, |
---|
53 | 0 |
---|
54 | ); |
---|
55 | |
---|
56 | TEST_END(); |
---|
57 | rtems_test_exit( 0 ); |
---|
58 | return NULL; |
---|
59 | } |
---|
60 | |
---|
61 | void *Middle( |
---|
62 | void *argument |
---|
63 | ) |
---|
64 | { |
---|
65 | int status; |
---|
66 | |
---|
67 | status = sem_wait(&sem1); /* semaphore blocks */ |
---|
68 | rtems_test_assert( status == 0 ); |
---|
69 | |
---|
70 | /* thread switch occurs */ |
---|
71 | |
---|
72 | status = sem_post(&sem1); |
---|
73 | rtems_test_assert( status == 0 ); |
---|
74 | |
---|
75 | /* thread switch occurs */ |
---|
76 | |
---|
77 | /* should never return */ |
---|
78 | rtems_test_assert( FALSE ); |
---|
79 | return NULL; |
---|
80 | } |
---|
81 | |
---|
82 | void *POSIX_Init( |
---|
83 | void *argument |
---|
84 | ) |
---|
85 | { |
---|
86 | int i; |
---|
87 | int status; |
---|
88 | pthread_t threadId; |
---|
89 | pthread_attr_t attr; |
---|
90 | struct sched_param param; |
---|
91 | |
---|
92 | TEST_BEGIN(); |
---|
93 | |
---|
94 | /* |
---|
95 | * Deliberately create the semaphore BEFORE the threads. This way the |
---|
96 | * threads should preempt this thread and block as they are created. |
---|
97 | */ |
---|
98 | status = sem_init( &sem1, 0, 1 ); |
---|
99 | rtems_test_assert( status == 0 ); |
---|
100 | |
---|
101 | /* |
---|
102 | * Obtain the semaphore so the threads will block. |
---|
103 | */ |
---|
104 | status = sem_wait( &sem1 ); |
---|
105 | rtems_test_assert( status == 0 ); |
---|
106 | |
---|
107 | /* |
---|
108 | * Now lower our priority |
---|
109 | */ |
---|
110 | status = pthread_attr_init( &attr ); |
---|
111 | rtems_test_assert( status == 0 ); |
---|
112 | |
---|
113 | status = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED ); |
---|
114 | rtems_test_assert( status == 0 ); |
---|
115 | |
---|
116 | status = pthread_attr_setschedpolicy( &attr, SCHED_RR ); |
---|
117 | rtems_test_assert( status == 0 ); |
---|
118 | |
---|
119 | param.sched_priority = 2; |
---|
120 | status = pthread_attr_setschedparam( &attr, ¶m ); |
---|
121 | rtems_test_assert( status == 0 ); |
---|
122 | |
---|
123 | /* |
---|
124 | * And create rest of threads as more important than we are. They |
---|
125 | * will preempt us as they are created and block. |
---|
126 | */ |
---|
127 | for ( i=0 ; i < OPERATION_COUNT ; i++ ) { |
---|
128 | |
---|
129 | param.sched_priority = 3 + i; |
---|
130 | status = pthread_attr_setschedparam( &attr, ¶m ); |
---|
131 | rtems_test_assert( status == 0 ); |
---|
132 | |
---|
133 | status = pthread_create( |
---|
134 | &threadId, |
---|
135 | &attr, |
---|
136 | (i == OPERATION_COUNT - 1) ? Low : Middle, |
---|
137 | NULL |
---|
138 | ); |
---|
139 | rtems_test_assert( status == 0 ); |
---|
140 | } |
---|
141 | |
---|
142 | /* |
---|
143 | * Now start the timer which will be stopped in Low. |
---|
144 | * Release the semaphore so threads unblock and preempt. |
---|
145 | */ |
---|
146 | benchmark_timer_initialize(); |
---|
147 | |
---|
148 | status = sem_post( &sem1 ); |
---|
149 | /* thread switch occurs */ |
---|
150 | |
---|
151 | /* should never return */ |
---|
152 | rtems_test_assert( FALSE ); |
---|
153 | return NULL; |
---|
154 | } |
---|
155 | |
---|
156 | /* configuration information */ |
---|
157 | |
---|
158 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
159 | #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER |
---|
160 | |
---|
161 | #define CONFIGURE_MAXIMUM_POSIX_THREADS OPERATION_COUNT + 2 |
---|
162 | #define CONFIGURE_MAXIMUM_POSIX_SEMAPHORES MAX_SEMS |
---|
163 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
164 | |
---|
165 | #define CONFIGURE_INIT |
---|
166 | |
---|
167 | #include <rtems/confdefs.h> |
---|
168 | /* end of file */ |
---|