1 : /*
2 : * Copyright 2008 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can
4 : * be found in the LICENSE file.
5 : */
6 :
7 : /*
8 : * NaCl semaphore implementation (Linux)
9 : */
10 :
11 : #include <errno.h>
12 : #include <unistd.h>
13 :
14 : #include "native_client/src/shared/platform/linux/nacl_semaphore.h"
15 : #include "native_client/src/shared/platform/nacl_log.h"
16 : #include "native_client/src/shared/platform/nacl_sync.h"
17 :
18 1 : int NaClSemCtor(struct NaClSemaphore *sem, int32_t value) {
19 : /*
20 : * TODO(gregoryd): make sure that sem_init behaves identically on all
21 : * platforms. Examples: negative values, very large value.
22 : */
23 1 : return (0 == sem_init(&sem->sem_obj, 0, value));
24 : }
25 :
26 1 : void NaClSemDtor(struct NaClSemaphore *sem) {
27 1 : sem_destroy(&sem->sem_obj);
28 1 : }
29 :
30 12 : NaClSyncStatus NaClSemWait(struct NaClSemaphore *sem) {
31 12 : sem_wait(&sem->sem_obj); /* always returns 0 */
32 12 : return NACL_SYNC_OK;
33 : }
34 :
35 71 : NaClSyncStatus NaClSemTryWait(struct NaClSemaphore *sem) {
36 71 : if (0 == sem_trywait(&sem->sem_obj)) {
37 4 : return NACL_SYNC_OK;
38 : }
39 68 : return NACL_SYNC_BUSY;
40 : }
41 :
42 16 : NaClSyncStatus NaClSemPost(struct NaClSemaphore *sem) {
43 16 : if (0 == sem_post(&sem->sem_obj)) {
44 16 : return NACL_SYNC_OK;
45 : }
46 : /* Posting above SEM_MAX_VALUE does not always fail, but sometimes it may */
47 0 : if ((ERANGE == errno) || (EOVERFLOW == errno)) {
48 0 : return NACL_SYNC_SEM_RANGE_ERROR;
49 : }
50 0 : return NACL_SYNC_INTERNAL_ERROR;
51 : }
52 :
53 0 : int32_t NaClSemGetValue(struct NaClSemaphore *sem) {
54 : int32_t value;
55 0 : sem_getvalue(&sem->sem_obj, &value); /* always returns 0 */
56 0 : return value;
57 : }
|