Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / android / forwarder2 / pipe_notifier.cc
blobff944fbc3d1373329e7c95596640394ccb409cf3
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "tools/android/forwarder2/pipe_notifier.h"
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
12 #include "base/logging.h"
13 #include "base/posix/eintr_wrapper.h"
15 namespace forwarder2 {
17 PipeNotifier::PipeNotifier() {
18 int pipe_fd[2];
19 int ret = pipe(pipe_fd);
20 CHECK_EQ(0, ret);
21 receiver_fd_ = pipe_fd[0];
22 sender_fd_ = pipe_fd[1];
23 fcntl(sender_fd_, F_SETFL, O_NONBLOCK);
26 PipeNotifier::~PipeNotifier() {
27 close(receiver_fd_);
28 close(sender_fd_);
31 bool PipeNotifier::Notify() {
32 CHECK_NE(-1, sender_fd_);
33 errno = 0;
34 int ret = HANDLE_EINTR(write(sender_fd_, "1", 1));
35 if (ret < 0) {
36 PLOG(ERROR) << "write";
37 return false;
39 return true;
42 void PipeNotifier::Reset() {
43 char c;
44 int ret = HANDLE_EINTR(read(receiver_fd_, &c, 1));
45 if (ret < 0) {
46 PLOG(ERROR) << "read";
47 return;
49 DCHECK_EQ(1, ret);
50 DCHECK_EQ('1', c);
53 } // namespace forwarder