blob: a3aa5c681bb57badb986ed008a37d473da9a70c3 [file] [log] [blame]
Florian Mayer824274d2018-09-17 11:33:45 +01001/*
2 * Copyright (C) 2018 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
17#ifndef SRC_PROFILING_MEMORY_CLIENT_H_
18#define SRC_PROFILING_MEMORY_CLIENT_H_
19
20#include <stddef.h>
21#include <mutex>
22#include <vector>
23
24#include "perfetto/base/scoped_file.h"
25
26namespace perfetto {
27
28class SocketPool;
29
30class BorrowedSocket {
31 public:
32 BorrowedSocket(const BorrowedSocket&) = delete;
33 BorrowedSocket& operator=(const BorrowedSocket&) = delete;
34 BorrowedSocket(BorrowedSocket&& other) {
35 fd_ = std::move(other.fd_);
36 socket_pool_ = other.socket_pool_;
37 other.socket_pool_ = nullptr;
38 }
39
40 BorrowedSocket(base::ScopedFile fd, SocketPool* socket_pool);
41 int operator*();
42 int get();
43 void Close();
44 ~BorrowedSocket();
45
46 private:
47 base::ScopedFile fd_;
48 SocketPool* socket_pool_ = nullptr;
49};
50
51class SocketPool {
52 public:
53 friend class BorrowedSocket;
54 SocketPool(std::vector<base::ScopedFile> sockets);
55
56 BorrowedSocket Borrow();
57
58 private:
59 void Return(base::ScopedFile fd);
60 std::mutex mtx_;
61 std::condition_variable cv_;
62 std::vector<base::ScopedFile> sockets_;
63 size_t available_sockets_;
64};
65
66} // namespace perfetto
67
68#endif // SRC_PROFILING_MEMORY_CLIENT_H_