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

Unified Diff: media/audio/shared_mem_synchronizer.cc

Issue 9605015: Add a SharedMemSynchronizer class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments. I'll do the file name change later so that you can see the diffs clearer. 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.cc
diff --git a/media/audio/shared_mem_synchronizer.cc b/media/audio/shared_mem_synchronizer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..942e70479d96c3eacb4c7888e2063e5862a67e92
--- /dev/null
+++ b/media/audio/shared_mem_synchronizer.cc
@@ -0,0 +1,141 @@
+// 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"
+
+#if defined(OS_POSIX)
+#include <errno.h>
+#include <sys/poll.h>
+#endif
+
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+
+#if defined(OS_POSIX)
+#include "base/file_descriptor_posix.h"
+#endif
+
+SharedMemSynchronizer::WaitForMultiple::WaitForMultiple(
+ SharedMemSynchronizer* synchronizers, size_t count)
+ : synchronizers_(synchronizers), count_(count), last_(count - 1) {
+ DCHECK_GT(count, 0U);
+}
+
+int SharedMemSynchronizer::WaitForMultiple::Wait() {
+ int ret = WaitMultiple(synchronizers_, count_, last_);
+ last_ = (ret >= 0) ? static_cast<size_t>(ret) : count_ - 1;
Ami GONE FROM CHROMIUM 2012/03/08 17:04:36 When will the conditional be false, given the DCHE
tommi (sloooow) - chröme 2012/03/09 12:15:49 That DCHECK is to catch programmer error so hopefu
Ami GONE FROM CHROMIUM 2012/03/09 16:36:47 That's what the DCHECK connotes to me, as well.
tommi (sloooow) - chröme 2012/03/13 12:26:13 Done. Added CHECKs to WaitMultiple.
+ return ret;
+}
+
+SharedMemSynchronizer::SharedMemSynchronizer() {}
+
+#if defined(OS_POSIX)
+
+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));
Ami GONE FROM CHROMIUM 2012/03/08 17:04:36 Is this depending on the socket having a buffer so
tommi (sloooow) - chröme 2012/03/09 12:15:49 Yes, the socket will buffer the signal to satisfy
Ami GONE FROM CHROMIUM 2012/03/09 16:36:47 I suspect on posix you'll get a hang as the pipe's
tommi (sloooow) - chröme 2012/03/13 12:26:13 It doesn't block actually and there should only be
+ 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(SharedMemSynchronizer* synchronizers,
+ size_t count,
+ size_t last_signaled) {
+ DCHECK_LT(last_signaled < count);
+
+ for (size_t i = 0; i < count; ++i) {
+ DCHECK(synchronizers[i].IsValid());
+ }
+
+ int ret = -1;
+
+ // 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[count]);
+ memset(&sockets[0], 0, count * sizeof(sockets[0]));
+ size_t index = 0;
+ for (size_t i = last_signaled + 1; i < count; ++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, count);
+
+ int err = poll(&sockets[0], count, -1);
+ if (err != -1) {
+ for (size_t i = 0; i < count; ++i) {
+ if (sockets[i].revents) {
+ ret = (i + last_signaled + 1) % count;
+ DCHECK_EQ(sockets[i].fd, synchronizers[ret].socket_.handle());
+ synchronizers[ret].Wait();
+ break;
Ami GONE FROM CHROMIUM 2012/03/08 17:04:36 could just return ret; here and replace the remain
tommi (sloooow) - chröme 2012/03/09 12:15:49 This has fewer returns and produces smaller code.
Ami GONE FROM CHROMIUM 2012/03/09 16:36:47 Yes.
tommi (sloooow) - chröme 2012/03/13 12:26:13 Done.
+ }
+ }
+ } else {
+ NOTREACHED() << "poll() failed errno: " << errno;
+ }
+
+ DCHECK_NE(ret, -1);
+ return ret;
+}
+
+#endif // !defined(OS_POSIX)

Powered by Google App Engine
This is Rietveld 408576698