2 * Copyright 2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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 * C bindings for C++ implementation of synchronization objects
34 * (based on Crome code)
37 #include "native_client/include/portability.h"
39 #include <sys/types.h>
40 #include <sys/timeb.h>
42 #include "native_client/service_runtime/nacl_sync.h"
45 # include "native_client/service_runtime/win/lock.h"
46 # include "native_client/service_runtime/win/condition_variable.h"
47 #elif NACL_LINUX || NACL_OSX
48 # include "native_client/service_runtime/linux/lock.h"
49 # include "native_client/service_runtime/linux/condition_variable.h"
53 int NaClMutexCtor(struct NaClMutex
*mp
) {
54 mp
->lock
= new NaCl::Lock();
58 void NaClMutexDtor(struct NaClMutex
*mp
) {
59 delete reinterpret_cast<NaCl::Lock
*>(mp
->lock
);
63 NaClSyncStatus
NaClMutexLock(struct NaClMutex
*mp
) {
64 /* TODO - add argument validation for debug version,
67 reinterpret_cast<NaCl::Lock
*>(mp
->lock
)->Acquire();
71 NaClSyncStatus
NaClMutexTryLock(struct NaClMutex
*mp
) {
72 return reinterpret_cast<NaCl::Lock
*>(mp
->lock
)->Try() ? NACL_SYNC_OK
:
76 NaClSyncStatus
NaClMutexUnlock(struct NaClMutex
*mp
) {
77 reinterpret_cast<NaCl::Lock
*>(mp
->lock
)->Release();
81 /* Condition variable */
83 int NaClCondVarCtor(struct NaClCondVar
*cvp
) {
84 cvp
->cv
= new NaCl::ConditionVariable();
88 void NaClCondVarDtor(struct NaClCondVar
*cvp
) {
89 delete reinterpret_cast<NaCl::ConditionVariable
*>(cvp
->cv
);
92 NaClSyncStatus
NaClCondVarSignal(struct NaClCondVar
*cvp
) {
93 reinterpret_cast<NaCl::ConditionVariable
*>(cvp
->cv
)->Signal();
97 NaClSyncStatus
NaClCondVarBroadcast(struct NaClCondVar
*cvp
) {
98 reinterpret_cast<NaCl::ConditionVariable
*>(cvp
->cv
)->Broadcast();
102 NaClSyncStatus
NaClCondVarWait(struct NaClCondVar
*cvp
,
103 struct NaClMutex
*mp
) {
104 reinterpret_cast<NaCl::ConditionVariable
*>(cvp
->cv
)->Wait(
105 *(reinterpret_cast<NaCl::Lock
*>(mp
->lock
)));
109 NaClSyncStatus
NaClCondVarTimedWaitRelative(
110 struct NaClCondVar
*cvp
,
111 struct NaClMutex
*mp
,
112 struct nacl_abi_timespec
const *reltime
) {
113 NaCl::TimeTicks zero
, ticks(reltime
->tv_sec
114 * NaCl::Time::kMicrosecondsPerSecond
116 / NaCl::Time::kNanosecondsPerMicrosecond
);
117 // This is ugly, but at this point there is no better way
118 // to create TimeDelta from TimeTicks.
120 result
= (reinterpret_cast<NaCl::ConditionVariable
*>(cvp
->cv
)->TimedWaitRel(
121 *(reinterpret_cast<NaCl::Lock
*>(mp
->lock
)),
124 return NACL_SYNC_CONDVAR_TIMEDOUT
;
129 NaClSyncStatus
NaClCondVarTimedWaitAbsolute(
130 struct NaClCondVar
*cvp
,
131 struct NaClMutex
*mp
,
132 struct nacl_abi_timespec
const *abstime
) {
133 NaCl::TimeTicks
ticks(abstime
->tv_sec
134 * NaCl::Time::kMicrosecondsPerSecond
136 / NaCl::Time::kNanosecondsPerMicrosecond
);
138 result
= reinterpret_cast<NaCl::ConditionVariable
*>(cvp
->cv
)->TimedWaitAbs(
139 *(reinterpret_cast<NaCl::Lock
*>(mp
->lock
)),
142 return NACL_SYNC_CONDVAR_TIMEDOUT
;