1 // Copyright 2014 The Android Open Source Project
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "anbox/common/message_channel.h"
19 MessageChannelBase::MessageChannelBase(size_t capacity) : pos_(0U),
26 MessageChannelBase::~MessageChannelBase() {}
28 size_t MessageChannelBase::before_write() {
29 std::unique_lock<std::mutex> l(lock_, std::defer_lock);
32 while (count_ >= capacity_)
35 size_t result = pos_ + count_;
36 if (result >= capacity_)
42 void MessageChannelBase::after_write() {
44 can_read_.notify_one();
48 size_t MessageChannelBase::before_read() {
49 std::unique_lock<std::mutex> l(lock_, std::defer_lock);
56 void MessageChannelBase::after_read() {
57 if (++pos_ == capacity_)
60 can_write_.notify_one();