blob: d0e8f615b26e86c8ccb1b2c8820e5ed5cab1581c [file] [log] [blame]
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "src/profiling/memory/client.h"
#include <inttypes.h>
#include <sys/socket.h>
#include <atomic>
#include "perfetto/base/logging.h"
#include "perfetto/base/utils.h"
#include "src/profiling/memory/transport_data.h"
namespace perfetto {
BorrowedSocket::BorrowedSocket(base::ScopedFile fd, SocketPool* socket_pool)
: fd_(std::move(fd)), socket_pool_(socket_pool) {}
int BorrowedSocket::operator*() {
return get();
}
int BorrowedSocket::get() {
return *fd_;
}
void BorrowedSocket::Close() {
fd_.reset();
}
BorrowedSocket::~BorrowedSocket() {
if (socket_pool_ != nullptr)
socket_pool_->Return(std::move(fd_));
}
SocketPool::SocketPool(std::vector<base::ScopedFile> sockets)
: sockets_(std::move(sockets)), available_sockets_(sockets_.size()) {}
BorrowedSocket SocketPool::Borrow() {
std::unique_lock<std::mutex> lck_(mtx_);
if (available_sockets_ == 0)
cv_.wait(lck_, [this] { return available_sockets_ > 0; });
PERFETTO_CHECK(available_sockets_ > 0);
return {std::move(sockets_[--available_sockets_]), this};
}
void SocketPool::Return(base::ScopedFile sock) {
std::unique_lock<std::mutex> lck_(mtx_);
PERFETTO_CHECK(available_sockets_ < sockets_.size());
sockets_[available_sockets_++] = std::move(sock);
if (available_sockets_ == 1) {
lck_.unlock();
cv_.notify_one();
}
}
} // namespace perfetto