1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "common/vsoc/shm/lock.h"
17 
18 #include "common/vsoc/lib/compat.h"
19 #include "common/vsoc/lib/single_sided_signal.h"
20 
21 namespace vsoc {
22 namespace layout {
23 
24 void GuestLock::Lock() {
25   uint32_t tid = gettid();
26   uint32_t expected_value;
27   uint32_t* uaddr = reinterpret_cast<uint32_t*>(&lock_uint32_);
28 
29   while (1) {
30     if (TryLock(tid, &expected_value)) {
31       return;
32     }
33     SingleSidedSignal::AwaitSignal(expected_value, uaddr);
34   }
35 }
36 
37 void GuestLock::Unlock() {
38   Sides sides_to_signal = UnlockCommon(gettid());
39   if (sides_to_signal != Sides::NoSides) {
40     SingleSidedSignal::Signal(&lock_uint32_);
41   }
42 }
43 
44 bool GuestLock::Recover() {
45   return RecoverSingleSided();
46 }
47 
48 }  // namespace layout
49 }  // namespace vsoc
50