imcplugin demo: Extend to support stat() call
[nativeclient.git] / service_runtime / nacl_desc_semaphore.c
blob5917b75c579e458ef5e06da6a86d1d9dc9880ea0
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Service Runtime. Semaphore Descriptor / Handle abstraction.
36 #include "native_client/include/portability.h"
38 #include <stdlib.h>
40 #if NACL_WINDOWS
41 # include "io.h"
42 #endif
44 #include "native_client/service_runtime/internal_errno.h"
46 #include "native_client/service_runtime/nacl_config.h"
47 #include "native_client/service_runtime/nacl_desc_base.h"
48 #include "native_client/service_runtime/nacl_desc_mutex.h"
49 #include "native_client/service_runtime/nacl_semaphore.h"
50 #include "native_client/service_runtime/nacl_log.h"
52 #include "native_client/service_runtime/include/sys/errno.h"
53 #include "native_client/service_runtime/include/sys/fcntl.h"
54 #include "native_client/service_runtime/include/sys/mman.h"
56 #include "native_client/intermodule_comm/nacl_imc_c.h"
59 * This file contains the implementation for the NaClDescSemaphore subclass
60 * of NaClDesc.
62 * NaClDescSemaphore is the subclass that wraps host-OS semaphore abstractions
65 int NaClDescSemaphoreCtor(struct NaClDescSemaphore *self, int value)
67 struct NaClDesc *basep = (struct NaClDesc *) self;
69 basep->vtbl = (struct NaClDescVtbl *) NULL;
70 if (!NaClDescCtor(basep)) {
71 return 0;
73 if (!NaClSemCtor(&self->sem, value)) {
74 NaClDescDtor(basep);
75 return 0;
78 basep->vtbl = &kNaClDescSemaphoreVtbl;
79 return 1;
82 void NaClDescSemaphoreDtor(struct NaClDesc *vself)
84 struct NaClDescSemaphore *self = (struct NaClDescSemaphore *) vself;
86 NaClLog(4, "NaClDescSemaphoreDtor(0x%08"PRIxPTR").\n",
87 (uintptr_t) vself);
88 NaClSemDtor(&self->sem);
89 vself->vtbl = (struct NaClDescVtbl *) NULL;
90 NaClDescDtor(&self->base);
93 int NaClDescSemaphoreClose(struct NaClDesc *vself,
94 struct NaClDescEffector *effp)
96 NaClDescUnref(vself);
97 return 0;
100 int NaClDescSemaphorePost(struct NaClDesc *vself,
101 struct NaClDescEffector *effp)
103 struct NaClDescSemaphore *self = (struct NaClDescSemaphore *)vself;
105 NaClSyncStatus status = NaClSemPost(&self->sem);
106 return -NaClXlateNaClSyncStatus(status);
109 int NaClDescSemaphoreSemWait(struct NaClDesc *vself,
110 struct NaClDescEffector *effp)
112 struct NaClDescSemaphore *self = (struct NaClDescSemaphore *)vself;
114 NaClSyncStatus status = NaClSemWait(&self->sem);
115 return -NaClXlateNaClSyncStatus(status);
118 int NaClDescSemaphoreGetValue(struct NaClDesc *vself,
119 struct NaClDescEffector *effp)
121 NaClLog(LOG_ERROR, "SemGetValue is not implemented yet\n");
122 return -NACL_ABI_EINVAL;
124 * TODO: sem_getvalue is not implemented on OSX.
125 * Remove this syscall or implement it using semctl
127 #if 0
128 struct NaClDescSemaphore *self = (struct NaClDescSemaphore *) vself;
129 NaClSyncStatus status = NaClSemGetValue(&self->sem);
130 return NaClXlateNaClSyncStatus(status);
131 #endif
135 struct NaClDescVtbl const kNaClDescSemaphoreVtbl = {
136 NaClDescSemaphoreDtor,
137 NaClDescMapNotImplemented,
138 NaClDescUnmapUnsafeNotImplemented,
139 NaClDescUnmapNotImplemented,
140 NaClDescReadNotImplemented,
141 NaClDescWriteNotImplemented,
142 NaClDescSeekNotImplemented,
143 NaClDescIoctlNotImplemented,
144 NaClDescFstatNotImplemented,
145 NaClDescSemaphoreClose,
146 NaClDescGetdentsNotImplemented,
147 NACL_DESC_SEMAPHORE,
148 NaClDescExternalizeSizeNotImplemented,
149 NaClDescExternalizeNotImplemented,
150 NaClDescLockNotImplemented,
151 NaClDescTryLockNotImplemented,
152 NaClDescUnlockNotImplemented,
153 NaClDescWaitNotImplemented,
154 NaClDescTimedWaitAbsNotImplemented,
155 NaClDescSignalNotImplemented,
156 NaClDescBroadcastNotImplemented,
157 NaClDescSendMsgNotImplemented,
158 NaClDescRecvMsgNotImplemented,
159 NaClDescConnectAddrNotImplemented,
160 NaClDescAcceptConnNotImplemented,
161 NaClDescSemaphorePost,
162 NaClDescSemaphoreSemWait,
163 NaClDescSemaphoreGetValue,
166 int NaClDescSemaphoreInternalize(struct NaClDesc **baseptr,
167 struct NaClDescXferState *xfer)
169 NaClLog(LOG_ERROR, "NaClDescSemaphoreInternalize: not shared yet\n");
170 return -NACL_ABI_EINVAL;