Remove recursive mutex in IncomingFrameReader.

PiperOrigin-RevId: 662219648
This commit is contained in:
Francis Tsui
2024-08-12 14:05:46 -07:00
committed by Copybara-Service
parent c148174cb0
commit 0a4fbb0cb7
4 changed files with 129 additions and 113 deletions
+17 -13
View File
@@ -24,6 +24,7 @@
#include <queue>
#include <vector>
#include "absl/base/thread_annotations.h"
#include "absl/time/time.h"
#include "internal/platform/mutex.h"
#include "internal/platform/task_runner.h"
@@ -55,7 +56,7 @@ class IncomingFramesReader
virtual void ReadFrame(
std::function<
void(std::optional<nearby::sharing::service::proto::V1Frame>)>
callback);
callback) ABSL_LOCKS_EXCLUDED(mutex_);
// Reads a frame of type |frame_type| from |connection|. |callback| is called
// with the frame read from connection or nullopt if connection socket is
@@ -68,7 +69,7 @@ class IncomingFramesReader
std::function<
void(std::optional<nearby::sharing::service::proto::V1Frame>)>
callback,
absl::Duration timeout);
absl::Duration timeout) ABSL_LOCKS_EXCLUDED(mutex_);
std::weak_ptr<IncomingFramesReader> GetWeakPtr() {
return this->weak_from_this();
@@ -83,30 +84,33 @@ class IncomingFramesReader
std::optional<absl::Duration> timeout = std::nullopt;
};
void ReadNextFrame();
void OnDataReadFromConnection(std::optional<std::vector<uint8_t>> bytes);
void OnFrameDecoded(
std::optional<nearby::sharing::service::proto::Frame> frame);
void CloseAllPendingReads() ABSL_LOCKS_EXCLUDED(mutex_);
void ReadNextFrame() ABSL_LOCKS_EXCLUDED(mutex_);
void OnDataReadFromConnection(std::optional<std::vector<uint8_t>> bytes)
ABSL_LOCKS_EXCLUDED(mutex_);
const nearby::sharing::service::proto::V1Frame* OnFrameDecoded(
const nearby::sharing::service::proto::Frame& frame)
ABSL_LOCKS_EXCLUDED(mutex_);
void OnTimeout();
void Done(std::optional<nearby::sharing::service::proto::V1Frame> frame);
void Done(const nearby::sharing::service::proto::V1Frame& frame)
ABSL_LOCKS_EXCLUDED(mutex_);
std::optional<nearby::sharing::service::proto::V1Frame> GetCachedFrame(
std::optional<nearby::sharing::service::proto::V1Frame_FrameType>
frame_type);
frame_type) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
TaskRunner& service_thread_;
const NearbySharingDecoder& decoder_;
NearbyConnection* connection_;
NearbyConnection* const connection_;
RecursiveMutex mutex_;
std::queue<ReadFrameInfo> read_frame_info_queue_;
std::function<void()> timeout_callback_;
std::queue<ReadFrameInfo> read_frame_info_queue_ ABSL_GUARDED_BY(mutex_);
// Caches frames read from NearbyConnection which are not used immediately.
std::map<nearby::sharing::service::proto::V1Frame_FrameType,
std::optional<nearby::sharing::service::proto::V1Frame>>
cached_frames_;
cached_frames_ ABSL_GUARDED_BY(mutex_);
std::unique_ptr<ThreadTimer> timeout_timer_;
std::unique_ptr<ThreadTimer> timeout_timer_ ABSL_GUARDED_BY(mutex_);
};
} // namespace sharing