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 | #if !defined(OPERATION_COUNT) |
---|
11 | #define OPERATION_COUNT 100 |
---|
12 | #endif |
---|
13 | |
---|
14 | #if defined(USE_WAIT) |
---|
15 | #define TEST_NUMBER "08" |
---|
16 | #define TEST_CASE "pthread_cond_wait: blocking" |
---|
17 | #elif defined(USE_TIMEDWAIT_WITH_VALUE) |
---|
18 | #define TEST_NUMBER "09" |
---|
19 | #define TEST_CASE "pthread_cond_timedwait: blocking" |
---|
20 | #elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST) |
---|
21 | #define TEST_NUMBER "10" |
---|
22 | #define TEST_CASE "pthread_cond_timedwait: time in past error" |
---|
23 | #else |
---|
24 | #error "How am I being compiled?" |
---|
25 | #endif |
---|
26 | |
---|
27 | #ifdef HAVE_CONFIG_H |
---|
28 | #include "config.h" |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <sys/time.h> |
---|
32 | #include <stdio.h> |
---|
33 | #include <time.h> |
---|
34 | #include <errno.h> |
---|
35 | #include <sched.h> |
---|
36 | #include <timesys.h> |
---|
37 | #include <tmacros.h> |
---|
38 | #include <rtems/btimer.h> |
---|
39 | #include "test_support.h" |
---|
40 | |
---|
41 | #include <pthread.h> |
---|
42 | |
---|
43 | const char rtems_test_name[] = "PSXTMCOND 08"; |
---|
44 | |
---|
45 | /* forward declarations to avoid warnings */ |
---|
46 | void *POSIX_Init(void *argument); |
---|
47 | void *Middle(void *argument); |
---|
48 | void *Low(void *argument); |
---|
49 | |
---|
50 | pthread_cond_t CondID; |
---|
51 | pthread_mutex_t MutexID; |
---|
52 | struct timespec sleepTime; |
---|
53 | |
---|
54 | void *Low( |
---|
55 | void *argument |
---|
56 | ) |
---|
57 | { |
---|
58 | uint32_t end_time; |
---|
59 | |
---|
60 | end_time = benchmark_timer_read(); |
---|
61 | |
---|
62 | put_time( |
---|
63 | TEST_CASE, |
---|
64 | end_time, |
---|
65 | OPERATION_COUNT, |
---|
66 | 0, |
---|
67 | 0 |
---|
68 | ); |
---|
69 | |
---|
70 | TEST_END(); |
---|
71 | |
---|
72 | rtems_test_exit( 0 ); |
---|
73 | return NULL; |
---|
74 | } |
---|
75 | |
---|
76 | void *Middle( |
---|
77 | void *argument |
---|
78 | ) |
---|
79 | { |
---|
80 | int rc; |
---|
81 | |
---|
82 | |
---|
83 | rc = pthread_mutex_lock(&MutexID); |
---|
84 | rtems_test_assert( rc == 0 ); |
---|
85 | |
---|
86 | /* block and switch to another task here */ |
---|
87 | |
---|
88 | #if defined(USE_WAIT) |
---|
89 | rc = pthread_cond_wait( &CondID, &MutexID ); |
---|
90 | rtems_test_assert( rc == 0 ); |
---|
91 | |
---|
92 | #elif defined(USE_TIMEDWAIT_WITH_VALUE) |
---|
93 | /* adjust sleepTime to get something obviously in the future */ |
---|
94 | ++sleepTime.tv_sec; |
---|
95 | |
---|
96 | rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime ); |
---|
97 | rtems_test_assert( rc == 0 ); |
---|
98 | |
---|
99 | #elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST) |
---|
100 | { |
---|
101 | /* override sleepTime with something obviously in the past */ |
---|
102 | sleepTime.tv_sec = 0; |
---|
103 | sleepTime.tv_nsec = 5; |
---|
104 | |
---|
105 | /* this does all the work of timedwait but immediately returns */ |
---|
106 | rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime ); |
---|
107 | rtems_test_assert(rc == ETIMEDOUT); |
---|
108 | benchmark_timer_read(); |
---|
109 | } |
---|
110 | #endif |
---|
111 | |
---|
112 | pthread_mutex_unlock(&MutexID); |
---|
113 | #if defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST) |
---|
114 | /* |
---|
115 | * In this case, unlock does not switch to another thread. so we need |
---|
116 | * to explicitly yield. If we do not yield, then we will measure the |
---|
117 | * time required to do an implicit pthread_exit() which is undesirable |
---|
118 | * from a measurement viewpoint. |
---|
119 | */ |
---|
120 | sched_yield(); |
---|
121 | #endif |
---|
122 | return NULL; |
---|
123 | } |
---|
124 | |
---|
125 | void *POSIX_Init( |
---|
126 | void *argument |
---|
127 | ) |
---|
128 | { |
---|
129 | int i; |
---|
130 | int status; |
---|
131 | pthread_t threadId; |
---|
132 | int rc; |
---|
133 | struct timeval tp; |
---|
134 | |
---|
135 | TEST_BEGIN(); |
---|
136 | |
---|
137 | rc = gettimeofday(&tp, NULL); |
---|
138 | rtems_test_assert( rc == 0 ); |
---|
139 | |
---|
140 | /* Convert from timeval to timespec */ |
---|
141 | sleepTime.tv_sec = tp.tv_sec; |
---|
142 | sleepTime.tv_nsec = tp.tv_usec * 1000; |
---|
143 | |
---|
144 | rc = pthread_cond_init(&CondID, NULL); |
---|
145 | rtems_test_assert( rc == 0 ); |
---|
146 | |
---|
147 | rc = pthread_mutex_init(&MutexID, NULL); |
---|
148 | rtems_test_assert( rc == 0 ); |
---|
149 | |
---|
150 | rc = pthread_mutex_lock(&MutexID); |
---|
151 | rtems_test_assert( rc == 0 ); |
---|
152 | |
---|
153 | for ( i=0 ; i < OPERATION_COUNT - 1 ; i++ ) { |
---|
154 | status = pthread_create( &threadId, NULL, Middle, NULL ); |
---|
155 | rtems_test_assert( !status ); |
---|
156 | } |
---|
157 | |
---|
158 | status = pthread_create( &threadId, NULL, Low, NULL ); |
---|
159 | rtems_test_assert( !status ); |
---|
160 | |
---|
161 | /* start the timer and switch through all the other tasks */ |
---|
162 | benchmark_timer_initialize(); |
---|
163 | |
---|
164 | rc = pthread_mutex_unlock(&MutexID); |
---|
165 | rtems_test_assert( rc == 0 ); |
---|
166 | |
---|
167 | /* Should never return. */ |
---|
168 | return NULL; |
---|
169 | } |
---|
170 | |
---|
171 | /* configuration information */ |
---|
172 | |
---|
173 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
174 | #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER |
---|
175 | |
---|
176 | #define CONFIGURE_MAXIMUM_POSIX_THREADS OPERATION_COUNT + 2 |
---|
177 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
178 | |
---|
179 | #define CONFIGURE_INIT |
---|
180 | |
---|
181 | #include <rtems/confdefs.h> |
---|
182 | /* end of file */ |
---|