OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /* | 7 /* |
8 * Native Client semaphore API | 8 * Native Client semaphore API |
9 */ | 9 */ |
10 | 10 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 return 0; | 83 return 0; |
84 | 84 |
85 __sync_fetch_and_add(&sem->nwaiters, 1); | 85 __sync_fetch_and_add(&sem->nwaiters, 1); |
86 do { | 86 do { |
87 __nc_irt_futex.futex_wait_abs(&sem->count, 0, NULL); | 87 __nc_irt_futex.futex_wait_abs(&sem->count, 0, NULL); |
88 } while (!decrement_if_positive(&sem->count)); | 88 } while (!decrement_if_positive(&sem->count)); |
89 __sync_fetch_and_sub(&sem->nwaiters, 1); | 89 __sync_fetch_and_sub(&sem->nwaiters, 1); |
90 return 0; | 90 return 0; |
91 } | 91 } |
92 | 92 |
| 93 int sem_trywait(sem_t *sem) { |
| 94 if (decrement_if_positive(&sem->count)) |
| 95 return 0; |
| 96 |
| 97 errno = EAGAIN; |
| 98 return -1; |
| 99 } |
| 100 |
93 int sem_post(sem_t *sem) { | 101 int sem_post(sem_t *sem) { |
94 /* Increment sem->count, checking for overflow. */ | 102 /* Increment sem->count, checking for overflow. */ |
95 int old_value; | 103 int old_value; |
96 do { | 104 do { |
97 old_value = sem->count; | 105 old_value = sem->count; |
98 if (old_value == SEM_VALUE_MAX) { | 106 if (old_value == SEM_VALUE_MAX) { |
99 errno = EOVERFLOW; | 107 errno = EOVERFLOW; |
100 return -1; | 108 return -1; |
101 } | 109 } |
102 } while (!__sync_bool_compare_and_swap(&sem->count, old_value, | 110 } while (!__sync_bool_compare_and_swap(&sem->count, old_value, |
103 old_value + 1)); | 111 old_value + 1)); |
104 | 112 |
105 /* | 113 /* |
106 * We only need to call futex_wake() if there are waiters. Note | 114 * We only need to call futex_wake() if there are waiters. Note |
107 * that this might do an unnecessary call to futex_wake() if the | 115 * that this might do an unnecessary call to futex_wake() if the |
108 * last waiter has already been woken using futex_wake() but has not | 116 * last waiter has already been woken using futex_wake() but has not |
109 * yet decremented sem->nwaiters. | 117 * yet decremented sem->nwaiters. |
110 */ | 118 */ |
111 if (sem->nwaiters != 0) { | 119 if (sem->nwaiters != 0) { |
112 int woken_count; | 120 int woken_count; |
113 __nc_irt_futex.futex_wake(&sem->count, 1, &woken_count); | 121 __nc_irt_futex.futex_wake(&sem->count, 1, &woken_count); |
114 } | 122 } |
115 return 0; | 123 return 0; |
116 } | 124 } |
| 125 |
| 126 int sem_getvalue(sem_t *sem, int *value) { |
| 127 *value = sem->count; |
| 128 return 0; |
| 129 } |
OLD | NEW |