| 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 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 // Wait 6 times, once for the post, 5 times for the initial starting value. | 244 // Wait 6 times, once for the post, 5 times for the initial starting value. |
| 245 for (int i = 0; i < 6; ++i) { | 245 for (int i = 0; i < 6; ++i) { |
| 246 EXPECT(0 == sem_wait(&my_semaphore)); | 246 EXPECT(0 == sem_wait(&my_semaphore)); |
| 247 } | 247 } |
| 248 EXPECT(0 == pthread_join(my_thread, 0)); | 248 EXPECT(0 == pthread_join(my_thread, 0)); |
| 249 EXPECT(0 == sem_destroy(&my_semaphore)); | 249 EXPECT(0 == sem_destroy(&my_semaphore)); |
| 250 | 250 |
| 251 END_TEST(); | 251 END_TEST(); |
| 252 } | 252 } |
| 253 | 253 |
| 254 int TestSemTryWait() { |
| 255 START_TEST("test sem_trywait() and sem_getvalue()"); |
| 256 |
| 257 int start_value = 10; |
| 258 sem_t sem; |
| 259 EXPECT(0 == sem_init(&sem, 0, start_value)); |
| 260 |
| 261 int value = -1; |
| 262 EXPECT(0 == sem_getvalue(&sem, &value)); |
| 263 EXPECT(10 == value); |
| 264 // When the semaphore's value is positive, each call to |
| 265 // sem_trywait() should decrement the semaphore's value. |
| 266 for (int i = 1; i <= start_value; i++) { |
| 267 EXPECT(0 == sem_trywait(&sem)); |
| 268 EXPECT(0 == sem_getvalue(&sem, &value)); |
| 269 EXPECT(start_value - i == value); |
| 270 } |
| 271 // When the semaphore's value is zero, sem_trywait() should fail. |
| 272 EXPECT(-1 == sem_trywait(&sem)); |
| 273 EXPECT(EAGAIN == errno); |
| 274 EXPECT(0 == sem_getvalue(&sem, &value)); |
| 275 EXPECT(0 == value); |
| 276 |
| 277 EXPECT(0 == sem_destroy(&sem)); |
| 278 |
| 279 END_TEST(); |
| 280 } |
| 281 |
| 254 int main() { | 282 int main() { |
| 255 int fail_count = 0; | 283 int fail_count = 0; |
| 256 fail_count += TestSemNormalOperation(); | 284 fail_count += TestSemNormalOperation(); |
| 285 fail_count += TestSemTryWait(); |
| 257 | 286 |
| 258 // A semaphore implementation is not required to check for the | 287 // A semaphore implementation is not required to check for the |
| 259 // errors that are tested for here. nacl-newlib checks, but glibc | 288 // errors that are tested for here. nacl-newlib checks, but glibc |
| 260 // does not. | 289 // does not. |
| 261 #ifndef __GLIBC__ | 290 #ifndef __GLIBC__ |
| 262 fail_count += TestSemInitErrors(); | 291 fail_count += TestSemInitErrors(); |
| 263 fail_count += TestSemDestroy(); | 292 fail_count += TestSemDestroy(); |
| 264 fail_count += TestSemPostErrors(); | 293 fail_count += TestSemPostErrors(); |
| 265 fail_count += TestSemWaitErrors(); | 294 fail_count += TestSemWaitErrors(); |
| 266 #endif | 295 #endif |
| 267 | 296 |
| 268 std::exit(fail_count); | 297 std::exit(fail_count); |
| 269 } | 298 } |
| OLD | NEW |