Make ncval-annotate and ncval-stubout executable
[nativeclient.git] / service_runtime / nacl_interruptible_condvar.c
blobdca97e20d9dea71c6aedfaf4b41018ff1219a258
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 Server Runtime interruptible condvar, based on nacl_sync
34 * interface.
37 #include "native_client/include/portability.h"
38 #include "native_client/service_runtime/nacl_interruptible_condvar.h"
40 #include "native_client/service_runtime/nacl_log.h"
41 #include "native_client/service_runtime/nacl_sync_checked.h"
43 int NaClIntrCondVarCtor(struct NaClIntrCondVar *cp)
45 return NaClCondVarCtor(&cp->cv);
49 void NaClIntrCondVarDtor(struct NaClIntrCondVar *cp)
51 NaClCondVarDtor(&cp->cv);
54 NaClSyncStatus NaClIntrCondVarWait(struct NaClIntrCondVar *cp,
55 struct NaClIntrMutex *mp,
56 struct nacl_abi_timespec const *ts)
58 NaClSyncStatus rv = NACL_SYNC_INTERNAL_ERROR;
59 NaClXMutexLock(&mp->mu);
61 if (NACL_INTR_LOCK_HELD != mp->lock_state) {
62 if (NACL_INTR_LOCK_FREE == mp->lock_state) {
63 /* NACL_INTR_LOCK_FREE - error - you must hold the lock */
64 rv = NACL_SYNC_MUTEX_PERMISSION;
66 } else {
67 /* NACL_INTR_LOCK_INTERRUPTED - just fail the request, we assume
68 * that all objects are interrupted for the same reason
70 rv = NACL_SYNC_INTERNAL_ERROR;
73 NaClXMutexUnlock(&mp->mu);
74 return rv;
77 mp->lock_state = NACL_INTR_LOCK_FREE;
78 NaClXCondVarSignal(&mp->cv);
79 /* wait on the internal condvar according to the call type */
80 if (NULL == ts) {
81 rv = NaClCondVarWait(&cp->cv, &mp->mu);
82 } else {
83 rv = NaClCondVarTimedWaitAbsolute(&cp->cv, &mp->mu, ts);
86 /* When we get here we own mp->mu again
87 * so we need to take mp as in its implementation
89 while ((NACL_SYNC_CONDVAR_TIMEDOUT != rv) &&
90 (NACL_INTR_LOCK_HELD == mp->lock_state)) {
91 if (NULL == ts) {
92 rv = NaClCondVarWait(&mp->cv, &mp->mu);
93 } else {
94 rv = NaClCondVarTimedWaitAbsolute(&mp->cv, &mp->mu, ts);
97 if (NACL_INTR_LOCK_FREE == mp->lock_state) {
98 mp->lock_state = NACL_INTR_LOCK_HELD;
99 rv = NACL_SYNC_OK;
101 NaClXMutexUnlock(&mp->mu);
102 return rv;
105 NaClSyncStatus NaClIntrCondVarSignal(struct NaClIntrCondVar *cp)
107 return NaClCondVarSignal(&cp->cv);
110 NaClSyncStatus NaClIntrCondVarBroadcast(struct NaClIntrCondVar *cp)
112 return NaClCondVarBroadcast(&cp->cv);
115 void NaClIntrCondVarIntr(struct NaClIntrCondVar *cp)
117 /* NOTE: we assume that mutexes are interrupted first,
118 * so we will fail to regain ownership of the mutex once
119 * the wait for cp->cv is completed (see NaClIntrCondVarWait above) */
120 NaClCondVarBroadcast(&cp->cv);
123 void NaClIntrCondVarReset(struct NaClIntrCondVar *cp)
125 /* nothing to do here - we don't keep status */
126 return;