Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Unified Diff: media/audio/shared_mem_synchronizer_posix.cc

Issue 9605015: Add a SharedMemSynchronizer class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review comments from Ami Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/audio/shared_mem_synchronizer_posix.cc
diff --git a/media/audio/shared_mem_synchronizer_posix.cc b/media/audio/shared_mem_synchronizer_posix.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3d6254d13454b6da515a06237a4426d42d4f1606
--- /dev/null
+++ b/media/audio/shared_mem_synchronizer_posix.cc
@@ -0,0 +1,116 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/audio/shared_mem_synchronizer.h"
+
+#include <errno.h>
+#include <sys/poll.h>
+
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/file_descriptor_posix.h"
+
+SharedMemSynchronizer::~SharedMemSynchronizer() {}
+
+SharedMemSynchronizer::SharedMemSynchronizer(IPCHandle handle_1,
+ IPCHandle handle_2)
+ : socket_(handle_1.fd) {
+ DCHECK_NE(handle_1.fd, -1);
+ DCHECK_EQ(handle_2.fd, -1);
+ DCHECK(IsValid());
+}
+
+void SharedMemSynchronizer::Signal() {
+ DCHECK(IsValid());
+ char signal = 1;
+ size_t bytes = socket_.Send(&signal, sizeof(signal));
+ DCHECK_EQ(bytes, 1U) << "errno: " << errno;
+}
+
+void SharedMemSynchronizer::Wait() {
+ DCHECK(IsValid());
+ char signal = 0;
+ size_t bytes = socket_.Receive(&signal, sizeof(signal));
+ DCHECK_EQ(bytes, 1U) << "errno: " << errno;
+ DCHECK_EQ(signal, 1);
+}
+
+bool SharedMemSynchronizer::IsValid() const {
+ return socket_.handle() != SocketClass::kInvalidHandle;
+}
+
+bool SharedMemSynchronizer::ShareToProcess(base::ProcessHandle process,
+ IPCHandle* handle_1,
+ IPCHandle* handle_2) {
+ DCHECK(IsValid());
+ handle_1->fd = socket_.handle();
+ handle_1->auto_close = false;
+ handle_2->fd = -1;
+ return true;
+}
+
+// static
+bool SharedMemSynchronizer::InitializePair(SharedMemSynchronizer* a,
+ SharedMemSynchronizer* b) {
+ DCHECK(!a->IsValid());
+ DCHECK(!b->IsValid());
+
+ bool ok = SocketClass::CreatePair(&a->socket_, &b->socket_);
+
+ DLOG_IF(WARNING, !ok) << "failed to create socket: " << errno;
+ DCHECK(!ok || a->IsValid());
+ DCHECK(!ok || b->IsValid());
+ return ok;
+}
+
+// static
+int SharedMemSynchronizer::WaitMultiple(const SynchronizerVector& synchronizers,
+ size_t last_signaled) {
+ DCHECK_LT(last_signaled, synchronizers.size());
+
+ for (size_t i = 0; i < synchronizers.size(); ++i) {
+ DCHECK(synchronizers[i]->IsValid());
+ }
+
+ // Below, we always check the |revents| of the first socket in the array
+ // and return the index of that socket if set. This can cause sockets
+ // that come later in the array to starve when the first sockets are
+ // very busy. So to avoid the starving problem, we use the |last_signaled|
+ // variable to split up the array so that the last socket to be signaled
+ // becomes the last socket in the array and all the other sockets will have
+ // priority the next time WaitMultiple is called.
+ scoped_array<struct pollfd> sockets(new struct pollfd[synchronizers.size()]);
+ memset(&sockets[0], 0, synchronizers.size() * sizeof(sockets[0]));
+ size_t index = 0;
+ for (size_t i = last_signaled + 1; i < synchronizers.size(); ++i) {
+ struct pollfd& fd = sockets[index++];
+ fd.events = POLLIN;
+ fd.fd = synchronizers[i]->socket_.handle();
+ }
+
+ for (size_t i = 0; i <= last_signaled; ++i) {
+ struct pollfd& fd = sockets[index++];
+ fd.events = POLLIN;
+ fd.fd = synchronizers[i]->socket_.handle();
+ }
+ DCHECK_EQ(index, synchronizers.size());
+
+ int err = poll(&sockets[0], synchronizers.size(), -1);
+ if (err != -1) {
+ for (size_t i = 0; i < synchronizers.size(); ++i) {
+ if (sockets[i].revents) {
+ int ret = (i + last_signaled + 1) % synchronizers.size();
Ami GONE FROM CHROMIUM 2012/03/13 20:08:02 It is asymmetric that ret is an "int" but last_sig
tommi (sloooow) - chröme 2012/03/14 13:32:43 Done.
+ DCHECK_EQ(sockets[i].fd, synchronizers[ret]->socket_.handle());
+ synchronizers[ret]->Wait();
+ return ret;
+ }
+ }
+ } else {
+ LOG(ERROR) << "poll() failed: " << errno;
Ami GONE FROM CHROMIUM 2012/03/13 20:08:02 The case of poll returning 0 but none of the reven
tommi (sloooow) - chröme 2012/03/14 13:32:43 Done.
+ }
+ // Either poll() failed or we failed to find a single socket that was
+ // signaled. Either way continuing will result in undefined behavior.
+ CHECK(false);
+ return -1;
+}

Powered by Google App Engine
This is Rietveld 408576698