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 * NaCl Server Runtime interruptible condvar, based on nacl_sync
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
;
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
);
77 mp
->lock_state
= NACL_INTR_LOCK_FREE
;
78 NaClXCondVarSignal(&mp
->cv
);
79 /* wait on the internal condvar according to the call type */
81 rv
= NaClCondVarWait(&cp
->cv
, &mp
->mu
);
83 rv
= NaClCondVarTimedWaitAbsolute(&cp
->cv
, &mp
->mu
, ts
);
87 * When we get here we own mp->mu again so we need to take mp as in
90 while ((NACL_SYNC_CONDVAR_TIMEDOUT
!= rv
) &&
91 (NACL_INTR_LOCK_HELD
== mp
->lock_state
)) {
93 rv
= NaClCondVarWait(&mp
->cv
, &mp
->mu
);
95 rv
= NaClCondVarTimedWaitAbsolute(&mp
->cv
, &mp
->mu
, ts
);
98 if (NACL_INTR_LOCK_FREE
== mp
->lock_state
) {
99 mp
->lock_state
= NACL_INTR_LOCK_HELD
;
102 NaClXMutexUnlock(&mp
->mu
);
106 NaClSyncStatus
NaClIntrCondVarSignal(struct NaClIntrCondVar
*cp
)
108 return NaClCondVarSignal(&cp
->cv
);
111 NaClSyncStatus
NaClIntrCondVarBroadcast(struct NaClIntrCondVar
*cp
)
113 return NaClCondVarBroadcast(&cp
->cv
);
116 void NaClIntrCondVarIntr(struct NaClIntrCondVar
*cp
)
119 * NOTE: we assume that mutexes are interrupted first, so we will
120 * fail to regain ownership of the mutex once the wait for cp->cv is
121 * completed (see NaClIntrCondVarWait above)
123 NaClCondVarBroadcast(&cp
->cv
);
126 void NaClIntrCondVarReset(struct NaClIntrCondVar
*cp
)
128 /* nothing to do here - we don't keep status */