imcplugin demo: Extend to support stat() call
[nativeclient.git] / service_runtime / nacl_interruptible_mutex.c
blobc1f9037597c4bf3c71beed8c657cf4d6f34035b5
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 binary mutex, based on nacl_sync
34 * interface.
37 #include "native_client/include/portability.h"
38 #include "native_client/service_runtime/nacl_interruptible_mutex.h"
40 #include "native_client/service_runtime/nacl_log.h"
41 #include "native_client/service_runtime/nacl_sync_checked.h"
43 int NaClIntrMutexCtor(struct NaClIntrMutex *mp)
45 if (!NaClMutexCtor(&mp->mu)) {
46 return 0;
48 if (!NaClCondVarCtor(&mp->cv)) {
49 NaClMutexDtor(&mp->mu);
50 return 0;
52 mp->lock_state = NACL_INTR_LOCK_FREE;
53 return 1;
56 void NaClIntrMutexDtor(struct NaClIntrMutex *mp)
58 NaClCondVarDtor(&mp->cv);
59 NaClMutexDtor(&mp->mu);
62 NaClSyncStatus NaClIntrMutexLock(struct NaClIntrMutex *mp)
64 NaClSyncStatus rv = NACL_SYNC_INTERNAL_ERROR;
65 NaClXMutexLock(&mp->mu);
66 while (NACL_INTR_LOCK_HELD == mp->lock_state) {
67 NaClXCondVarWait(&mp->cv, &mp->mu);
69 if (NACL_INTR_LOCK_FREE == mp->lock_state) {
70 mp->lock_state = NACL_INTR_LOCK_HELD;
71 rv = NACL_SYNC_OK;
74 if (NACL_INTR_LOCK_INTERRUPTED == mp->lock_state) {
75 rv = NACL_SYNC_MUTEX_INTERRUPTED;
77 NaClXMutexUnlock(&mp->mu);
78 return rv;
81 NaClSyncStatus NaClIntrMutexTryLock(struct NaClIntrMutex *mp)
83 NaClSyncStatus rv = NACL_SYNC_INTERNAL_ERROR;
85 NaClXMutexLock(&mp->mu);
86 switch (mp->lock_state) {
87 case NACL_INTR_LOCK_FREE:
88 mp->lock_state = NACL_INTR_LOCK_HELD;
89 rv = NACL_SYNC_OK;
90 break;
91 case NACL_INTR_LOCK_HELD:
92 rv = NACL_SYNC_BUSY;
93 break;
94 case NACL_INTR_LOCK_INTERRUPTED:
95 rv = NACL_SYNC_MUTEX_INTERRUPTED;
96 break;
97 default:
98 rv = NACL_SYNC_INTERNAL_ERROR;
99 break;
101 NaClXMutexUnlock(&mp->mu);
102 return rv;
105 NaClSyncStatus NaClIntrMutexUnlock(struct NaClIntrMutex *mp)
107 NaClSyncStatus rv = NACL_SYNC_INTERNAL_ERROR;
108 NaClXMutexLock(&mp->mu);
109 if (NACL_INTR_LOCK_HELD != mp->lock_state) {
110 NaClLog(1, "NaClIntrMutxUnlock: unlocking when lock is not held\n");
111 rv = NACL_SYNC_MUTEX_PERMISSION;
112 } else {
113 rv = NACL_SYNC_OK;
115 mp->lock_state = NACL_INTR_LOCK_FREE;
117 NaClXCondVarSignal(&mp->cv);
118 NaClXMutexUnlock(&mp->mu);
119 return rv;
122 void NaClIntrMutexIntr(struct NaClIntrMutex *mp)
124 NaClXMutexLock(&mp->mu);
125 if (NACL_INTR_LOCK_HELD == mp->lock_state) {
126 /* potentially there are threads waiting for this thread */
127 mp->lock_state = NACL_INTR_LOCK_INTERRUPTED;
128 NaClCondVarBroadcast(&mp->cv);
129 } else {
130 mp->lock_state = NACL_INTR_LOCK_INTERRUPTED;
132 NaClXMutexUnlock(&mp->mu);
136 * Reset the interruptible mutex, presumably after the condition
137 * causing the interrupt has been cleared. In our case, this would be
138 * an E_MOVE_ADDRESS_SPACE induced address space move.
140 * This is safe to invoke only after all threads are known to be in a
141 * quiescent state -- i.e., will no longer call
142 * NaClIntrMutex{Try,}Lock on the interruptible mutex -- since there
143 * is no guarntee that all the threads awaken by NaClIntrMutexIntr
144 * have actually been run yet.
146 void NaClIntrMutexReset(struct NaClIntrMutex *mp)
148 NaClXMutexLock(&mp->mu);
149 if (NACL_INTR_LOCK_INTERRUPTED != mp->lock_state) {
150 NaClLog(LOG_FATAL,
151 "NaClIntrMutexReset: lock at 0x%08"PRIxPTR" not interrupted\n",
152 (uintptr_t) mp);
154 mp->lock_state = NACL_INTR_LOCK_FREE;
155 NaClXMutexUnlock(&mp->mu);