| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2010 The Native Client Authors. All rights reserved. | 2 * Copyright 2010 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
| 4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include <semaphore.h> | 7 #include <semaphore.h> |
| 8 | 8 |
| 9 #include <errno.h> | 9 #include <errno.h> |
| 10 #include <limits.h> | 10 #include <limits.h> |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 // Wait 6 times, once for the post, 5 times for the initial starting value. | 156 // Wait 6 times, once for the post, 5 times for the initial starting value. |
| 157 for (int i = 0; i < 6; ++i) { | 157 for (int i = 0; i < 6; ++i) { |
| 158 EXPECT(0 == sem_wait(&my_semaphore)); | 158 EXPECT(0 == sem_wait(&my_semaphore)); |
| 159 } | 159 } |
| 160 EXPECT(0 == pthread_join(my_thread, 0)); | 160 EXPECT(0 == pthread_join(my_thread, 0)); |
| 161 EXPECT(0 == sem_destroy(&my_semaphore)); | 161 EXPECT(0 == sem_destroy(&my_semaphore)); |
| 162 | 162 |
| 163 END_TEST(); | 163 END_TEST(); |
| 164 } | 164 } |
| 165 | 165 |
| 166 int TestSemTryWait() { |
| 167 START_TEST("test sem_trywait() and sem_getvalue()"); |
| 168 |
| 169 int start_value = 10; |
| 170 sem_t sem; |
| 171 EXPECT(0 == sem_init(&sem, 0, start_value)); |
| 172 |
| 173 int value = -1; |
| 174 EXPECT(0 == sem_getvalue(&sem, &value)); |
| 175 EXPECT(10 == value); |
| 176 // When the semaphore's value is positive, each call to |
| 177 // sem_trywait() should decrement the semaphore's value. |
| 178 for (int i = 1; i <= start_value; i++) { |
| 179 EXPECT(0 == sem_trywait(&sem)); |
| 180 EXPECT(0 == sem_getvalue(&sem, &value)); |
| 181 EXPECT(start_value - i == value); |
| 182 } |
| 183 // When the semaphore's value is zero, sem_trywait() should fail. |
| 184 EXPECT(-1 == sem_trywait(&sem)); |
| 185 EXPECT(EAGAIN == errno); |
| 186 EXPECT(0 == sem_getvalue(&sem, &value)); |
| 187 EXPECT(0 == value); |
| 188 |
| 189 EXPECT(0 == sem_destroy(&sem)); |
| 190 |
| 191 END_TEST(); |
| 192 } |
| 193 |
| 166 int main() { | 194 int main() { |
| 167 int fail_count = 0; | 195 int fail_count = 0; |
| 168 fail_count += TestSemInitErrors(); | 196 fail_count += TestSemInitErrors(); |
| 169 fail_count += TestSemPostErrors(); | 197 fail_count += TestSemPostErrors(); |
| 170 fail_count += TestSemNormalOperation(); | 198 fail_count += TestSemNormalOperation(); |
| 199 fail_count += TestSemTryWait(); |
| 171 std::exit(fail_count); | 200 std::exit(fail_count); |
| 172 } | 201 } |
| OLD | NEW |