Home | History | Annotate | Download | only in lib
      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 "sys/types.h"
     19 
     20 #include "common/vsoc/lib/compat.h"
     21 #include "common/vsoc/lib/single_sided_signal.h"
     22 
     23 namespace vsoc {
     24 namespace layout {
     25 
     26 void HostLock::Lock() {
     27   uint32_t tid = gettid();
     28   uint32_t expected_value;
     29   uint32_t* uaddr = reinterpret_cast<uint32_t*>(&lock_uint32_);
     30 
     31   while (1) {
     32     if (TryLock(tid, &expected_value)) {
     33       return;
     34     }
     35     SingleSidedSignal::AwaitSignal(expected_value, uaddr);
     36   }
     37 }
     38 
     39 void HostLock::Unlock() {
     40   Sides sides_to_signal = UnlockCommon(gettid());
     41   if (sides_to_signal != Sides::NoSides) {
     42     SingleSidedSignal::Signal(&lock_uint32_);
     43   }
     44 }
     45 
     46 bool HostLock::Recover() {
     47   return RecoverSingleSided();
     48 }
     49 
     50 }  // namespace layout
     51 }  // namespace vsoc
     52