Merge branch 'upstream'
[nativeclient.git] / service_runtime / nacl_sync.cc
blob336b39f51d4177bd92eedbf13a4ed598689aaf6b
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 * 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"
44 #if NACL_WINDOWS
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"
50 #endif
52 /* Mutex */
53 int NaClMutexCtor(struct NaClMutex *mp) {
54 mp->lock = new NaCl::Lock();
55 return 1;
58 void NaClMutexDtor(struct NaClMutex *mp) {
59 delete reinterpret_cast<NaCl::Lock *>(mp->lock);
60 mp->lock = NULL;
63 NaClSyncStatus NaClMutexLock(struct NaClMutex *mp) {
64 /* TODO - add argument validation for debug version,
65 * here and below
67 reinterpret_cast<NaCl::Lock *>(mp->lock)->Acquire();
68 return NACL_SYNC_OK;
71 NaClSyncStatus NaClMutexTryLock(struct NaClMutex *mp) {
72 return reinterpret_cast<NaCl::Lock *>(mp->lock)->Try() ? NACL_SYNC_OK :
73 NACL_SYNC_BUSY;
76 NaClSyncStatus NaClMutexUnlock(struct NaClMutex *mp) {
77 reinterpret_cast<NaCl::Lock *>(mp->lock)->Release();
78 return NACL_SYNC_OK;
81 /* Condition variable */
83 int NaClCondVarCtor(struct NaClCondVar *cvp) {
84 cvp->cv = new NaCl::ConditionVariable();
85 return 1;
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();
94 return NACL_SYNC_OK;
97 NaClSyncStatus NaClCondVarBroadcast(struct NaClCondVar *cvp) {
98 reinterpret_cast<NaCl::ConditionVariable*>(cvp->cv)->Broadcast();
99 return NACL_SYNC_OK;
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)));
106 return NACL_SYNC_OK;
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
115 + reltime->tv_nsec
116 / NaCl::Time::kNanosecondsPerMicrosecond);
117 // This is ugly, but at this point there is no better way
118 // to create TimeDelta from TimeTicks.
119 int result;
120 result = (reinterpret_cast<NaCl::ConditionVariable*>(cvp->cv)->TimedWaitRel(
121 *(reinterpret_cast<NaCl::Lock *>(mp->lock)),
122 ticks - zero));
123 if (0 == result) {
124 return NACL_SYNC_CONDVAR_TIMEDOUT;
126 return NACL_SYNC_OK;
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
135 + abstime->tv_nsec
136 / NaCl::Time::kNanosecondsPerMicrosecond);
137 int result;
138 result = reinterpret_cast<NaCl::ConditionVariable*>(cvp->cv)->TimedWaitAbs(
139 *(reinterpret_cast<NaCl::Lock *>(mp->lock)),
140 ticks);
141 if (0 == result) {
142 return NACL_SYNC_CONDVAR_TIMEDOUT;
144 return NACL_SYNC_OK;